串口组件

从 Delphi、C++Builder 和 .NET 打开 RS-232 和 RS-485 端口。配置线路参数,写入数据,并在 Windows、Linux 和 macOS 上通过事件或阻塞读取接收数据。

新增

TsgcSerialPort

带后台读取线程和 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 返回完整的应答:它会等待第一个字节,并在您设定的静默时间后结束。

RS-485 方向控制

对于通过 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 子句。

文档与演示

直达组件参考,打开演示项目并阅读技术文档。

Online Help: TsgcSerialPort 本组件的完整属性、方法和事件参考。
演示项目:Industrial\01.Serial_Port 适用于 Delphi、C++Builder 和 .NET 的串口终端。随 sgcWebSockets 软件包一同提供。
技术文档 (PDF) 仅针对此组件的功能、快速入门、代码示例和一手资料参考。
用户手册 (PDF) 涵盖库中每个组件的综合手册。
超值之选:All-AccesseSeGeCe 全部产品,含高级支持,每年 €1,059 起。
查看 All-Access 价格

包含在 sgcWebSockets Enterprise 中

包含在 sgcWebSockets Enterprise(Delphi 和 .NET)和 All-Access 或独立的 sgcIndustrial 软件包中。