LOC

From QB64 Phoenix Edition Wiki
Revision as of 00:10, 25 February 2023 by RhoSigma (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

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