The SerialPort class (gb.net) was developed to enable communication via a serial interface (usually an RS-232 interface). This class inherits from the stream class, so you can use standard streams to send and receive data and close the port. You can create the class.
Dim hSerialPort As SerialPort hSerialPort = New SerialPort() As "RS232" ' Event name
Constant | Value | Description |
---|---|---|
Bits1 | 1 | This constant is used if the serial interface has 1 stop bit. |
Bits2 | 2 | This constant is used if the serial interface has 2 or 1.5 stop bits. |
Bits5 | 5 | This constant is used if the serial interface has 5 data bits. |
Bits6 | 6 | This constant is used if the serial interface has 6 data bits. |
Bits7 | 7 | This constant is used if the serial interface has 7 data bits. |
Bits8 | 8 | This constant is used if the serial interface has 8 data bits. |
Both | 3 | This constant is used when the serial interface is to use both software and hardware flow control. This means that CRTSCTS plus XON/XOFF are used for flow control. |
Hardware | 1 | This constant is used when the serial port is to use hardware flow control. This means that RTS/CTS or RFR/CTS are used for flow control. |
Software | 2 | This constant is used when the serial interface is to use software flow control. This means that the special control characters Xon (DC1, 11hex, 17dec) and Xoff (DC3, 13hex, 19dec) are used for flow control in the data stream. |
None | 0 | This constant is used if the serial interface should not use parity or if the serial interface does not use flow control. |
Even | 1 | This constant represents an even parity for the serial interface. You can set or read this value in the Parity property. |
Odd | 2 | This constant stands for an odd parity of the serial interface. You can set or read this value in the Parity property. |
Table 24.1.5.0.1: Constants of the class SerialPort
The applicable constants from the Net class (gb.net) also apply. For example, the status property is set to Net.Active after successfully opening the serial port via SerialPort.Open().
The SerialPort class has these properties:
Property | Data type | Description |
---|---|---|
Blocking | Boolean | Returns True if the stream is blocked or sets the value. If this property is set to True, reading from the stream is blocked if there is nothing to read and writing to the stream is blocked if, for example, the internal system buffer is full. |
ByteOrder | Integer | Returns or sets the byte order used to read or write binary data to the stream. The property can have the following values: gb.BigEndian = BigEndianByte order and gb.LittleEndian = LittleEndianByte order. |
CTS | Boolean | Returns the current status of the signal “Clear To Send” of the serial interface. The value is False if the interface is closed. |
DCD | Boolean | Returns the current status of the signal “Data Carrier Detect” of the serial interface. The value is False if the interface is closed. |
DSR | Boolean | Returns the current status of the “Data Set Ready” signal of the serial interface. The value is False if the interface is closed. |
RNG | Boolean | Returns the current status of the “Ring Indicator” signal of the serial interface. If the serial interface is closed and you read this property, False is returned. |
DTR | Boolean | Returns or sets the current status of the “Data Terminal Ready” signal of the serial interface. This value cannot be set when the serial port is closed. If the serial interface is closed and you read this property, False is returned. |
RTS | Boolean | Returns or sets the current status of the “Request To Send” signal of the serial interface. This value cannot be set when the serial port is closed. If the serial interface is closed and you read this property, False is returned. |
DataBits | Integer | Returns or sets the number of data bits used by the serial interface. Use one of the following constants: SerialPort.Bits5, SerialPort.Bits6, SerialPort.Bits7 or SerialPort.Bits8. |
StopBits | Integer | Return or set the stop bits of the serial interface. Use one of the following constants: SerialPort.Bits1 or SerialPort.Bits2 Note: For 5-bit data width, setting 2 stop bits results in 1.5 stop bits! |
Parity | Integer | Returns or sets the current parity of the serial interface. Use one of the following constants: SerialPort.None, SerialPort.Odd or SerialPort.Even. |
Speed | Integer | Set the communication speed in Baud. The value must be a valid default value supported by the serial interface driver (…, 4800, 9600,…). Be careful as some common speed values are allowed but not all serial interfaces (UART) support them! |
FlowControl | Integer | The current data flow control of the serial interface is set or its value is read out. Use one of the following constants: SerialPort.None, SerialPort.Hardware, SerialPort.Software, or SerialPort.Both. |
EndOfFile | Boolean | This property indicates whether the last use of LINE INPUT has reached the end of the file, instead of reading an entire line with a line end character. |
EndOfLine | Integer | Returns or sets the line break separator used by this stream. The possible values are: gb.Unix for lines separated by Chr$(10), gb.Windows for lines separated by Chr$(13) and Chr$(10) or gb.Mac for lines separated by Chr$(13). The value of this property is used by LINE INPUT, PRINT and the Stream.Lines property. |
PortName | String | Returns or sets the current path of the serial interface. Example: mySerialPort.PortName = “/dev/ttyS0” This property can only be changed if the serial port is closed. |
Handle | Integer | Returns the system file descriptor associated with this stream. |
IsTerm | Boolean | Returns True if a stream is assigned to a terminal. |
Term | .Stream.Term | Returns a virtual object with which the terminal assigned to the stream can be managed. |
Lines | .Stream.Lines | Returns a virtual object with which you can scroll through a stream line by line. (For Each aString In hStream.Lines … Next) |
InputBufferSize | Integer | Returns the number of bytes of the internal input buffer. |
OutputBufferSize | Integer | Returns the number of bytes of the internal output buffer. |
Tag | Variant | Returns or sets the Tag. This property is intended for the programmer and is never used by the component. Tag can contain any variant value. |
Status | Integer | Returns the current status of a SerialPort object. Net.Active: The port is open. Net.Inactive - The port is closed. |
Table 24.1.5.0.2: Properties of the class SerialPort
The class has these seven methods:
Method | Return type | Description |
---|---|---|
Open( [ Polling As Integer )) | ~ | Opens the serial interface. The Status property is set to Net.Active. The optional argument Polling determines the number of attempts. |
Close( ) | ~ | Closes the stream. This statement corresponds exactly to the Close() statement. |
Begin( ) | ~ | Starts to buffer the data written in the stream so that everything is sent when the send method is called. |
Send( )) | ~ | Sends all data written to the buffer at once since the last call from “Begin”. |
Drop( ) | ~ | Deletes the data buffered since the last call of the Begin() method. |
ReadLine( [ Escape As String ] ) | String | Reads a text line from the stream, as with the LINE INPUT statement. If escape is specified, line breaks between two escape characters are ignored. |
Watch( Mode As Integer, Watch As Boolean ) | ~ | Start or stop the observation of the stream file descriptor for reading or writing after it is opened. Mode is the watch type: gb.Read for reading and gb.Write for writing. If the argument Watch True, the observation is activated; if False, the observation is deactivated. |
Table 24.1.5.0.3: Methods of the class SerialPort
The SerialPort class has these events:
Event | Description |
---|---|
CTSChange( CurrentValue As Boolean ) | The event is triggered when the CTS signal value changes. CurrentValue stores the current value of this property. |
DSRChange( CurrentValue As Boolean ) | The event is triggered when the DSR signal value changes. CurrentValue stores the current value of this property. |
DTRChange( CurrentValue As Boolean ) | The event is triggered when the DTR signal value changes. CurrentValue stores the current value of this property. |
RTSChange( CurrentValue As Boolean ) | The event is triggered when the RTS signal value changes. CurrentValue stores the current value of this property. |
DCDChange( CurrentValue As Boolean ) | The event is triggered when the DCD signal value changes. CurrentValue stores the current value of this property. |
RNGChange ( CurrentValue As Boolean ) | The event is triggered when the RNG signal value changes. CurrentValue stores the current value of this property. |
Read( ) | The event is triggered when data can be read from the serial interface. |
Table 24.1.5.0.4: Events of the class SerialPort
With this source code, a temperature value from a temperature sensor (DAC) is read out, interpolated and displayed every 500ms via a serial interface USB-RS232 adapter:
' Gambas class file Public hRS232 As SerialPort Public sTemperatureDigit As String Public TTimer As Timer Public Sub Form_Open() FMain.Center FMain.Resizable = False hRS232 = New SerialPort As "hRS232" TTimer = New Timer As "TTimer" TTimer.Delay = 500 ' The temperature is read out every 500ms SetLEDColor(pboxStatus, "red") StartMeasurement() End Public Sub StartMeasurement() hRS232.PortName = "/dev/ttyUSB0" hRS232.Speed = 4800 hRS232.DataBits = SerialPort.Bits8 hRS232.StopBits = SerialPort.Bits1 hRS232.Parity = SerialPort.None hRS232.FlowControl = SerialPort.None Try hRS232.Open(3) If Error Then Message.Error(("Error when opening the V24-RS232-USB adapter interface!")) SetLEDColor(pboxStatus, "red") Else If hRS232.Status = Net.Active Then TTimer.Start() Print hRS232.DTR Print hRS232.DSR Print hRS232.Blocking Print hRS232.CTS Print hRS232.RTS Endif Endif End Public Sub hRS232_Read() Read #hRS232, sTemperatureDigit, Lof(hRS232) End Public Sub TTimer_Timer() If hRS232.Status <> Net.Active Or If Asc(sTemperatureDigit) = 0 Then lblTemperaturAnzeige.Text = "--- °C" Else lblTemperaturAnzeige.Text = Str(Interpolation(Asc(sTemperatureDigit))) & " °C" TextBox1.Text = Asc(sTemperatureDigit) pboxStatus.SetFocus() SetLEDColor(pboxStatus, "green") Endif End Private Function Interpolation(iArgument As Integer) As Float Dim fX0, fX1, fY0, fY1, f As Float ' Experimentally determined data points fX0 = 22 fY0 = 22.3 fX1 = 30 fY1 = 34.5 ' Interpolation f = ((fY1 - fY0) / (fX1 - fX0)) * (iArgument - fX0) + fY0 Return Round(f, -1) End Private Sub SetLEDColor(picBox As PictureBox, sLEDColor As String) picBox.Picture = Picture["LED/led_" & sLEDColor & ".svg"] End Public Sub Form_Close() If hRS232.Status = Net.Active Then hRS232.Close() FMain.Close() Endif End
Figure 24.1.5.0.1: Temperature display