[Mono-dev] Mono C# Serial Port problem

Alan_Chun kokolee88 at hotmail.com
Wed May 14 18:02:17 EDT 2008


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.



More information about the Mono-devel-list mailing list