Name
OpenSerialPort -- open serial port connection (V8.0)
Synopsis
[id] = OpenSerialPort(id, portname$[, table])
Function
This function can be used to open a connection to the serial port specified in portname$ and assign the identifier id to the connection. If you pass Nil in id, OpenSerialPort() will automatically choose a vacant identifier and return it.

The name you pass in portname$ depends on the platform your script is running on. On Windows it could be COM1, on Linux and macOS it could be /dev/ttyS0 or /dev/ttyUSB0 in case you're using a USB adapter. On AmigaOS you have to pass the serial.device unit you want to open in portname$ and on Android it is assumed that there is only one port so portname$ is ignored.

Additionally, you can pass an optional table argument allowing you to set the parameters for the serial port connection. The following fields are currently recognized:

BaudRate:
The desired baud rate for the connection. This can be one of the following special constants:

#BAUD_300:
300 bits per second.
#BAUD_2400:
2400 bits per second.
#BAUD_4800:
4800 bits per second.
#BAUD_9600:
9600 bits per second. This is the default.
#BAUD_19200:
19200 bits per second.
#BAUD_38400:
38400 bits per second.
#BAUD_57600:
57600 bits per second.
#BAUD_115200:
115200 bits per second.
#BAUD_460800:
460800 bits per second.

DataBits:
The desired data bits for the connection. This can be set to one of the following special constants:

#DATA_5:
Use 5 data bits.
#DATA_6:
Use 6 data bits.
#DATA_7:
Use 7 data bits.
#DATE_8:
Use 8 data bits. This is the default.

StopBits:
The desired stop bits for the connection. This can be set to one of the following special constants:

#STOP_1:
Use 1 stop bit. This is the default.
#STOP_2:
Use 2 stop bits.

Parity:
The desired parity mode. This can be set to one of the following special constants:

#PARITY_NONE:
Do not use any parity bit. This is the default.
#PARITY_EVEN:
Use 1 bit of even parity.
#PARITY_ODD:
Use 1 bit of odd parity.

FlowControl:
The desired type of flow control to use. This can be set to one of the following special constants:

#FLOW_OFF:
Do not use any flow control. This is the default.
#FLOW_HARDWARE:
Use hardware flow control using CTS/RTS.
#FLOW_XON_XOFF:
Use software flow control using XON/XOFF handshaking.

RTS:
The desired state of the RTS pin. Note that manually setting the RTS pin isn't supported on every platform. Where supported, it can be set to one of the following special constants:

#RTS_ON:
Set the RTS pin.
#RTS_OFF:
Clear the RTS pin.

DTR:
The desired state of the DTR pin. Note that manually setting the DTR pin isn't supported on every platform. Where supported, it can be set to one of the following special constants:

#DTR_ON:
Set the DTR pin.
#DTR_OFF:
Clear the DTR pin.

As you can see above, the default configuration used by OpenSerialPort() is 9600/8-N-1, i.e. 9600 bps, 8 data bits, no parity bit, 1 stop bit. This is the most common configuration and should work on every platform.

Inputs
id
identifier for the new serial connection or Nil for auto id selection
portname$
serial port to open
table
optional: further options (see above)
Results
id
optional: identifier of the serial port connection; will only be returned when you pass Nil as argument 1 (see above)
Example
OpenSerialPort(1, "COM1")
WriteSerialData(1, "Hello World!")
CloseSerialPort(1)
The code above opens the serial port COM1 on Windows, sends the string "Hello World!" to the receiver and closes the serial port connection. Note that there is no guarantee that all 12 bytes could be sent to the serial port. In stable code, you would have to check the return value of WriteSerialData() and call it again if necessary to send the remaining bytes.

Show TOC