LOC: Difference between revisions

From QB64 Phoenix Edition Wiki
Jump to navigation Jump to search
(Created page with "The LOC function returns the status of a serial (COM) port received buffer or the current byte position in an open file. {{PageSyntax}} : {{Parameter|bytes%}} = LOC({{Parameter|fileOrPortNumber%}}) * {{Parameter|fileOrPortNumber%}} is the number used in the port OPEN AS statement. * Returns 0 if the buffer is empty. Any value above 0 indicates the COM port has received data. * Use it in conjunction with INPUT$ to get the data bytes received. * Can also be...")
Tag: visualeditor-switched
 
No edit summary
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
The [[LOC]] function returns the status of a serial (COM) port received buffer or the current byte position in an open file.
The '''LOC''' function returns the status of a serial (COM) port received buffer or the last read/written byte or record position in an open file.




{{PageSyntax}}
{{PageSyntax}}
: {{Parameter|bytes%}} = LOC({{Parameter|fileOrPortNumber%}})
: {{Parameter|bytes%}} = [[LOC]]({{Parameter|fileOrPortNumber%}})




* {{Parameter|fileOrPortNumber%}} is the number used in the port [[OPEN]] AS statement.
{{PageParameters}}
* {{Parameter|fileOrPortNumber%}} is the number used in the port or file [[OPEN]] AS statement.
* Returns 0 if the buffer is empty. Any value above 0 indicates the COM port has received data.
* Returns 0 if the buffer is empty. Any value above 0 indicates the COM port has received data.
* Use it in conjunction with [[INPUT$]] to get the data bytes received.
* Use it in conjunction with [[INPUT$]] to get the data bytes received.
* Can also be used to read the current position in a file routine. See [[SEEK]].
* Can also be used to get the last read/written byte or record position in a file. See also [[SEEK (function)|SEEK]].
 
=== Notes ===
* Don't confuse the [[LOC]] position with the [[SEEK (function)|SEEK]] position !!
** '''LOC''' is the {{Text|last|red}} read or written byte or record prosition.
** '''SEEK''' is the byte or record prosition to read or write {{Text|next|red}}.




{{PageExamples}}
{{PageExamples}}
''Example:'' Reading and writing from a COM port opened in Basic.
;Example 1:Reading and writing from a COM port opened in Basic.
{{CodeStart}} '' ''
{{CodeStart}}
{{Cl|OPEN}} "{{Cl|OPEN_COM|COM}}1: 9600,N,8,1,OP0" {{Cl|FOR (file statement)|FOR}} {{Cl|RANDOM}} {{Cl|AS}} #1 {{Cl|LEN}} = 2048 ' random mode = input and output
{{Cl|OPEN}} "{{Cl|OPEN_COM|COM}}1: 9600,N,8,1,OP0" {{Cl|FOR (file statement)|FOR}} {{Cl|RANDOM}} {{Cl|AS}} #1 {{Cl|LEN}} = 2048 ' random mode = input and output
   {{Cl|DO}}: t$ = {{Cl|INKEY$}} ' get any transmit keypresses from user
   {{Cl|DO}}: t$ = {{Cl|INKEY$}} ' get any transmit keypresses from user
Line 22: Line 28:
       r$ = {{Cl|INPUT$}}(bytes%, 1)          ' get bytes in the receive buffer
       r$ = {{Cl|INPUT$}}(bytes%, 1)          ' get bytes in the receive buffer
       {{Cl|PRINT}} r$; ' print byte strings consecutively to screen"
       {{Cl|PRINT}} r$; ' print byte strings consecutively to screen"
     {{Cl|END IF}}  
     {{Cl|END IF}}
   {{Cl|LOOP}} {{Cl|UNTIL}} t$ = {{Cl|CHR$}}(27) 'escape key exit
   {{Cl|LOOP}} {{Cl|UNTIL}} t$ = {{Cl|CHR$}}(27) 'escape key exit
{{Cl|CLOSE}} # '' ''
{{Cl|CLOSE}} #
{{CodeEnd}}
{{CodeEnd}}
----
;Example 2:Demonstrate the difference between '''LOC''' and '''SEEK''' positions in a file.
{{CodeStart}}
OPEN "readme.md" FOR BINARY AS #1
PRINT LOC(1) 'LOC returns 0, as we didn't read something yet
PRINT SEEK(1) 'SEEK otherwise returns 1, as it's the first byte to read
GET #1, , a& 'now let's read a LONG (4 bytes)
PRINT LOC(1) 'now LOC returns 4, the last read byte
PRINT SEEK(1) 'and SEEK returns 5 now, the next byte to read
CLOSE #1
END
{{CodeEnd}}
{{OutputStart}}
0
1
4
5
{{OutputEnd}}




{{PageSeeAlso}}
{{PageSeeAlso}}
* [[PRINT]], [[OPEN COM]], [[PRINT (file statement)]]
* [[PRINT]], [[OPEN COM]], [[PRINT (file statement)]]
* [[SEEK]]
* [[SEEK]], [[SEEK (function)]]
 


{{PageNavigation}}
{{PageNavigation}}

Latest revision as of 00:10, 25 February 2023

The LOC function returns the status of a serial (COM) port received buffer or the last read/written byte or record position in an open file.


Syntax

bytes% = LOC(fileOrPortNumber%)


Parameters

  • fileOrPortNumber% is the number used in the port or file OPEN AS statement.
  • Returns 0 if the buffer is empty. Any value above 0 indicates the COM port has received data.
  • Use it in conjunction with INPUT$ to get the data bytes received.
  • Can also be used to get the last read/written byte or record position in a file. See also SEEK.

Notes

  • Don't confuse the LOC position with the SEEK position !!
    • LOC is the last read or written byte or record prosition.
    • SEEK is the byte or record prosition to read or write next.


Examples

Example 1
Reading and writing from a COM port opened in Basic.
OPEN "COM1: 9600,N,8,1,OP0" FOR RANDOM AS #1 LEN = 2048 ' random mode = input and output
  DO: t$ = INKEY$ ' get any transmit keypresses from user
    IF LEN(t$) THEN PRINT #1, t$ ' send keyboard byte to transmit buffer
    bytes% = LOC(1) ' bytes in buffer
    IF bytes% THEN ' check receive buffer for data"
      r$ = INPUT$(bytes%, 1)          ' get bytes in the receive buffer
      PRINT r$; ' print byte strings consecutively to screen"
    END IF
  LOOP UNTIL t$ = CHR$(27) 'escape key exit
CLOSE #

Example 2
Demonstrate the difference between LOC and SEEK positions in a file.
OPEN "readme.md" FOR BINARY AS #1

PRINT LOC(1) 'LOC returns 0, as we didn't read something yet
PRINT SEEK(1) 'SEEK otherwise returns 1, as it's the first byte to read

GET #1, , a& 'now let's read a LONG (4 bytes)

PRINT LOC(1) 'now LOC returns 4, the last read byte
PRINT SEEK(1) 'and SEEK returns 5 now, the next byte to read

CLOSE #1
END
0
1
4
5


See also



Navigation:
Main Page with Articles and Tutorials
Keyword Reference - Alphabetical
Keyword Reference - By usage
Report a broken link