0

我的步骤: 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() //从串口接收数据。

希望您对问题提出一些建议。

4

1 回答 1

0

我也在进行串行端口通信,我的工作是:

            ///<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);
            }         
于 2013-03-11T08:01:38.837 回答