串行端口与线程的通信,在monodevelop IDE中,使用C#

时间:2013-03-11 01:59:02

标签: c# thread-safety serial-port monodevelop

我的步骤: 1.使用System.IO.Ports;使用system.Threading;

2.SerialPort mySerialPort = new SerialPort(“Com1”,9200,Parity.None,8,StopBits.One); 3.mySerialPort.Open(); // 异常捕获:抛出System.IO.IOException:没有此类文件或目录。

4.myThread = new Thread(new ParameterizedThreadStart(ReadPortThread)); 5.myThread.Start(mySerialPort);

6.ReadPortThread(SerialPort serialPort()//从serialport接收数据。

希望你对这个问题提出一些建议。

1 个答案:

答案 0 :(得分:0)

我还在进行serialPort通信,而我的工作也是如此:

            ///<summary>
            ///creating new serialPort, fill it with a portname and give it BaudRate value 9600.
            ///</summary>
            _serialPort = new SerialPort();
            _serialPort.PortName = portname;
            _serialPort.BaudRate = 9600;
            _serialPort.Parity = Parity.None;
            _serialPort.StopBits = StopBits.One;

            try
            {
                if (!_serialPort.IsOpen)
                {
                    //open the port.
                    _serialPort.Open();
                }
                //when the port is open
                if (_serialPort.IsOpen)
                {          
                    _serialUsbThread = new Thread(SerialWorker);
                    _serialUsbThread.Priority = ThreadPriority.Highest;
                    _serialUsbThread.Start();
                }   
            }
            catch (Exception ex)
            {
                Debug.WriteLine(DateTime.UtcNow + " SERIAL: " + ex.Message);
            }