如何使用Delphi Prism进行串口通信?

发布于 2024-11-15 11:05:37 字数 785 浏览 4 评论 0原文

我对 Delphi Prism 非常陌生。事实上,你甚至可以说我是一个新手,但我已经用 Delphi 编程了 10 年了。我能够将在以前版本的 Delphi 中编写的遗留软件移植到 Delphi 2010。现在,我准备使用 Delphi Prism 为 WEB (ASP.NET) 的 Delphi.Net 编写相同的软件。对于我的一生,我似乎无法理解 Delphi Prism 的总体布局或工作原理。我已经阅读了教程和示例,甚至在 Stackoverflow 上搜索了示例和代码。尽管如此,我似乎还是无法理解 Delphi Prism,尽管我已经花了整整一个星期的时间来玩 Delphi Prism IDE。

网上有人说,只需使用.NET框架,就可以将一个窗口独立程序编写为完全用于Web的程序。

现在我只是尝试使用 SerialPort 组件创建一个 Web 表单以在 comport 上进行通信。我能够设计网页控件(按钮、标签)...该程序在本地主机启动时编译并加载。我在浏览器上看到按钮,但它无法在串行端口上进行通信。

我很困惑。看来我做得不对。有人能给我一个 Delphi Prism SerialPort 组件使用的例子吗?

我不确定是我安装了它还是它随 Delphi Prism IDE 一起提供。但是,如果启动 Delphi Prism 后查看“组件”部分下的工具箱,您将看到一个名为 SerialPort 的组件。显然,它是 Microsoft .NET Framework 的一部分。

我想问如何使用 Delphi Prism IDE 附带的串行端口组件。

非常感谢,

I am very much new to Delphi Prism. In fact, you can even say I am a newbie, but I have programmed in Delphi for the last good 10 years. I was able to port our legacy software that was written in previous version of Delphi to Delphi 2010. Now, I am ready to write the same software for Delphi.Net for the WEB (ASP.NET) using Delphi Prism. For the life of me, I can't seem to understand the overall layout or idea of how Delphi Prism works. I have gone through tutorials and examples and even searched on Stackoverflow for samples and codes. Still, I can't seem to understand Delphi Prism, even though I have already spent a whole week just playing around with Delphi Prism IDE.

Someone online said that one can take a window standalone program and write it entirely for the web by just using .NET framework.

Right now I am simply trying to create a webform with SerialPort Component to communicate on the comport. I was able to design the webpage controls(buttons, labels)... The program compiles and loads with a localhost started. I see the buttons on the browser, but it won't communicate on the serialport.

I am confused. It looks like I am not doing it right. Can someone give me an example of Delphi Prism SerialPort component usage?

I am not sure if I installed it or it came with the Delphi Prism IDE. However, if you look in the toolbox under Components section after starting Delphi Prism, you will see a component called SerialPort. Apparently, it is part of Microsoft .NET Framework.

I meant to ask how one would use the serial port component that came with the Delphi Prism IDE.

Thank you much,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

九八野马 2024-11-22 11:05:37

您必须寻找 .Net 串行端口组件,而不是寻找用于串行端口的 Delphi prism 组件。您可以使用这篇文章 SerialPort(RS-232 串行 COM 端口) C# .NET 作为有关此主题的指南。

现在检查这个处理串行端口通信(仅接收数据)的delphi prism类。

namespace SerialComm;

interface

uses
  System.IO.Ports, //this namespace contains classes for controlling serial ports.      
  System.Collections.Generic,
  System.Linq,
  System.Text;

type
  DataMode = (Text,Hex);

  TSerialComm = public class
  private
    CurrentDataMode     : DataMode;
    method port_DataReceived( sender : object;  e : SerialDataReceivedEventArgs);
  public
    ComPort             : SerialPort := new SerialPort();
    method OpenPort();
    method ClosePort();
    method Init;
  end;

implementation

//here you receive the data
method TSerialComm.port_DataReceived( sender : object;  e : SerialDataReceivedEventArgs);

    method  ByteArrayToHexString(data : Array of Byte) : string;
    Begin
      var sb : StringBuilder := new StringBuilder(data.Length * 3);
      for each b in data do 
      sb.Append(Convert.ToString(b, 16).PadLeft(2, '0').PadRight(3, ' '));
      result:=sb.ToString().ToUpper();
    end;

Var
  data :string ;
begin

    if not ComPort.IsOpen then  exit;

    try
              // text mode
              if (CurrentDataMode = DataMode.Text) then
              begin                                    
                   data  := comport.ReadExisting();    
             //do your stuff here 
              end
              else
              ///binary (hex) mode
              begin
                var bytes : Integer  := ComPort.BytesToRead;
                var buffer : Array of byte  := new byte[bytes];
                ComPort.Read(buffer, 0, bytes);
                Data:= ByteArrayToHexString(buffer);
                //do your stuff here 
              end;

    except
      on ex: exception do begin         
          OutLog('port_DataReceived: ' + ex.Message + ' ** Trace: ' + ex.StackTrace);
         exit;
      end;
    end;
end;

method TSerialComm.OpenPort();
begin
      CurrentDataMode := DataMode.Text;
      OutLog('Open Port COM');
      if ComPort.IsOpen then ClosePort();       
       ComPort.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived); 

        //ComPort.ReadTimeout:=100;
        ComPort.BaudRate := Convert.ToInt32(_Settings.BaudRate);
        ComPort.DataBits := Convert.ToInt32(_Settings.DataBits);
        var  aStopBits : StopBits  := StopBits(Enum.Parse(typeof(StopBits), _Settings.StopBits, true));
        ComPort.StopBits           := aStopBits;
        var  aParity : Parity      := Parity(Enum.Parse(typeof(Parity), _Settings.Parity, true));
        ComPort.Parity             := aParity;
        ComPort.PortName := _Settings.PortName;
        ComPort.Open();
        if ComPort.IsOpen then
         OutLog('Port '+ComPort.PortName+' Open')
        else
         OutLog('was not possible open the port '+ComPort.PortName);
end;

method TSerialComm.ClosePort();
begin
    if ComPort.IsOpen then
    begin
      ComPort.DataReceived +=nil;
      ComPort.ReadExisting;
      ComPort.Close();
    end;    
end;

method TSerialComm.Init;
begin 
 ComPort.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
end;

注1:OutLog只是一个记录函数,您可以注释对此函数的调用。

注2:此代码必须从服务器端运行。

Instead of looking for a Delphi prism component for Serial Port , you must look for an .Net Serial Port component. You can use this article SerialPort (RS-232 Serial COM Port) in C# .NET as guide about this topic.

Now check this delphi prism class which handle the Serial Port communication (only receive data)

namespace SerialComm;

interface

uses
  System.IO.Ports, //this namespace contains classes for controlling serial ports.      
  System.Collections.Generic,
  System.Linq,
  System.Text;

type
  DataMode = (Text,Hex);

  TSerialComm = public class
  private
    CurrentDataMode     : DataMode;
    method port_DataReceived( sender : object;  e : SerialDataReceivedEventArgs);
  public
    ComPort             : SerialPort := new SerialPort();
    method OpenPort();
    method ClosePort();
    method Init;
  end;

implementation

//here you receive the data
method TSerialComm.port_DataReceived( sender : object;  e : SerialDataReceivedEventArgs);

    method  ByteArrayToHexString(data : Array of Byte) : string;
    Begin
      var sb : StringBuilder := new StringBuilder(data.Length * 3);
      for each b in data do 
      sb.Append(Convert.ToString(b, 16).PadLeft(2, '0').PadRight(3, ' '));
      result:=sb.ToString().ToUpper();
    end;

Var
  data :string ;
begin

    if not ComPort.IsOpen then  exit;

    try
              // text mode
              if (CurrentDataMode = DataMode.Text) then
              begin                                    
                   data  := comport.ReadExisting();    
             //do your stuff here 
              end
              else
              ///binary (hex) mode
              begin
                var bytes : Integer  := ComPort.BytesToRead;
                var buffer : Array of byte  := new byte[bytes];
                ComPort.Read(buffer, 0, bytes);
                Data:= ByteArrayToHexString(buffer);
                //do your stuff here 
              end;

    except
      on ex: exception do begin         
          OutLog('port_DataReceived: ' + ex.Message + ' ** Trace: ' + ex.StackTrace);
         exit;
      end;
    end;
end;

method TSerialComm.OpenPort();
begin
      CurrentDataMode := DataMode.Text;
      OutLog('Open Port COM');
      if ComPort.IsOpen then ClosePort();       
       ComPort.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived); 

        //ComPort.ReadTimeout:=100;
        ComPort.BaudRate := Convert.ToInt32(_Settings.BaudRate);
        ComPort.DataBits := Convert.ToInt32(_Settings.DataBits);
        var  aStopBits : StopBits  := StopBits(Enum.Parse(typeof(StopBits), _Settings.StopBits, true));
        ComPort.StopBits           := aStopBits;
        var  aParity : Parity      := Parity(Enum.Parse(typeof(Parity), _Settings.Parity, true));
        ComPort.Parity             := aParity;
        ComPort.PortName := _Settings.PortName;
        ComPort.Open();
        if ComPort.IsOpen then
         OutLog('Port '+ComPort.PortName+' Open')
        else
         OutLog('was not possible open the port '+ComPort.PortName);
end;

method TSerialComm.ClosePort();
begin
    if ComPort.IsOpen then
    begin
      ComPort.DataReceived +=nil;
      ComPort.ReadExisting;
      ComPort.Close();
    end;    
end;

method TSerialComm.Init;
begin 
 ComPort.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
end;

Note 1 : the OutLog is just a function to log , you can comment the calls to this function.

Note 2 : This code must run from the server side.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文