Serial Port
TsgcSerialPort: RS-232 and RS-485 serial port for Delphi, C++Builder and .NET, with event driven or blocking reads on Windows, Linux and macOS.
TsgcSerialPort: RS-232 and RS-485 serial port for Delphi, C++Builder and .NET, with event driven or blocking reads on Windows, Linux and macOS.
TsgcSerialPort opens RS-232 and RS-485 ports and USB serial adapters from Delphi, C++Builder and .NET. It configures the line, writes data and receives it in an event or with blocking reads, on Windows, Linux and macOS. A background reader thread fills a 64 KB receive buffer, and the API is the same in Delphi, C++Builder and C#. The component is also the transport of the Modbus RTU client and server.
TsgcSerialPort| Standards & specs | Windows DCB structure (serial line settings) · POSIX termios (Linux and macOS) · Linux RS-485 serial communications |
| Component class | TsgcSerialPort (unit sgcSerial_Port; in Delphi add sgcIndustrial to the uses clause). In .NET the class is TsgcSerialPort in the namespace esegece.sgcWebSockets. |
| Frameworks | VCL, FireMonkey, .NET |
| Platforms | In Delphi and C++Builder it works on Windows (Win32 and Win64) with the Win32 communications API, and on Linux64 and macOS with POSIX termios. It is not available on Android and iOS. The .NET version is built on System.IO.Ports. |
| Receive buffer | 64 KB (CS_SERIAL_BUFFER_SIZE), filled by a background reader thread |
| Errors | EsgcSerialError, with ErrorCode holding the operating system error code |
The principal published properties used to configure and drive the component. Consult the online help for the full list.
Active | Opens or closes the port. |
Port | Name of the port, such as COM3 on Windows or /dev/ttyUSB0 on Linux. |
BaudRate | Line speed in bits per second. |
DataBits | Data bits per character, 5 to 8. |
Parity | spNone, spOdd, spEven, spMark or spSpace. |
StopBits | ssOne, ssOnePointFive or ssTwo. |
FlowControl | sfcNone, sfcRtsCts, sfcDtrDsr or sfcXonXoff. |
RtsControl | srcDisable, srcEnable, srcHandshake or srcToggle (RS-485 direction control). |
ReadTimeout | Read timeout in milliseconds. |
WriteTimeout | Write timeout in milliseconds. |
NotifyEvents | How the events are delivered. By default they are queued to the main thread. |
The principal public methods exposed by the component.
Open() | Opens the port with the current line settings. |
Close() | Closes the port. Works from any thread, also from an event handler, and wakes a blocked Read. |
Write() | Writes a byte array and returns the number of bytes written. Can be called from any thread. |
WriteString() | Writes a string. |
Read() | Blocking read: waits for the first byte and returns when the device has been silent for the given time. Only while OnData is not assigned. |
Purge() | Discards the bytes waiting in the receive buffer. |
SetDTR() | Raises or lowers the DTR line. |
SetRTS() | Raises or lowers the RTS line. |
ModemStatus() | Returns the state of the CTS, DSR, RI and DCD lines. |
BufferCount() | Number of bytes waiting in the receive buffer. |
ListPorts() | Class method that lists the serial ports of the system in natural order. |
The published events of the component.
OnOpen | The port has been opened. |
OnClose | The port has been closed. |
OnData | A block of bytes has been received. |
OnError | An error happened while the port is open, for example the device was unplugged or the receive buffer is full. |
Set the port name and the line settings, assign the events and call Open. The snippet that follows shows the typical Open a port and receive data configuration, the same compiled sample as the component page.
uses sgcIndustrial; procedure TForm1.OpenPort; begin FPort := TsgcSerialPort.Create(Self); FPort.Port := 'COM3'; FPort.BaudRate := 19200; FPort.DataBits := 8; FPort.Parity := spEven; FPort.StopBits := ssOne; FPort.OnData := OnSerialData; FPort.OnError := OnSerialError; FPort.Open; FPort.WriteString('AT' + #13); end; procedure TForm1.OnSerialData(Sender: TObject; const aData: TBytes); begin // queued to the main thread by default (NotifyEvents = neAsynchronous) Memo1.Lines.Add(IntToStr(Length(aData)) + ' bytes received'); end; procedure TForm1.OnSerialError(Sender: TObject; const aError: string); begin Memo1.Lines.Add('Error: ' + aError); end;
#include "sgcIndustrial.hpp" void __fastcall TForm1::OpenPort() { FPort = new TsgcSerialPort(this); FPort->Port = "COM3"; FPort->BaudRate = 19200; FPort->DataBits = 8; FPort->Parity = spEven; FPort->StopBits = ssOne; FPort->OnData = OnSerialData; FPort->OnError = OnSerialError; FPort->Open(); FPort->WriteString("AT\r"); } void __fastcall TForm1::OnSerialData(TObject *Sender, const TBytes aData) { // queued to the main thread by default (NotifyEvents = neAsynchronous) Memo1->Lines->Add(IntToStr(aData.Length) + " bytes received"); } void __fastcall TForm1::OnSerialError(TObject *Sender, const String aError) { Memo1->Lines->Add("Error: " + aError); }
using System; using esegece.sgcWebSockets; class SerialTerminal { TsgcSerialPort FPort = new TsgcSerialPort(); public void OpenPort() { FPort.Port = "COM3"; FPort.BaudRate = 19200; FPort.DataBits = 8; FPort.Parity = TsgcSerialParity.spEven; FPort.StopBits = TsgcSerialStopBits.ssOne; FPort.OnData += OnSerialData; FPort.OnError += OnSerialError; FPort.Open(); FPort.WriteString("AT\r"); } void OnSerialData(object Sender, byte[] aData) { Console.WriteLine(aData.Length + " bytes received"); } void OnSerialError(object Sender, string aError) { Console.WriteLine("Error: " + aError); } }
The following scenarios use the same compiled samples as the component page. Each shows the configuration and method calls needed to drive the component through a specific real-world flow.
Leave OnData empty, write the command and call Read: it waits up to 1000 ms for the first byte and returns when the device has been silent for 50 ms.
procedure TForm1.QueryDevice; var vData: TBytes; vCount: Integer; begin // OnData is not assigned: the bytes wait in the receive buffer for Read FPort.Purge; FPort.WriteString('*IDN?' + #10); // wait up to 1000 ms for the first byte, the reply ends after 50 ms of silence vCount := FPort.Read(vData, 256, 1000, 50); if vCount = 0 then Memo1.Lines.Add('No reply') else Memo1.Lines.Add(IntToStr(vCount) + ' bytes read'); end;
void __fastcall TForm1::QueryDevice() { // OnData is not assigned: the bytes wait in the receive buffer for Read FPort->Purge(); FPort->WriteString("*IDN?\n"); // wait up to 1000 ms for the first byte, the reply ends after 50 ms of silence TBytes vData; int vCount = FPort->Read(vData, 256, 1000, 50); if (vCount == 0) Memo1->Lines->Add("No reply"); else Memo1->Lines->Add(IntToStr(vCount) + " bytes read"); }
using System; using esegece.sgcWebSockets; class SerialQuery { TsgcSerialPort FPort = new TsgcSerialPort(); public void QueryDevice() { // OnData is not assigned: the bytes wait in the receive buffer for Read FPort.Port = "COM3"; FPort.Open(); FPort.Purge(); FPort.WriteString("*IDN?\n"); // wait up to 1000 ms for the first byte, the reply ends after 50 ms of silence byte[] vData = FPort.Read(256, 1000, 50); if (vData.Length == 0) Console.WriteLine("No reply"); else Console.WriteLine(vData.Length + " bytes read"); } }
Every external claim links back to a primary source. The online-help references decode the canonical deep-link the company maintains for this component.
Industrial\01.Serial_Port