[Mono-dev] Mono C# Serial Port problem

Xavi de Blas xaviblas at gmail.com
Thu May 15 20:13:09 EDT 2008


Hello, you can take a look at chronojump code. It uses serial port on
both platforms:

http://svn.gnome.org/viewvc/chronojump/trunk/src/chronopic.cs?revision=383&view=markup
http://svn.gnome.org/viewvc/chronojump/trunk/src/chronojump_mini.cs?revision=364&view=markup

It's a pity this two parts are not written/commented in english

Luck


2008/5/15 Alan_Chun <kokolee88 at hotmail.com>:
>
> 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: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
> --
> View this message in context: http://www.nabble.com/Mono-C--Serial-Port-problem-tp17241892p17241892.html
> Sent from the Mono - Dev mailing list archive at Nabble.com.
>
> _______________________________________________
> Mono-devel-list mailing list
> Mono-devel-list at lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-devel-list
>


More information about the Mono-devel-list mailing list