[Mono-dev] Mono C# Serial Port problem

Panoramix aria.francesco at gmail.com
Thu May 15 03:44:06 EDT 2008


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:
 
ls-al / dev / ttyS *




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):
> 
>  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  
> 


-----
:working:
-----------------------------------------------------------------------------
http://freeflow.awardspace.com http://freeflow.awardspace.com 
-----------------------------------------------------------------------------

-- 
View this message in context: http://www.nabble.com/Mono-C--Serial-Port-problem-tp17241892p17247791.html
Sent from the Mono - Dev mailing list archive at Nabble.com.



More information about the Mono-devel-list mailing list