Difference between revisions of "OPEN COM"

From QB64 Wiki
Jump to navigation Jump to search
imported>Clippy
m
imported>Clippy
Line 40: Line 40:
  
  
''Example:'' Opening COM port 1 with the OP0 no line timeout option and setting the receive and transmit buffer size using [[LEN]] =.
+
''Example:'' Opening COM port with the BIN, CS0 and DS0 options and setting the receive and transmit buffer size using [[LEN]] =.
 +
 
 +
{{CodeStart}} '' ''
 +
{{Cl|INPUT}} "{{Cl|COM}} port number #", port$
 +
{{Cl|DIM}} bytestr {{Cl|AS}} {{Cl|STRING}} * 1
 +
{{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|DO}} 'main loop
 +
    'receive
 +
    {{Cl|IF}} {{Cl|LOC}}(1) {{Cl|THEN}} {{Cl|GET}} #1, , bytestr: {{Cl|PRINT}} "[" + bytestr + "]";
 +
    'send
 +
    k$ = {{Cl|INKEY$}}
 +
    {{Cl|IF}} {{Cl|LEN}}(k$) = 1 {{Cl|THEN}}
 +
        k = {{Cl|ASC}}(k$)
 +
        {{Cl|IF}} k >= 32 {{Cl|AND (boolean)|AND}} k <> 27 {{Cl|THEN}}
 +
            {{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!
  
{{CodeStart}}
 
OPEN "COM1: 9600, N, 8, 1, OP0" FOR RANDOM AS #1 LEN = 2048 ' input and output buffersize
 
 
{{CodeEnd}}
 
{{CodeEnd}}
  
Line 49: Line 66:
 
''See also:''  
 
''See also:''  
  
[[INPUT$]], [[LOC]], [[INKEY$]], [[PRINT (file statement)|PRINT #]], [[OPEN]]
+
* [[INPUT$]], [[PRINT (file statement)|PRINT #]]
 +
 
 +
* [[LOC]], [[INKEY$]], [[OPEN]]
 +
 
 +
* [[GET|GET #]], [[PUT|PUT #]]
  
  

Revision as of 08:29, 8 October 2010

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 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 no other 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.
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 :
CDm : Time until timeout of DCD (Carrier Detect) line in. CD0 ignores timeouts.
CSm : Time until timeout of CTS (Clear to Send) line in. CS0 ignores timeouts.
DSm : Time until timeout of DSR (Data Set Ready) line in. DS0 ignores timeouts.
OPm : 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!
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.




Example: Opening COM port with the BIN, CS0 and DS0 options and setting the receive and transmit buffer size using LEN =.

INPUT "COM port number #", port$ DIM bytestr AS STRING * 1 OPEN "COM" + port$ + ":9600,N,8,1,BIN,CS0,DS0" FOR RANDOM AS #1 LEN = 512 DO 'main loop 'receive IF LOC(1) THEN GET #1, , bytestr: PRINT "[" + bytestr + "]"; 'send k$ = INKEY$ IF LEN(k$) = 1 THEN k = ASC(k$) IF k >= 32 AND k <> 27 THEN 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