[Mono-dev] Mono C# Serial Port problem

Leszek Ciesielski skolima at gmail.com
Tue Jun 10 11:37:06 EDT 2008


The RecievedEvent is not implemented (check the exception...). If you
need to monitor the serial port for incoming data, create a separate
thread for handling the port and use the blocking Read methods.
Remember not to use the port outside the thread that has created it.

Please note that MSDN states that the SerialPort class is not
thread-safe, then happily ignores this and keeps tossing it between
threads in every example.

On Tue, Jun 10, 2008 at 1:42 PM, PaceyIV <paceynet at gmail.com> wrote:
>
> Uhm...
>
> Now I try the code below. It works on Microsoft Visual Studio, but not in
> mono on Ubuntu.
> It compile it, but when I try to run it I get this error:
>
> Unhandled Exception: System.NotImplementedException: The requested feature
> is not implemented.
>  at System.IO.Ports.SerialPort.set_ReceivedBytesThreshold (Int32 value)
> [0x00012] in
> /build/buildd/mono-1.2.6+dfsg/mcs/class/System/System.IO.Ports/SerialPort.cs:410
>  at (wrapper remoting-invoke-with-check)
> System.IO.Ports.SerialPort:set_ReceivedBytesThreshold (int)
>  at ConsoleApplication1.Program.Main (System.String[] args) [0x0004f] in
> /home/paceyiv/programmazione/mono/Microsoft/Microsoft/Main.cs:29
>
> So I comment the line!
> I think don't work the SerialDataReceivedEventHandler. I can write code to
> read the byte from serial and the device respond, but the Serial Data
> Received Event newer works on mono.
>
> What's wrong?
>
> using System;
> using System.Collections.Generic;
> //using System.Linq;
> using System.Text;
> using System.IO;
> using System.IO.Ports;
> using System.Threading;
>
> namespace ConsoleApplication1
> {
>    class Program
>    {
>        static System.IO.Ports.SerialPort rs232;
>
>        static string dati;
>
>        static void Main(string[] args)
>        {
>            // Setup RS-232
>                        rs232 = new SerialPort("/dev/ttyUSB0", 9600);
>                        // for Win
>            //rs232 = new SerialPort("COM3", 9600);
>            rs232.BaudRate = 9600;
>            rs232.Parity = Parity.None;
>            rs232.DataBits = 8;
>            rs232.StopBits = StopBits.One;
>            rs232.Handshake = Handshake.None;
>
>                        //rs232.ReadTimeout = 2000;
>                        //rs232.ReadBufferSize = 1024;
>            //rs232.ReceivedBytesThreshold = 1; // This don't work in mono!
>            rs232.DataReceived +=
>                new SerialDataReceivedEventHandler(rs232_DataReceived); //
> This don't work in mono!
>
>            // Open SerialPort
>                        try
>                        {
>                                rs232.Open();
>                                // Do I really need this?
>                                //rs232.DiscardInBuffer();
>                                //rs232.DiscardOutBuffer();
>
>                                // Dealy for my specific device
>                                Thread.Sleep(1500);
>                        }
>                        catch (System.IO.IOException ioe)
>                        {
>                                Console.Write(ioe.Message);
>                                Environment.Exit(1);
>                        }
>
>                        // survive to Microsoft :)
>                        //CheckForIllegalCrossThreadCalls = false;
>
>                        // Send command to read some date from the device
>            rs232.WriteLine("A");
>
>            string m;
>            bool continua = true;
>            while (continua)
>            {
>                m = Console.ReadLine();
>                if (m=="q")
>                {
>                    continua = false;
>                }
>            }
>
>        }
>
>        static void rs232_DataReceived(object sender,
> SerialDataReceivedEventArgs e)
>        {
>            dati += rs232.ReadExisting();
>            if (dati.Contains("$"))
>            {
>                Console.WriteLine(dati);
>            }
>        }
>    }
> }
>
>
> --
> View this message in context: http://www.nabble.com/Mono-C--Serial-Port-problem-tp17241892p17753378.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
>



-- 
MS-DOS user since 5.0
Windows user since 3.11
Linux user since kernel 2.4
Novell Netware user since 2.2
WARCRAFT user since 1.0


More information about the Mono-devel-list mailing list