6.1-roSerialPort

ON THIS PAGE


This object controls the RS-232 serial port, allowing you to receive input and send responses.

Object Creation: The roSerialPort object is created with two parameters.

CreateObject(roSerialPort, port As Integer, baud_rate As Integer)
  • port: The port enumeration of the serial device. Most standard RS-232 serial devices enumerate on port 0. If you are connecting a USB-serial device (such as a GPS receiver), it will enumerate on port 2.
  • baud_rate: The baud rate for serial communication. The serial port supports the following baud rates: 1800, 2400, 4800, 9600, 19200, 38400, 57600, 115200.

 

The roSerialPort object sends the following event types:

  • roStreamLineEvent: The line event is generated whenever the end of line string set using SetEol is found and contains a string for the whole line. This object implements the ifString and ifUserData interfaces.
  • roStreamByteEvent: The byte event is generated on every byte received. This object implements the ifInt and ifUserData interfaces.

ifStreamSend

SetSendEol(eol_sequence As String) As Void

Sets the EOL sequence when writing to the stream. The default value is CR (ASCII value 13). If you need to set this value to a non-printing character, use the chr() global function.

SendByte(byte As Integer) As Void

Writes the specified byte to the stream.

SendLine(string As String) As Void

Writes the specified characters to the stream followed by the current EOL sequence.

SendBlock(a As Dynamic) As Void

Writes the specified characters to the stream. This method can support either a string or an roByteArray. If the block is a string, any null bytes will terminate the block.

Flush()

ifStreamReceive

SetLineEventPort(port As Object) As Void
 
SetByteEventPort(port As Object) As Void
 
SetReceiveEol(eol_sequence As String)

Sets the EOL sequence to detect when receiving the stream. The default value is CR (ASCII value 13). If you need to set this value to a non-printing character, use the chr() global function.

SetMatcher(matcher As Object) As Boolean

Instructs the stream to use the specified matcher. This method returns True if successful. Pass Invalid to this method to stop using the specified matcher.

ifSerialControl

SetBaudRate(baud_rate As Integer) As Boolean

Sets the baud rate of the device. The supported baud rates are as follows: 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800, 9600, 19200, 38400, 57600, 115200, 230400.

SetMode(mode As String) As Boolean

Sets the serial mode in "8N1" syntax. The first character is the number of data bits. It can be either 5, 6, 7, or 8. The second number is the parity. It can be "N"one, "O"dd, or "E"ven. The third is the number of stop bits. It can be 1 or 2.

SetEcho(enable As Boolean) As Boolean

Enables or disables serial echo. It returns True on success and False on failure.

SetEol(a As String)
 
SetFlowControl(enalbe As Boolean) As Boolean

Enables or disable RTS/CTS handshaking over the serial port. This feature is currently only available on 4Kx42, XDx32, and HDx22 models.

SetInverted(inverted As Boolean) As Boolean

Inverts the signals on the player serial port. This allows the player to communicate with most PCs that use -12v to 12v signaling. Passing True to the method enables inversion, whereas passing False disables it.

SendBreak(duration_in_ms As Integer) as Boolean

Sends a serial break or sets the serial break condition. This method returns True upon success and False upon failure.

  • duration_in_ms = -1: Sends a continuous break.
  • duration_in_ms = 0: Clears the break state.
  • duration_in_ms >= 100: Sets the break condition for the specified period of milliseconds (note that this integer is only accurate to the tenth of a second).

ifUserData

SetUserData(user_data As Object)

Sets the user data that will be returned when events are raised.

GetUserData() As Object

Returns the user data that has previously been set via SetUserData(). It will return Invalid if no data has been set.

 


 

This example script waits for a serial event and echoes the input received on the serial port to the shell:

 

serial = CreateObject("roSerialPort", 0, 9600)
p = CreateObject("roMessagePort")
serial.SetLineEventPort(p)

serial_only:
msg = Wait(0,p) ' Wait forever for a message.
if(type(msg) <> "roStreamLineEvent") goto serial_only 'Accept serial messages only.
serial.SendLine(msg) ' Echo the message back to serial.