串口组件
从 Delphi、C++Builder 和 .NET 打开 RS-232 和 RS-485 端口。配置线路参数,写入数据,并在 Windows、Linux 和 macOS 上通过事件或阻塞读取接收数据。
新增
从 Delphi、C++Builder 和 .NET 打开 RS-232 和 RS-485 端口。配置线路参数,写入数据,并在 Windows、Linux 和 macOS 上通过事件或阻塞读取接收数据。
新增
带后台读取线程和 64 KB 接收缓冲区的串口,在 Delphi、C++Builder 和 C# 中使用相同的 API。它同时也是 Modbus RTU 客户端和服务器的传输层。
TsgcSerialPort
RS-232、RS-485 和 USB 串口适配器
Windows、Linux 和 macOS(不支持 Android 或 iOS)
包含在 sgcWebSockets Enterprise(Delphi 和 .NET)和 All-Access 或独立的 sgcIndustrial 软件包中
设置端口名称和线路参数,分配事件并调用 Open。OnData 会在数据到达时接收每一个字节块。
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);
}
}
适用于仪器、调制解调器、条码阅读器、电子秤和现场总线的完整串口。
BaudRate、DataBits(5 至 8)、Parity(无、奇、偶、标记、空格)、StopBits(1、1.5、2)和 FlowControl(RTS/CTS 或 XON/XOFF)。无效的组合会在端口打开时抛出 EsgcSerialError。
分配了 OnData 后,每个字节块都会交给该事件。未分配时,字节会保留在 64 KB 缓冲区中,Read 返回完整的应答:它会等待第一个字节,并在您设定的静默时间后结束。
对于通过 RTS 切换方向的适配器,请将 RtsControl 设置为 srcToggle。在 Windows 上,驱动程序会在发送期间拉高 RTS,在 Linux 上则启用内核的 RS-485 模式。在 .NET 中,切换时序由组件控制。
TsgcSerialPort.ListPorts 按自然顺序列出端口(COM2 在 COM10 之前)。SetDTR、SetRTS 和 ModemStatus 用于驱动和读取调制解调器线路。
Write 可在任何线程中调用。Close 可在任何线程中使用,包括事件处理程序内部,并会唤醒被阻塞的 Read。在 Delphi 和 C++Builder 中,事件默认排入主线程队列,可通过 NotifyEvents 更改。
每次失败都会抛出 EsgcSerialError,当系统报告了操作系统错误码时,其 ErrorCode 会保存该错误码。端口打开期间设备被拔出,或接收缓冲区已满,都会触发 OnError。
保持 OnData 为空,写入命令并调用 Read:它最多等待 1000 毫秒以接收第一个字节,并在设备静默 50 毫秒后返回。
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");
}
}
本组件所基于的操作系统接口。
在 Delphi 和 C++Builder 中,它通过 Win32 通信 API 在 Windows(Win32 和 Win64)上运行,并通过 POSIX termios 在 Linux64 和 macOS 上运行。Android 和 iOS 不可用。.NET 版本基于 System.IO.Ports 构建。
对于会主动发送数据的设备,例如条码阅读器或电子秤,请使用 OnData。对于请求与应答协议,请使用 Read:发送命令,然后读取直到设备停止发送。两者互斥:分配了 OnData 时,Read 会抛出 EsgcSerialError,并且同一时间只能有一个线程调用 Read。
自动切换方向的适配器无需任何设置。通过 RTS 线切换方向的适配器需要将 RtsControl 设置为 srcToggle。在 Linux 上,组件随后会启用驱动程序的 RS-485 模式,如果驱动程序不支持,则抛出 EsgcSerialError。macOS 不支持 srcToggle。
它作为 sgcIndustrial 包的一部分,包含在 sgcWebSockets Enterprise(Delphi 和 .NET)和 All-Access 中。在 Delphi 中,请将 sgcIndustrial 添加到 uses 子句。