[Mono-dev] Mono C# Serial Port problem
buhochileno at gmail.com
buhochileno at gmail.com
Thu May 15 10:03:43 EDT 2008
Alan_Chun wrote:
> 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):
>
>
Hi Alan, cna you test this code in linux? (using of course /dev/ttyS0 or
the correct serial device), I use lot of serial communications with mono
inlinux and work ok, I don't know the current status of mono serialport
in windows...also be sure to use kernel 2.6.24 or up in linux..
> 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:get_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
>
More information about the Mono-devel-list
mailing list