Tuesday, July 5, 2011

Serial Communication (RS232) in LINUX

I am currently working with Delphi Prism for Mono development. So, the software can be cross-platform mainly to be run on Linux OS. 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. It seems that there is no library or .NET framework that supports Linux environment after searching the Internet and Stackoverflow. Although there are some similar questions that were asked by others in Stackoverflow, the answers don't really show any sample. I am kind of stuck.

Here is the code I wrote for Mono serial comm. 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." I can't understand why. Is this even possible to do for Mono?
I need to get access to the serial port on Linux for RS232 communication.
Thanks,

Answer:
The property ParityReplace is currently not implemented. 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. However I have also to find an idea how to unit test it ;)

No comments: