View previous topic :: View next topic |
Author |
Message |
albertsm
Joined: 09 Apr 2004 Posts: 5913 Location: Holland
|
Posted: Tue Nov 24, 2020 7:47 pm Post subject: |
|
|
please make sure when you try that that these files do not exist.
it is not clear if they exist or not and what the files size is.
i need to know the exact conditions.
as i see it, the file exist when you try and then it will work. _________________ Mark |
|
Back to top |
|
|
hzz
Joined: 20 Feb 2007 Posts: 314
|
Posted: Tue Nov 24, 2020 8:19 pm Post subject: |
|
|
This is what I found. I hope you can make any sense of it:
- It doesn't matter if the file is just created or already existed in the SD Card, the behavior is always the same.
- The first SEEK in the code only works if the file is closed and opened again before the SEEK is called and after the last PUT was called
- Subsecuent SEEK statements in the code always work correctly, no matter if any PUT has been called before and no matter if the file has been closed or not
This is what I tested
Code: | If Sd_card_ready = 1 Then
' ------------------------------------------------------------------------------------
P_file_handle = Freefile()
File_name1 = "Test17.bin"
Open File_name1 For Binary As #p_file_handle
' ------------------------------------------------------------------------------------
For Btemp1 = 1 To 255
Put #p_file_handle , Btemp1 ' Fill first 255 positions
Next Btemp1
' ------------------------------------------
' Close #p_file_handle ' We need to close the file in order for SEEK to work
' Open File_name1 For Binary As #p_file_handle
' ------------------------------------------
Ltemp = 20 : Seek #p_file_handle , Ltemp ' Set file pointer at position =20
Get #p_file_handle , Btemp1 ' Get the byte at position 20, which should be =20
Print #1 , "Data_byte at pos" ; Loc(#p_file_handle) ; " is " ; Btemp1 ' Result:: Data_byte at pos20 is 20 OK only if file was closed
' --------------------------------------
Ltemp = 40 : Seek #p_file_handle , Ltemp ' Set file pointer at position =40
Btemp1 = 100 : Put #p_file_handle , Btemp1, ' put the value 100 at position 40
Btemp1 = 101 : Put #p_file_handle , Btemp1, ' put the value 101 at position 41
Btemp1 = 102 : Put #p_file_handle , Btemp1, ' put the value 102 at position 42
' --------------------------------------
Ltemp = 41 : Seek #p_file_handle , Ltemp ' Set file pointer at position =41
Get #p_file_handle , Btemp1 ' Get the byte at position 41, which should be =101
Print #1 , "Data_byte at pos" ; Loc(#p_file_handle) ; " is " ; Btemp1 ' Result Data_byte at pos41 is 101 OK always
' --------------------------------------
Ltemp = 50
Seek #p_file_handle , Ltemp ' Set file pointer at position =50
Btemp1 = 200 : Put #p_file_handle , Btemp1, ' put the value 200 at position 50
Btemp1 = 201 : Put #p_file_handle , Btemp1, ' put the value 201 at position 51
Btemp1 = 202 : Put #p_file_handle , Btemp1, ' put the value 202 at position 52
' --------------------------------------
Ltemp = 51 : Seek #p_file_handle , Ltemp ' Set file pointer at position =51
Get #p_file_handle , Btemp1 ' Get the byte at position 41, which should be =201
Print #1 , "Data_byte at pos" ; Loc(#p_file_handle) ; " is " ; Btemp1 ' Result Data_byte at pos51 is 201 OK always
' --------------------------------------
Close #p_file_handle
' --------------------------------------
End If |
|
|
Back to top |
|
|
albertsm
Joined: 09 Apr 2004 Posts: 5913 Location: Holland
|
Posted: Tue Nov 24, 2020 8:44 pm Post subject: |
|
|
but then
Code: | Ltemp = 20
Seek #p_file_handle , Ltemp ' Set file pointer at position =20
Get #p_file_handle , Btemp1 ' Get the byte at position 20, which should be =20
Print "Data_byte at pos" ; Loc(#p_file_handle) ; " is " ; Btemp1
Seek #p_file_handle , Ltemp ' Set file pointer at position =20
Get #p_file_handle , Btemp1 ' Get the byte at position 20, which should be =20
Print "Data_byte at pos" ; Loc(#p_file_handle) ; " is " ; Btemp1
|
should print the right value the second time? _________________ Mark |
|
Back to top |
|
|
hzz
Joined: 20 Feb 2007 Posts: 314
|
Posted: Tue Nov 24, 2020 8:58 pm Post subject: |
|
|
But it doesn`t.
So my statement "Subsecuent SEEK statements in the code always work correctly" is only true for the code shown.
I resist to believe that this is an error in BASCOM, I suppose many people is working with binary files and I haven't seen any mention to a problem with SEEK. Am I using correct "CONFIG_AVR-DOS.inc" and "Config_MMCSD_HC.inc" files? I took them form BASCOM samples and just changed SPIC to SPID. It is extrange that "Config_MMCSD_HC.inc" contains the line Code: | Open "SPID" For Binary As #14 | (Originally Open "SPIC" For Binary As #14). |
|
Back to top |
|
|
hzz
Joined: 20 Feb 2007 Posts: 314
|
Posted: Tue Nov 24, 2020 11:00 pm Post subject: |
|
|
The behavior can be different with different files even when all files are created with the same code (at least the first part when the file is open is always exactly the sale)
The following behavior is also extrange. This code:
Code: | If Sd_card_ready = 1 Then
' ------------------------------------------------------------------------------------
P_file_handle = Freefile()
File_name1 = "Test20.bin"
Open File_name1 For Binary As #p_file_handle
' ------------------------------------------------------------------------------------
Get #p_file_handle , Btemp1 ' Read byte at position 1
Put #p_file_handle , Btemp1 ' Dummy write byte at position 1
Close #p_file_handle ' We need to close the file in order for SEEK to work
Open File_name1 For Binary As #p_file_handle ' From now on SEEK will work
Ltemp = 1 : Seek #p_file_handle , Ltemp ' Set file pointer at position =1
Get #p_file_handle , Btemp1 ' Read byte at position 1
Get #p_file_handle , Btemp1 ' Read byte at position 2
Get #p_file_handle , Btemp1 ' Read byte at position 3
Print #1 , "A) Data_byte at pos" ; Loc(#p_file_handle) ; " is " ; Btemp1
' ------------------------------------------------------------------------------------
For Btemp1 = 1 To 255
Put #p_file_handle , Btemp1 ' Fill first 255 positions
Next Btemp1
' ------------------------------------------
Close #p_file_handle ' We need to close the file in order for SEEK to work
Open File_name1 For Binary As #p_file_handle
' ------------------------------------------
Ltemp = 20 : Seek #p_file_handle , Ltemp ' Set file pointer at position =20
Get #p_file_handle , Btemp1 ' Get the byte at position 20, which should be =20
Print #1 , "B) Data_byte at pos" ; Loc(#p_file_handle) ; " is " ; Btemp1
' --------------------------------------
Ltemp = 40 : Seek #p_file_handle , Ltemp ' Set file pointer at position =40
Btemp1 = 100 : Put #p_file_handle , Btemp1, ' put the value 100 at position 40
Btemp1 = 101 : Put #p_file_handle , Btemp1, ' put the value 101 at position 41
Btemp1 = 102 : Put #p_file_handle , Btemp1, ' put the value 102 at position 42
' --------------------------------------
Ltemp = 41 : Seek #p_file_handle , Ltemp ' Set file pointer at position =41
Get #p_file_handle , Btemp1 ' Get the byte at position 41, which should be =101
Print #1 , "C) Data_byte at pos" ; Loc(#p_file_handle) ; " is " ; Btemp1
' --------------------------------------
Ltemp = 50
Seek #p_file_handle , Ltemp ' Set file pointer at position =50
Btemp1 = 200 : Put #p_file_handle , Btemp1, ' put the value 200 at position 50
Btemp1 = 201 : Put #p_file_handle , Btemp1, ' put the value 201 at position 51
Btemp1 = 202 : Put #p_file_handle , Btemp1, ' put the value 202 at position 52
' --------------------------------------
Ltemp = 51 : Seek #p_file_handle , Ltemp ' Set file pointer at position =51
Get #p_file_handle , Btemp1 ' Get the byte at position 41, which should be =201
Print #1 , "D) Data_byte at pos" ; Loc(#p_file_handle) ; " is " ; Btemp1
' --------------------------------------
Close #p_file_handle
' --------------------------------------
End If |
Prints the following when the file does not exist previously in the SD Card (first execution):
Quote: | Test SD Card
SD Card OK_____
A) Data_byte at pos1 is 0
B) Data_byte at pos20 is 19
C) Data_byte at pos41 is 101
D) Data_byte at pos51 is 201 |
The second and next executions prints this:
Quote: | Test SD Card
SD Card OK_____
A) Data_byte at pos3 is 2
B) Data_byte at pos20 is 17
C) Data_byte at pos41 is 101
D) Data_byte at pos51 is 201
|
If I remove
Code: | Get #p_file_handle , Btemp1 |
the line just before printing A)
Then the value printed in B) canges:
Quote: | Test SD Card
SD Card OK_____
A) Data_byte at pos2 is 0
B) Data_byte at pos20 is 18
C) Data_byte at pos41 is 101
D) Data_byte at pos51 is 201
|
|
|
Back to top |
|
|
albertsm
Joined: 09 Apr 2004 Posts: 5913 Location: Holland
|
Posted: Wed Nov 25, 2020 9:49 am Post subject: |
|
|
- you should watch the GBDOSERROR variable.
- using get/put will change the location so you get a difference
- again, always use the same conditions. so explicit kill a file when it exists. or simply open in binary mode, write data, and close the file.
- avr-dos is used a lot but it does not mean there could not be an error. maybe users do not use seek. or they close the file. i think that there was some discussion about seek some time ago.
- i could not find an obvious error in the lib but something is not right. it takes time to check it. so i will not update this topic any more until i have figured it out. _________________ Mark |
|
Back to top |
|
|
hzz
Joined: 20 Feb 2007 Posts: 314
|
Posted: Wed Nov 25, 2020 4:51 pm Post subject: |
|
|
Thanks Mark
Quote: | - you should watch the GBDOSERROR variable. |
OK, I have printed it and is always 0, even when Seek doesn`t work well
Quote: | - using get/put will change the location so you get a difference |
But there is a SEEK statement setting the file pointer after the GET's so they should not affect the pointer after the SEEK
Quote: | - again, always use the same conditions. so explicit kill a file when it exists. or simply open in binary mode, write data, and close the file. |
OK, but I have two examples when a SEEK works when the file exists and it doesn't when the file is newly created using the same code
Quote: | - avr-dos is used a lot but it does not mean there could not be an error. maybe users do not use seek. or they close the file. i think that there was some discussion about seek some time ago.
- i could not find an obvious error in the lib but something is not right. it takes time to check it. so i will not update this topic any more until i have figured it out. |
OK. I will stop testing until I have more news |
|
Back to top |
|
|
albertsm
Joined: 09 Apr 2004 Posts: 5913 Location: Holland
|
Posted: Tue Dec 01, 2020 9:27 pm Post subject: |
|
|
it took some time but the problem was found and fixed.
the end of file position was not updated with seek.
until the update is published the work around is to close the file and re open it. _________________ Mark |
|
Back to top |
|
|
hzz
Joined: 20 Feb 2007 Posts: 314
|
Posted: Mon Jan 25, 2021 3:44 pm Post subject: |
|
|
Please notice that GET does not work either. In some cases the bytes read form a file are wrong. You need to close the file and open it again before reading.
This code
Code: | For Ltemp = Reg_ini_pos To Reg_end_pos
Get #p_file_handle , Btemp , Ltemp
Ltemp2 = Loc(#p_file_handle)
Print #1 , Ltemp2 ; "=" ; Ltemp ; ":" ; Btemp
Next Ltemp
Print #1 ,
Close #p_file_handle : Open P_file_name For Binary As #p_file_handle ' Sin cerrar y volver a abrior el fichero no funciona la lectura de registros
For Ltemp = Reg_ini_pos To Reg_end_pos
Get #p_file_handle , Btemp , Ltemp
Ltemp2 = Loc(#p_file_handle)
Print #1 , Ltemp2 ; "=" ; Ltemp ; ":" ; Btemp
Next Ltemp
|
Generates this output (correct)
11=11:3
12=12:24
13=13:0
14=14:1
15=15:2
16=16:3
17=17:4
18=18:5
19=19:6
20=20:7
21=21:8
22=22:9
23=23:10
24=24:101
25=25:102
26=26:103
27=27:104
28=28:105
29=29:106
30=30:107
31=31:108
32=32:109
33=33:110
34=34:113
11=11:3
12=12:24
13=13:0
14=14:1
15=15:2
16=16:3
17=17:4
18=18:5
19=19:6
20=20:7
21=21:8
22=22:9
23=23:10
24=24:101
25=25:102
26=26:103
27=27:104
28=28:105
29=29:106
30=30:107
31=31:108
32=32:109
33=33:110
34=34:113
This code
Code: | For Ltemp = Reg_ini_pos To Reg_end_pos
Get #p_file_handle , Btemp , Ltemp
Ltemp2 = Loc(#p_file_handle)
Print #1 , Ltemp2 ; "=" ; Ltemp ; ":" ; Btemp
Next Ltemp
Print #1 ,
' Close #p_file_handle : Open P_file_name For Binary As #p_file_handle ' Sin cerrar y volver a abrior el fichero no funciona la lectura de registros
For Ltemp = Reg_ini_pos To Reg_end_pos
Get #p_file_handle , Btemp , Ltemp
Ltemp2 = Loc(#p_file_handle)
Print #1 , Ltemp2 ; "=" ; Ltemp ; ":" ; Btemp
Next Ltemp
|
Generates this output (not correct)
11=11:3
12=12:24
13=13:0
14=14:1
15=15:2
16=16:3
17=17:4
18=18:5
19=19:6
20=20:7
21=21:8
22=22:9
23=23:10
24=24:101
25=25:102
26=26:103
27=27:104
28=28:105
29=29:106
30=30:107
31=31:108
32=32:109
33=33:110
34=34:113
10=11:110
11=12:110
12=13:110
13=14:110
14=15:110
15=16:110
16=17:110
17=18:110
18=19:110
19=20:110
20=21:110
21=22:110
22=23:110
23=24:110
24=25:110
25=26:110
26=27:110
27=28:110
28=29:110
29=30:110
30=31:110
31=32:110
32=33:110
33=34:110 |
|
Back to top |
|
|
albertsm
Joined: 09 Apr 2004 Posts: 5913 Location: Holland
|
Posted: Tue Jan 26, 2021 4:23 pm Post subject: |
|
|
sure but that is caused by the same issue so should be resolved as well. _________________ Mark |
|
Back to top |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You cannot download files in this forum
|
|