简体   繁体   English

LINUX 中的串行通信 (RS232)

[英]Serial Communication (RS232) in LINUX

I am currently working with Delphi Prism for Mono development.我目前正在使用 Delphi Prism 进行 Mono 开发。 So, the software can be cross-platform mainly to be run on Linux OS.因此,该软件可以跨平台,主要运行在Linux操作系统上。 To get started, I have been implementing and test running the basic functions of my software - Serial and Network Communication.首先,我一直在实现和测试运行我的软件的基本功能 - 串行和网络通信。

But I can't seem to get passed serial communication implementation for Mono.但我似乎无法通过 Mono 的串行通信实现。 It seems that there is no library or .NET framework that supports Linux environment after searching the Internet and Stackoverflow.经过网上和 Stackoverflow 搜索,似乎没有支持 Linux 环境的库或 .NET 框架。 Although there are some similar questions that were asked by others in Stackoverflow, the answers don't really show any sample.尽管 Stackoverflow 中其他人提出了一些类似的问题,但答案并没有真正显示任何示例。 I am kind of stuck.我有点卡住了。

Here is the code I wrote for Mono serial comm.这是我为 Mono 串行通信编写的代码。 after visiting this one website.访问此网站后。

  {$IFDEF LINUX}
    if SerialPort1 = nil then
        SerialPort1 := new System.Io.Ports.SerialPort();
    SerialPort1.Close;

    SerialPort1.BaudRate:=19200;
    SerialPort1.DataBits:=8;
    SerialPort1.DtrEnable:=true;
    SerialPort1.Parity:=System.IO.Ports.Parity.Even;
    SerialPort1.PortName:="/dev/ttyS0";
    SerialPort1.ParityReplace:=63;
    SerialPort1.ReadBufferSize:=4096;
    SerialPort1.ReadTimeout:=1000;
    SerialPort1.RtsEnable:=true;
    SerialPort1.StopBits:=System.IO.Ports.StopBits.One;
    SerialPort1.WriteTimeout:=1000;
    SerialPort1.Open;

    while (true) do
    begin
        CommByte[0]:=$FF;
        CommByte[1]:=$04;
        CommByte[2]:=$04;
        CommByte[3]:=thechannel;
        CommByte[4]:=mcommand;
        CommByte[5]:=(CommByte[2] xor CommByte[3] xor CommByte[4]);

        SerialPort1.Write(CommByte,0,6);
        while SerialPort1.BytesToWrite>0 do;
        Thread.Sleep(10);
        Application.DoEvents;
    end;
{$ENDIF}

But everytime I run this code under Linux, Mono raises a message box with "The requested feature is not implemented."但是每次我在 Linux 下运行此代码时,Mono 都会引发一个消息框,其中显示“请求的功能未实现”。 I can't understand why.我不明白为什么。 Is this even possible to do for Mono?这甚至可以为 Mono 做吗?

I need to get access to the serial port on Linux for RS232 communication.我需要访问 Linux 上的串行端口以进行 RS232 通信。

Thanks,谢谢,

The property ParityReplace is currently not implemented. ParityReplace属性当前未实现。 If it is not really needed, throw it away and you won't probably have this exception.如果它不是真的需要,就把它扔掉,你可能不会有这个例外。 And if you need it, tell me, it could be probably be done using PARMRK and changing mark to this byte.如果您需要它,请告诉我,它可以使用PARMRK并将标记更改为该字节来完成。 However I have also to find an idea how to unit test it;)但是,我还必须找到如何对其进行单元测试的想法;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM