Serial Port Component
Open RS-232 and RS-485 ports from Delphi, C++Builder and .NET. Configure the line, write data and receive it in an event or with blocking reads, on Windows, Linux and macOS.
New
Open RS-232 and RS-485 ports from Delphi, C++Builder and .NET. Configure the line, write data and receive it in an event or with blocking reads, on Windows, Linux and macOS.
New
A serial port with a background reader thread, a 64 KB receive buffer and the same API in Delphi, C++Builder and C#. It is also the transport of the Modbus RTU client and server.
TsgcSerialPort
RS-232, RS-485 and USB serial adapters
Windows, Linux and macOS (not Android or iOS)
Included in sgcWebSockets Enterprise (Delphi and .NET) and All-Access, or as the standalone sgcIndustrial pack
Set the port name and the line settings, assign the events and call Open. OnData receives every block of bytes as it arrives.
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);
}
}
A complete serial port for instruments, modems, barcode readers, scales and field buses.
BaudRate, DataBits (5 to 8), Parity (none, odd, even, mark, space), StopBits (1, 1.5, 2) and FlowControl (RTS/CTS or XON/XOFF). Invalid combinations raise EsgcSerialError when the port opens.
With OnData assigned, every block of bytes goes to the event. Without it, the bytes wait in a 64 KB buffer and Read returns a whole reply: it waits for the first byte and ends after a silence you choose.
Set RtsControl to srcToggle for adapters that switch the direction with RTS. On Windows the driver raises RTS while it transmits, and on Linux the kernel RS-485 mode is enabled. In .NET the toggle is timed by the component.
TsgcSerialPort.ListPorts lists the ports in natural order (COM2 before COM10). SetDTR, SetRTS and ModemStatus drive and read the modem lines.
Write can be called from any thread. Close works from any thread, also from an event handler, and wakes a blocked Read. In Delphi and C++Builder the events are queued to the main thread by default, and NotifyEvents changes that.
Every failure raises EsgcSerialError, whose ErrorCode holds the operating system error code when the system reported one. A device unplugged while the port is open, or a full receive buffer, fires OnError.
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");
}
}
The operating system interfaces this component is built on.
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.
Use OnData for devices that send data on their own, such as a barcode reader or a scale. Use Read for request and reply protocols: send the command, then read until the device stops sending. The two are exclusive: Read raises EsgcSerialError while OnData is assigned, and only one thread may call Read at the same time.
An adapter that switches the direction automatically needs no setting. An adapter that switches the direction with the RTS line needs RtsControl set to srcToggle. On Linux the component then enables the RS-485 mode of the driver and raises EsgcSerialError when the driver does not support it. macOS does not support srcToggle.
It is included in sgcWebSockets Enterprise (Delphi and .NET) and All-Access, as part of the sgcIndustrial pack. In Delphi, add sgcIndustrial to the uses clause.
Deep-link to the component reference, open the demo project and read the technical document.
| Online Help: TsgcSerialPort Full property, method and event reference for this component. | Open | |
| Demo Project: Industrial\01.Serial_Port A serial terminal for Delphi, C++Builder and .NET. It ships inside the sgcWebSockets package. | Open | |
| Technical Document (PDF) Features, quick start, code samples and primary-source references for this component only. | Open | |
| User Manual (PDF) Comprehensive manual covering every component in the library. | Open |