[id] = OpenSerialPort(id, portname$[, table])
portname$and assign the identifier
idto the connection. If you pass Nil in
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/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:
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.
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.