HI, currently I am porting a windows app to linux using Mono. I have some
difficulties to get the serial port work. here is the code(based on mono
website):
public static void Main(string[] args)
{
SerialPortTest myTest = new SerialPortTest();
myTest.Test();
}
private SerialPort mySerial;
// Constructor
public SerialPortTest()
{
}
public void Test()
{
Console.WriteLine("Begin");
if (mySerial != null)
if (mySerial.IsOpen)
mySerial.Close();
mySerial = new System.IO.Ports.SerialPort("COM3", 9600, Parity.Even,
7, StopBits.One);
mySerial.Open();
mySerial.DtrEnable = true;
mySerial.RtsEnable = true;
mySerial.Handshake = System.IO.Ports.Handshake.None;
mySerial.ReadTimeout = 400;
Thread t = new Thread(new ThreadStart(DataReceived));
t.Start();
string tmp = "5D.1";
SendData(tmp);
// Should output some information about your modem firmware
//Console.WriteLine("Baaa");
// Console.WriteLine(ReadData());
}
public void DataReceived()
{
while (true)
{
byte tmpByte = 255;
string rxString = "";
//int tot = mySerial.BytesToRead;
tmpByte = (byte)mySerial.ReadByte();
while (tmpByte != 255)
{
rxString += ((char)tmpByte);
mpByte = (byte)mySerial.ReadByte();
}
if (!string.IsNullOrEmpty(rxString))
Console.WriteLine(rxString);
}
//return rxString;
}
I ran this under windows using .netframwork run time, it is fine but under
mono I always got this exception:
Unhandled Exception: System.NullReferenceException: Object reference not set
to an instance of an object
at (wrapper managed-to-native)
System.IO.Ports.WinSerialStream:ClearCommError
(int,System.IO.Ports.CommStat&)
at System.IO.Ports.WinSerialStream.get_BytesToRead () [0x00000]
at System.IO.Ports.SerialPort.get_BytesToRead () [0x00000]
at (wrapper remoting-invoke-with-check)
System.IO.Ports.SerialPort

et_BytesToRead ()
at SerialPortTest.DataReceived () [0x00000]
I have also tried BytesToRead, Read, ReadByte, all of them are working fine
under windows using .net2.0 framwork. But once I use the mono runtime, the
nightmare begins.
Any help will be really apprecaited!
Thanks
========================================================
In Linux to use a device the user who requires access should have the
permission to use it.
So you must verify that the user starts the application is registered in the
group owner of ttysN.
To find out the group owner of the device by a shell run the command below:
=========================================================
I haven't tried it under linux yet. What I meant is when I ran the
exeutable, if I use "mono test.exe" through windows xp's command line, it
will generate this error. However, if ran this exe directly (use "test.exe"
only on the command line without mono), it worked fine.
Any ideas?
========================================================
Hi,
We also have problems with the serial port under OSX. We had to re-implement most of the serial port unsing posix call.
Can you actually get any data out of the port ?
Shawn Schaerer
Director of Research and Development
Cogmation Robotics Inc
www.cogmation.com
=================================================
Shwn: Do you have this reimlemenation of serial n OsX on the net? is
it free software?
==================================================================
Hi,
No we do not have it on the web, but we are willing to put it up and or submit it to the mono-project.
Shawn Schaerer
Director of Research and Development
Cogmation Robotics Inc
www.cogmation.com
==================================================
Ok, it will be really useeful for us because we are working on porting
our program to OSX without success.
Please, let me know when you publish it
Thanks
Thanks
Thanks
ls-al / dev / ttyS *