Difference between revisions of "OPEN COM"

From QB64 Wiki
Jump to navigation Jump to search
imported>Clippy
m
m (Text replacement - "{{Parameters}}" to "{{PageParameters}}")
 
(49 intermediate revisions by 4 users not shown)
Line 1: Line 1:
The '''OPEN COM''' statement is used to access a comuter's Serial port COM1 or COM2  address using an OPEN statement.
The [[OPEN COM]] statement is used to access a computer's serial port COM.




{{PageSyntax}}
: '''OPEN''' "COMn: ''Speed'', ''Parity'', ''Bits'', ''Stopbit'', [''Options'']" [FOR {[[RANDOM]]|[[BINARY]]|[[OUTPUT]]|[[INPUT (file mode)|INPUT]]}] AS #''P'' [LEN = {{Parameter|byteSize}}]


:::''Syntax:'' [[OPEN]] "COMn: Speed, Parity, Bits, Stopbit, '''Options'''" FOR mode AS #f LEN = bytesize


 
{{PageParameters}}
* [[COM]] can only be 1 or 2 as [[OPEN]] cannot open other COM port numbers.(See Windows System Components list)
* ''Speed'' (baud rate): 50, 150, 300, 600, 1200, 1800, 2400, '''9600''' (QBasic's maximum), 19200 or '''115200''' ('''QB64''''s maximum).
* Four commas are required after the Speed, Parity, Bits, and Stopbit, even if no other Options are used.
* ''Parity'': '''N''' (none), E (even), O (odd), S (space) or M (mark). Note: If 8 bits, use parity N for numerical data.
* Speed(baud rate): 75, 110, 150, 300, 600, 1200, 1800, 2400 or 9600 maximum.
* ''Bits'' = number of bits/byte: Valid numbers: 5, 6, 7 or '''8'''
* Parity: N (none), E (even), O (odd), S (space) or M (mark). Note: If 8 bits use parity N for numerical data!
* ''Stopbit'' = number of stop bits: Valid numbers: '''1''', 1.5 or 2
* Bits = number of bits/byte: Valid numbers: 5, 6, 7 or 8
* Optional COM port ''Options'' (separated by commas):
* Stopbit = number of stop bits: Valid numbers: 1, 1.5 or 2
::* ASC : [[ASCII]] byte mode. End of line = [[CHR$]](13). End of file = CHR$(26)
* Other [[OPEN]] options are optional and in any order separated by commas within the OPEN [[STRING]] command.  
::* BIN : [[Binary]] byte mode. Default mode if ASC is not used(option not required).
:: ''Below ms is the timeout in milliseconds 1 to 65535. Zero ignores a timeout. Default without ms = 1000 :''
::* CD[ms] : Time until timeout of DCD (Carrier Detect) line in. CD0 ignores timeouts.
::* CS[ms] : Time until timeout of CTS (Clear to Send) line in. CS0 ignores timeouts.
::* DS[ms] : Time until timeout of DSR (Data Set Ready) line in. DS0 ignores timeouts.
::* OP[ms] : Time until data lines become active. If timeout then OPEN fails, OP0 ignores timeouts.
::* RB[b] : Size of receive buffer in bytes when used. Default when not used = 512 bytes
::* TB[b] : Size of transmit buffer in bytes when used. Default when not used = 512 bytes
::* RS  : Supress detection of Request to Send (RTS) line.


::::::::'''Additional Options'''


:: * ASC : [[ASCII]] byte mode. End of line = [[CHR$]](13). End of file = CHR$(26)
{{PageDescription}}
:: * BIN : [[Binary]] byte mode. Default mode if ASC is not used(option not required).
* '''If any optional CD, CS, DS or OP timeouts occur the OPEN will fail or port access will stop. Try 0 to ignore.'''  
:: Below m is the timeout in milliseconds 1 to 65535. 0 is ignore timeout. Default m = 1000 :
* '''QB64''' can open any COM''n'' port number from 1 to 9.
::: CDm : Time until timeout of DCD (Carrier Detect) line in. CD0 ignores timeouts.
* See Windows System '''Device Manager''' for COM port numbers and port addresses &H3F8, &H2F8, &H3E8 and &H2E8.
::: CSm : Time until timeout of CTS (Clear to Send) line in. CS0 ignores timeouts.
* Four commas are required after the Speed, Parity, Bits, and Stopbit, even if none of the Options are used.
::: DSm : Time until timeout of DSR (Data Set Ready) line in. DS0 ignores timeouts.
* Other [[OPEN]] ''options'' are optional and in any order separated by commas within the OPEN command [[STRING|string]].(See list below)
::: OPm : Time until data lines become active. If timeout then OPEN fails! OP0 ignores timeouts.
* The optional FOR access ''mode'' can be [[OUTPUT]], [[INPUT (file mode)|INPUT]] or [[RANDOM]] (default mode when no FOR statement is used).  
:::: ''If any timeouts occur the OPEN will fail or port access will stop!''
* '''Currently, QB64 only supports [[OPEN]] FOR [[RANDOM]] access using the [[GET]]/[[PUT]] commands in [[BINARY|BIN]] mode.'''
* '''Use the BIN option listed below for [[BINARY]] byte mode port access.'''
* The [[LEN]] statement is also optional. The default record size is 512 bytes when not used.
* Use the [[LOC]](portnumber) function to determine that there is data in the receive buffer when the value is greater than 0.
* OPEN AS number can use a [[FREEFILE]] value. Numbers used by files already open '''cannot''' be used by OPEN COM.
* [[Keywords_currently_not_supported_by_QB64#Keywords_Not_Supported_in_Linux_or_MAC_OSX_versions|Keyword Not Supported in Linux or MAC versions]]


::: RBs : Size of receive buffer when used. Default when not used = 512 bytes
::: TBs : Size of transmit buffer when used. Default when not used = 512 bytes
::: RS  : Supress detection of RTS (Ready to Send) line. No timeout.


{{PageExamples}}
''Example 1:'' Checking to see if a COM port exists. If the port does not exist QBasic will cause a Windows access error.
{{CodeStart}} '' ''
{{Cl|ON ERROR}} {{Cl|GOTO}} Handler
FF = {{Cl|FREEFILE}}
comPort$ = "COM1:"                        'try a COM port number that does not exist
{{Cl|CONST}} comMode$ = "9600,N,8,1,CS0,DS0"      'Use 0 to avoid timeouts
{{Cl|OPEN}} comPort$ + comMode$ {{Cl|FOR...NEXT|FOR}} {{Cl|RANDOM}} {{Cl|AS}} FF
{{Cl|IF...THEN|IF}} errnum = 0 {{Cl|THEN}} {{Cl|PRINT}} "COM exists!


*[[INPUT (file mode)|INPUT]] mode can use [[INPUT (file statement)|INPUT #]] or [[INPUT$]]. [[OUTPUT]] mode can use [[PRINT (file statement)|PRINT #]] or [[PRINT USING (file statement)|PRINT #, USING]]. 
K$ = {{Cl|INPUT$}}(1)  
*[[RANDOM]] or [[BINARY]] modes can use [[INPUT (file statement)|INPUT #]], [[INPUT$]], [[PRINT (file statement)|PRINT #]], [[GET]] or [[PUT]]. Binary cannot set the buffer size!
{{Cl|END}}
* Note: If random use [[LEN]] = to set size of random buffer(default LEN = 128). Use multiples of 128 for larger buffers.
* OPEN AS number can use a [[FREEFILE]] value. Numbers used by files already open '''cannot''' be used by the OPEN COM statement!
* NOTE: NT or XP computers need a program like [http://www.beyondlogic.org/porttalk/porttalk.htm PortTalk] to access COM or other ports!


Handler:
errnum = {{Cl|ERR}}
{{Cl|PRINT}} "Error:"; errnum
{{Cl|RESUME}} {{Cl|NEXT}}
{{CodeEnd}}
: ''Explanation:'' QB64 may create error 68 if COM is not found. Use a zero CD, CS, DS or OP timeout value to avoid COM timeouts.




''Example 2:'' Opening a COM port with the BIN, CS0 and DS0 options in '''QB64'''.
{{CodeStart}} '' ''
{{Cl|DIM}} bytestr {{Cl|AS}} {{Cl|STRING}} * 1  'one byte transfers
{{Cl|INPUT}} "{{Cl|COM}} port number #", port$  'any COM port number available


''Example:'' Opening COM port 1 with the OP0 no line timeout option and setting the buffer sizes using [[LEN]] =.
{{Cl|OPEN}} "{{Cl|COM}}" + port$ + ":9600,N,8,1,BIN,CS0,DS0" {{Cl|FOR (file statement)|FOR}} {{Cl|RANDOM}} {{Cl|AS}} #1
{{Cl|DO}} 'main loop
    'receive data in buffer when LOC > 0
    {{Cl|IF}} {{Cl|LOC}}(1) {{Cl|THEN}}
      {{Cl|GET}} #1, , bytestr
      {{Cl|PRINT}} "[" + bytestr + "]";
    {{Cl|END IF}}
    'transmit (send)
    k$ = {{Cl|INKEY$}} 
    {{Cl|IF}} {{Cl|LEN}}(k$) = 1 {{Cl|THEN}}
      k = {{Cl|ASC}}(k$)
      {{Cl|IF}} k >= 32 {{Cl|THEN}}    'ignore control key codes
          {{Cl|PRINT}} ">" + k$ + "<";
          bytestr = k$: {{Cl|PUT}} #1, , bytestr
      {{Cl|END IF}}
    {{Cl|END IF}}
{{Cl|LOOP}} {{Cl|UNTIL}} k$ = {{Cl|CHR$}}(27)
{{Cl|CLOSE}} #1: {{Cl|PRINT}} "Finished!" '' ''
{{CodeEnd}}


::OPEN "COM1: 9600, N, 8, 1, OP0" FOR RANDOM AS #1 LEN = 2048 ' input and output buffersize


''Example 3:'' Sending string data from one COM port to another requires predefined length strings:
{{CodeStart}} '' ''
{{Cl|DIM}} {{Cl|SHARED}} ByteIn {{Cl|AS}} {{Cl|STRING}} * 1 'One byte transfers
{{Cl|DIM}} {{Cl|SHARED}} Byte4 {{Cl|AS}} {{Cl|STRING}} * 4 'Four byte transfers


Byte4 = {{Cl|CHR$}}(254) + {{Cl|CHR$}}(175) + {{Cl|CHR$}}(0) + {{Cl|CHR$}}(3) 'Command code to query all 4 banks of switch input board.


''See also:'' [[INPUT$]], [[LOC]], [[INKEY$]], [[PRINT (file statement)|PRINT #]], [[PRINT USING (file statement)| PRINT #, USING]], [[OPEN]]
{{Cl|OPEN}} "COM1:115200,N,8,1,BIN,CS0,DS0" {{Cl|FOR...NEXT|FOR}} {{Cl|RANDOM}} {{Cl|AS}} #1 'Open port used to send commands.
{{Cl|OPEN}} "COM2:115200,N,8,1,BIN,CS0,DS0" {{Cl|FOR...NEXT|FOR}} {{Cl|RANDOM}} {{Cl|AS}} #2 'Open port used to receive commands.


{{Cl|PUT}} #1, , Byte4 'Send the 4 byte command.


Start# = {{Cl|TIMER}}
{{Cl|DO...LOOP|DO}} {{Cl|UNTIL}} {{Cl|LOC}}(2) <> 0 'Check if there is data received at com2
    {{Cl|IF...THEN|IF}} {{Cl|TIMER}} - Start# > .5 {{Cl|THEN}} {{Cl|EXIT DO}} 'Exit loop if no data arrives within .5 seconds.
{{Cl|LOOP}}


{{Cl|IF...THEN|IF}} {{Cl|LOC}}(2) = 0 {{Cl|THEN}} 'If no data was received.....
    {{Cl|PRINT}} "No data received from COM port."
    {{Cl|END}}
{{Cl|END IF}}


==Navigation:==
{{Cl|PRINT}} "Received from COM2:";


[[Keyword_Reference_-_Alphabetical|Go to Keyword Reference - Alphabetical]]
{{Cl|DO...LOOP|DO}} {{Cl|UNTIL}} {{Cl|LOC}}(2) = 0 'Read data from COM2 until there is no more data.
    {{Cl|GET}} #2, , ByteIn
    {{Cl|PRINT}} {{Cl|ASC}}(ByteIn);
{{Cl|LOOP}}
{{Cl|END}} '' ''
{{CodeEnd}}{{small|Code courtesy of forum member Hydrofoiler}}


[[Keyword Reference - By usage|Go to Keyword Reference - By usage]]
 
{{PageSeeAlso}}
* [[BINARY]], [[RANDOM]]
* [[INPUT$]], [[PRINT (file statement)|PRINT #]]
* [[LOC]], [[INKEY$]], [[OPEN]]
* [[GET|GET #]], [[PUT|PUT #]]
* [[Port Access Libraries]] {{text|(Includes full LPT and COM port descriptions with downloadable DLL library)}}
* [[Windows_Libraries#Windows_Ports|Enumerating Windows Ports]]
 
 
{{PageNavigation}}

Latest revision as of 12:44, 17 February 2021

The OPEN COM statement is used to access a computer's serial port COM.


Syntax

OPEN "COMn: Speed, Parity, Bits, Stopbit, [Options]" [FOR {RANDOM|BINARY|OUTPUT|INPUT}] AS #P [LEN = byteSize]


Parameters

  • Speed (baud rate): 50, 150, 300, 600, 1200, 1800, 2400, 9600 (QBasic's maximum), 19200 or 115200 (QB64's maximum).
  • Parity: N (none), E (even), O (odd), S (space) or M (mark). Note: If 8 bits, use parity N for numerical data.
  • Bits = number of bits/byte: Valid numbers: 5, 6, 7 or 8
  • Stopbit = number of stop bits: Valid numbers: 1, 1.5 or 2
  • Optional COM port Options (separated by commas):
  • ASC : ASCII byte mode. End of line = CHR$(13). End of file = CHR$(26)
  • BIN : Binary byte mode. Default mode if ASC is not used(option not required).
Below ms is the timeout in milliseconds 1 to 65535. Zero ignores a timeout. Default without ms = 1000 :
  • CD[ms] : Time until timeout of DCD (Carrier Detect) line in. CD0 ignores timeouts.
  • CS[ms] : Time until timeout of CTS (Clear to Send) line in. CS0 ignores timeouts.
  • DS[ms] : Time until timeout of DSR (Data Set Ready) line in. DS0 ignores timeouts.
  • OP[ms] : Time until data lines become active. If timeout then OPEN fails, OP0 ignores timeouts.
  • RB[b] : Size of receive buffer in bytes when used. Default when not used = 512 bytes
  • TB[b] : Size of transmit buffer in bytes when used. Default when not used = 512 bytes
  • RS  : Supress detection of Request to Send (RTS) line.


Description

  • If any optional CD, CS, DS or OP timeouts occur the OPEN will fail or port access will stop. Try 0 to ignore.
  • QB64 can open any COMn port number from 1 to 9.
  • See Windows System Device Manager for COM port numbers and port addresses &H3F8, &H2F8, &H3E8 and &H2E8.
  • Four commas are required after the Speed, Parity, Bits, and Stopbit, even if none of the Options are used.
  • Other OPEN options are optional and in any order separated by commas within the OPEN command string.(See list below)
  • The optional FOR access mode can be OUTPUT, INPUT or RANDOM (default mode when no FOR statement is used).
  • Currently, QB64 only supports OPEN FOR RANDOM access using the GET/PUT commands in BIN mode.
  • Use the BIN option listed below for BINARY byte mode port access.
  • The LEN statement is also optional. The default record size is 512 bytes when not used.
  • Use the LOC(portnumber) function to determine that there is data in the receive buffer when the value is greater than 0.
  • OPEN AS number can use a FREEFILE value. Numbers used by files already open cannot be used by OPEN COM.
  • Keyword Not Supported in Linux or MAC versions


Examples

Example 1: Checking to see if a COM port exists. If the port does not exist QBasic will cause a Windows access error.

ON ERROR GOTO Handler FF = FREEFILE comPort$ = "COM1:" 'try a COM port number that does not exist CONST comMode$ = "9600,N,8,1,CS0,DS0" 'Use 0 to avoid timeouts OPEN comPort$ + comMode$ FOR RANDOM AS FF IF errnum = 0 THEN PRINT "COM exists! K$ = INPUT$(1) END Handler: errnum = ERR PRINT "Error:"; errnum RESUME NEXT

Explanation: QB64 may create error 68 if COM is not found. Use a zero CD, CS, DS or OP timeout value to avoid COM timeouts.


Example 2: Opening a COM port with the BIN, CS0 and DS0 options in QB64.

DIM bytestr AS STRING * 1 'one byte transfers INPUT "COM port number #", port$ 'any COM port number available OPEN "COM" + port$ + ":9600,N,8,1,BIN,CS0,DS0" FOR RANDOM AS #1 DO 'main loop 'receive data in buffer when LOC > 0 IF LOC(1) THEN GET #1, , bytestr PRINT "[" + bytestr + "]"; END IF 'transmit (send) k$ = INKEY$ IF LEN(k$) = 1 THEN k = ASC(k$) IF k >= 32 THEN 'ignore control key codes PRINT ">" + k$ + "<"; bytestr = k$: PUT #1, , bytestr END IF END IF LOOP UNTIL k$ = CHR$(27) CLOSE #1: PRINT "Finished!"


Example 3: Sending string data from one COM port to another requires predefined length strings:

DIM SHARED ByteIn AS STRING * 1 'One byte transfers DIM SHARED Byte4 AS STRING * 4 'Four byte transfers Byte4 = CHR$(254) + CHR$(175) + CHR$(0) + CHR$(3) 'Command code to query all 4 banks of switch input board. OPEN "COM1:115200,N,8,1,BIN,CS0,DS0" FOR RANDOM AS #1 'Open port used to send commands. OPEN "COM2:115200,N,8,1,BIN,CS0,DS0" FOR RANDOM AS #2 'Open port used to receive commands. PUT #1, , Byte4 'Send the 4 byte command. Start# = TIMER DO UNTIL LOC(2) <> 0 'Check if there is data received at com2 IF TIMER - Start# > .5 THEN EXIT DO 'Exit loop if no data arrives within .5 seconds. LOOP IF LOC(2) = 0 THEN 'If no data was received..... PRINT "No data received from COM port." END END IF PRINT "Received from COM2:"; DO UNTIL LOC(2) = 0 'Read data from COM2 until there is no more data. GET #2, , ByteIn PRINT ASC(ByteIn); LOOP END

Code courtesy of forum member Hydrofoiler


See also



Navigation:
Keyword Reference - Alphabetical
Keyword Reference - By Usage
Main Wiki Page