Does the code example for LOC work?

Forum for discussion about the documentation project.
sancho3
Posts: 256
Joined: Sep 30, 2017 3:22

Does the code example for LOC work?

Postby sancho3 » Nov 11, 2017 22:23

Another thread I was looking at had me look up the LOC() command in help.
I can't test the example given as it returns an error "unable to open serial port".
The following is the code:

Code: Select all

Dim b As String

If Open Com ("com1:9600,n,8,1,cs,rs,ds,bin" For Binary As #1) <> 0 Then
  Print "unable to open serial port"
  End
End If

Print "Sending command: AT"

Print #1, "AT" + Chr(13, 10);

Sleep 500,1

Print "Response:"

While( LOC(1) > 0 )
  b = Input(LOC(1), 1)
  Print b;
Wend

Close #1

I tried the while loop with a file and it doesn't work at all. LOC returns 0 so it can't start the while loop.

Code: Select all

dim as string s = "hello help" + chr(13) + "hell" + chr(13) + "test"
dim as string buffer
open "mooose.txt" for output as #1
   put #1,,s
   print loc(1)
   While( LOC(1) > 0 )
      buffer = Input(LOC(1), 1)
      Print buffer;
   Wend
close #1
sleep


Is there a difference when the data coming from the serial port?
fxm
Posts: 8051
Joined: Apr 22, 2009 12:46
Location: Paris suburb, FRANCE

Re: Does the code example for LOC work?

Postby fxm » Nov 12, 2017 9:19

I too can not test the serial port on my professional PC.

By reading the help, one can understand that LOC is only useful for a RANDOM/BINARY opening mode (very low utility for a INPUT|OUTPUT|APPEND opening mode).
On the other hand, moreover it makes it possible to re-read a file just written without having to close it in the meantime (by testing LOC instead of EOF).
Example:

Code: Select all

dim as string * 16 s = "hello help!"
dim as string * 16 buffer

kill "testLOC.txt"

open "testLOC.txt" for binary as #1
   put #1, , s
   print loc(1)
   put #1, , s
   print loc(1)
   put #1, , s
   print loc(1)

   print
   seek #1, 1

   while loc(1) < lof(1)
      buffer = input(len(buffer), #1)
      print buffer,
      print loc(1)
   Wend
close #1

sleep
Last edited by fxm on Nov 12, 2017 14:50, edited 1 time in total.
fxm
Posts: 8051
Joined: Apr 22, 2009 12:46
Location: Paris suburb, FRANCE

Re: Does the code example for LOC work?

Postby fxm » Nov 12, 2017 9:56

KeyPgLoc → fxm [Added a explanation on record numbering]
fxm
Posts: 8051
Joined: Apr 22, 2009 12:46
Location: Paris suburb, FRANCE

Re: Does the code example for LOC work?

Postby fxm » Nov 12, 2017 14:56

fxm wrote:On the other hand, moreover it makes it possible to re-read a file just written without having to close it in the meantime (by testing LOC instead of EOF).

Same code except by testing EOF instead of LOC does not work:

Code: Select all

dim as string * 16 s = "hello help!"
dim as string * 16 buffer

kill "testLOC.txt"

open "testLOC.txt" for binary as #1
   put #1, , s
   print loc(1)
   put #1, , s
   print loc(1)
   put #1, , s
   print loc(1)

   print
   seek #1, 1

   while not eof(1)  '' while loc(1) < lof(1)
      buffer = input(len(buffer), #1)
      print buffer,
      print loc(1)
   Wend
close #1

sleep
sancho3
Posts: 256
Joined: Sep 30, 2017 3:22

Re: Does the code example for LOC work?

Postby sancho3 » Nov 12, 2017 17:44

I understand now that a file open for output counts loc() positions in steps of 128.
So until I have written 128 bytes loc() returns 0.
Thanks FXM.

Return to “Documentation”

Who is online

Users browsing this forum: No registered users and 1 guest