Difference between revisions of "LOC"

From QB64 Wiki
Jump to navigation Jump to search
imported>Clippy
imported>SMcNeill
(style guidelines)
 
(12 intermediate revisions by 2 users not shown)
Line 1: Line 1:
The '''LOC''' function returns the status of a Serial(COM) port receive buffer or current position in a file.
+
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%}})
  
''Syntax:'' bytes% = LOC(open_number)
 
  
 
+
* {{Parameter|fileOrPortNumber%}} is the number used in the port [[OPEN]] AS statement.
* The parameter used 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.
* Returns 0 if the buffer is empty. Any value above 0 indicates COM 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 for the current position in a file routine. See: [[SEEK]]
+
* Can also be used to read the current position in a file routine. See [[SEEK]].
  
  
 +
{{PageExamples}}
 
''Example:'' Reading and writing from a COM port opened in Basic.
 
''Example:'' Reading and writing from a COM port opened in Basic.
 +
{{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|DO}}: t$ = {{Cl|INKEY$}} ' get any transmit keypresses from user
 +
    {{Cl|IF}} {{Cl|LEN}}(t$) {{Cl|THEN}} {{Cl|PRINT (file statement)|PRINT}} #1, t$ ' send keyboard byte to transmit buffer
 +
    bytes% = {{Cl|LOC}}(1) ' bytes in buffer
 +
    {{Cl|IF}} bytes% {{Cl|THEN}} ' check receive buffer for data"
 +
      r$ = {{Cl|INPUT$}}(bytes%, 1)          ' get bytes in the receive buffer
 +
      {{Cl|PRINT}} r$; ' print byte strings consecutively to screen"
 +
    {{Cl|END IF}}   
 +
  {{Cl|LOOP}} {{Cl|UNTIL}} t$ = {{Cl|CHR$}}(27) 'escape key exit
 +
{{Cl|CLOSE}} # '' ''
 +
{{CodeEnd}}
  
  
:OPEN "COM1: 9600,N,8,1," FOR RANDOM AS #1 LEN = 2048 ' random mode = input and output
+
{{PageSeeAlso}}
::DO: t$ = INKEY$ ' get any transmit keypresses from user
+
* [[PRINT]], [[OPEN COM]], [[PRINT (file statement)]]
:::IF [[LEN]](t$) THEN PRINT #1, t$ ' send byte to UART transmit buffer"
+
* [[SEEK]]
:::bytes% = LOC(1) ' bytes in buffer
 
:::IF bytes% THEN ' check UART receive buffer for data"
 
::::r$ = INPUT$(bytes%, 1)          ' get bytes in the receive buffer"
 
::::PRINT r$; ' print characters consecutively to screen"
 
:::END IF   
 
::LOOP UNTIL t$ = CHR$(27) ' Escape exit and CLOSE #1"
 
 
 
 
 
''See also:'' [[PRINT]], [[PRINT (file statement)]]
 
 
 
 
 
 
 
==Navigation:==
 
 
 
[[Keyword_Reference_-_Alphabetical|Go to Keyword Reference - Alphabetical]]
 
  
[[Keyword Reference - By usage|Go to Keyword Reference - By usage]]
+
{{PageNavigation}}

Latest revision as of 14:04, 9 October 2017

The LOC function returns the status of a serial (COM) port received buffer or the current byte position in an open file.


Syntax

bytes% = LOC(fileOrPortNumber%)


  • 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 used to read the current position in a file routine. See SEEK.


Examples

Example: 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 #


See also



Navigation:
Go to Keyword Reference - Alphabetical
Go to Keyword Reference - By usage
Go to Main WIKI Page