[id] = OpenSerialPort(id, portname$[, table])
portname$
and assign the identifier id
to the connection. If you pass Nil
in id
, OpenSerialPort()
will automatically choose an 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.
Starting with Hollywood 9.0, portname$
can also be a string in the format "<devicename>:<port>" now
on AmigaOS and compatibles. This is useful in case you want OpenSerialPort()
to open an alternative
serial device instead of AmigaOS's standard serial.device
. For example, passing "serialpl2303.device:0"
in portname$
will try to open serialpl2303.device
on port 0.
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:
#BAUD_300:
#BAUD_600:
#BAUD_1200:
#BAUD_2400:
#BAUD_4800:
#BAUD_9600:
#BAUD_19200:
#BAUD_38400:
#BAUD_57600:
#BAUD_115200:
#BAUD_460800:
DataBits:
#DATA_5:
#DATA_6:
#DATA_7:
#DATE_8:
StopBits:
#STOP_1:
#STOP_2:
Parity:
#PARITY_NONE:
#PARITY_EVEN:
#PARITY_ODD:
FlowControl:
#FLOW_OFF:
#FLOW_HARDWARE:
#FLOW_XON_XOFF:
RTS:
#RTS_ON:
#RTS_OFF:
DTR:
#DTR_ON:
#DTR_OFF:
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.