Difference between revisions of "OPEN COM"

From QB64 Wiki
Jump to navigation Jump to search
imported>Clippy
m
imported>Clippy
m
Line 15: Line 15:
 
* Other [[OPEN]] options are optional and in any order separated by commas within the OPEN command [[STRING|string]].  
 
* Other [[OPEN]] options are optional and in any order separated by commas within the OPEN command [[STRING|string]].  
 
* The optional FOR access ''mode'' can be [[OUTPUT]], [[INPUT (file mode)|INPUT]] or [[RANDOM]] (default mode when no FOR statement is used).  
 
* The optional FOR access ''mode'' can be [[OUTPUT]], [[INPUT (file mode)|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 [[BINARY|BIN]] mode!'''
 
* '''Use the BIN option listed below for [[BINARY]] byte mode port access!'''
 
* '''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.
 
* 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.
 
* 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!
  
  
::::::::'''Additional Options'''
+
<center>'''Additional Options'''</center>
 
 
 
:: * ASC : [[ASCII]] byte mode. End of line = [[CHR$]](13). End of file = CHR$(26)
 
:: * 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).
 
:: * BIN : [[Binary]] byte mode. Default mode if ASC is not used(option not required).
Line 28: Line 29:
 
::: CS[m] : Time until timeout of CTS (Clear to Send) line in. CS0 ignores timeouts.
 
::: CS[m] : Time until timeout of CTS (Clear to Send) line in. CS0 ignores timeouts.
 
::: DS[m] : Time until timeout of DSR (Data Set Ready) line in. DS0 ignores timeouts.
 
::: DS[m] : Time until timeout of DSR (Data Set Ready) line in. DS0 ignores timeouts.
::: '''OP[m]''' : Time until data lines become active. If timeout then OPEN fails! OP0 ignores timeouts.
+
::: OP[m] : Time until data lines become active. If timeout then OPEN fails! OP0 ignores timeouts.
 
:::: ''If any timeouts occur the OPEN will fail or port access will stop!''
 
:::: ''If any timeouts occur the OPEN will fail or port access will stop!''
  
Line 36: Line 37:
  
  
 +
<center>'''Qbasic Description NOT Currently supported in QB64!'''</center>
 
*[[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]].   
 
*[[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]].   
*[[RANDOM]] or [[BINARY]] modes can use [[INPUT (file statement)|INPUT #]], [[INPUT$]], [[PRINT (file statement)|PRINT #]], [[GET]] or [[PUT]]. BIN cannot set a buffer size!
+
*[[RANDOM]] or [[BINARY|BIN]] modes can use [[INPUT (file statement)|INPUT #]], [[INPUT$]], [[PRINT (file statement)|PRINT #]], [[GET]] or [[PUT]]. BIN cannot set a buffer size!
 
* Note: If random use [[LEN]] = to set size of random buffer(default LEN = 128). Use multiples of 128 for larger buffers.
 
* 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 OPEN COM!
 
 
* NOTE: NT or XP computers need a program like [http://www.beyondlogic.org/porttalk/porttalk.htm PortTalk] to access COM or other ports with Qbasic only!
 
* NOTE: NT or XP computers need a program like [http://www.beyondlogic.org/porttalk/porttalk.htm PortTalk] to access COM or other ports with Qbasic only!
  
Line 45: Line 46:
  
  
''Example:'' Opening COM port with the BIN, CS0 and DS0 options and setting the receive and transmit buffer size using [[LEN]] =.
+
''Example:'' Opening a COM port with the BIN, CS0 and DS0 options in '''QB64'''.
  
 
{{CodeStart}} '' ''
 
{{CodeStart}} '' ''
 
{{Cl|INPUT}} "{{Cl|COM}} port number #", port$  'any COM port number available
 
{{Cl|INPUT}} "{{Cl|COM}} port number #", port$  'any COM port number available
 
{{Cl|DIM}} bytestr {{Cl|AS}} {{Cl|STRING}} * 1  'one byte transfers
 
{{Cl|DIM}} bytestr {{Cl|AS}} {{Cl|STRING}} * 1  'one byte transfers
{{Cl|OPEN}} "{{Cl|COM}}" + port$ + ":9600,N,8,1,BIN,CS0,DS0" {{Cl|FOR (file statement)|FOR}} {{Cl|RANDOM}} {{Cl|AS}} #1 {{Cl|LEN}} = 512
+
{{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
 
{{Cl|DO}} 'main loop
 
     'receive data in buffer when LOC > 0
 
     'receive data in buffer when LOC > 0
     {{Cl|IF}} {{Cl|LOC}}(1) {{Cl|THEN}} {{Cl|GET}} #1, , bytestr: {{Cl|PRINT}} "[" + bytestr + "]";
+
     {{Cl|IF}} {{Cl|LOC}}(1) {{Cl|THEN}}  
 +
      {{Cl|GET}} #1, , bytestr
 +
      {{Cl|PRINT}} "[" + bytestr + "]";
 +
    {{Cl|END IF}}
 
     'transmit (send)
 
     'transmit (send)
 
     k$ = {{Cl|INKEY$}}   
 
     k$ = {{Cl|INKEY$}}   
 
     {{Cl|IF}} {{Cl|LEN}}(k$) = 1 {{Cl|THEN}}
 
     {{Cl|IF}} {{Cl|LEN}}(k$) = 1 {{Cl|THEN}}
        k = {{Cl|ASC}}(k$)
+
      k = {{Cl|ASC}}(k$)
        {{Cl|IF}} k >= 32 {{Cl|THEN}}    'ignore control key codes
+
      {{Cl|IF}} k >= 32 {{Cl|THEN}}    'ignore control key codes
            {{Cl|PRINT}} ">" + k$ + "<";
+
          {{Cl|PRINT}} ">" + k$ + "<";
            bytestr = k$: {{Cl|PUT}} #1, , bytestr
+
          bytestr = k$: {{Cl|PUT}} #1, , bytestr
        {{Cl|END IF}}
+
      {{Cl|END IF}}
 
     {{Cl|END IF}}
 
     {{Cl|END IF}}
 
{{Cl|LOOP}} {{Cl|UNTIL}} k$ = {{Cl|CHR$}}(27)
 
{{Cl|LOOP}} {{Cl|UNTIL}} k$ = {{Cl|CHR$}}(27)

Revision as of 16:18, 4 February 2011

The OPEN COM statement is used to access a computer's Serial port COM1 or COM2 address using an OPEN statement.


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


  • QB64 can open any COM n port number. Qbasic could only open COM1 or COM2.
  • See Windows System Components list for COM port numbers and port addresses.
  • Four commas are required after the Speed, Parity, Bits, and Stopbit, even if none of the Options are used.
  • Speed(baud rate): 50, 150, 300, 600, 1200, 1800, 2400, 9600, 19200 or 115200 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
  • Other OPEN options are optional and in any order separated by commas within the OPEN command string.
  • 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!


Additional Options
* 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 m is the timeout in milliseconds 1 to 65535. Zero is ignore timeout. Default m = 1000 :
CD[m] : Time until timeout of DCD (Carrier Detect) line in. CD0 ignores timeouts.
CS[m] : Time until timeout of CTS (Clear to Send) line in. CS0 ignores timeouts.
DS[m] : Time until timeout of DSR (Data Set Ready) line in. DS0 ignores timeouts.
OP[m] : Time until data lines become active. If timeout then OPEN fails! OP0 ignores timeouts.
If any timeouts occur the OPEN will fail or port access will stop!
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.


Qbasic Description NOT Currently supported in QB64!



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

INPUT "COM port number #", port$ 'any COM port number available DIM bytestr AS STRING * 1 'one byte transfers 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!


See also:




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