c# 循环com,分别对串口写入与读取,获取需要的串口信息

private SerialPort serialPort;//串口对象类

        private string strPortDataReceived = "";//记录串口返回值
        private string serialPortName = "";//记录端口号

        private bool isGet = false;


        /// <summary>
        /// 获取产品设备号
        /// </summary>
        /// <returns>设备号</returns>
        public List<string> GetProductNumBySerialPort()
        {
            List<string> productNumList = new List<string>();
            string[] ports = SerialPort.GetPortNames();
            if (ports.Length > 0)
            {
                for (int i = 0; i < ports.Length; i++)
                {
                    string portName = ports[i];

                    serialPort = new SerialPort(portName, 115200, Parity.None, 8, StopBits.One);
                    //serialPort.DataReceived += new SerialDataReceivedEventHandler(serialPort_DataReceived);//DataReceived事件委托
                    serialPort.ReceivedBytesThreshold = 1;
                    serialPort.RtsEnable = true;
                    serialPort.WriteTimeout = 500;
                    serialPort.ReadTimeout = 500;
                    try//这里写成异常处理的形式以免串口打不开程序崩溃
                    {
                        serialPort.Open();
                    }
                    catch { }
                    if (serialPort.IsOpen)
                    {
                        //byte[] ar = StringToByteHelper.HexStringToByteArray("1F1109");
                        byte[] WriteBuffer = new byte[] { 0x1F, 0x11, 0x09 };
                        try
                        {
                            serialPort.Write(WriteBuffer, 0, WriteBuffer.Length);//获取设备编号
                        }
                        catch (Exception ex)
                        {
                            string str = ExceptionClass.GetExceptionMsg(ex, "串口读写超时"+ "\r\n");
                            ExceptionClass.WriteException(str);
                        }//Thread.Sleep(100);
                        int r = 0;
                        //读取返回数据
                        while (serialPort.BytesToRead == 0)
                        {
                            r++;
                            Thread.Sleep(1);
                            if (r >= 500)
                            {
                                break;
                            }
                        }
                        Thread.Sleep(50); //50毫秒内数据接收完毕,可根据实际情况调整
                        r = 0;
                        byte[] recData = new byte[serialPort.BytesToRead];
                        serialPort.Read(recData, 0, recData.Length);
                        string strPortDataReceived = System.Text.Encoding.Default.GetString(recData);
                        Console.WriteLine(strPortDataReceived);
                        if (strPortDataReceived.Length >= 17)
                        {
                            productNumList.Add(strPortDataReceived.Substring(2, 4));
                        }
                        serialPort.Close();//关闭该端口
                    }
                    else
                    {
                        //MessageBox.Show("串口打开失败!");
                        //return false;
                    }
                }
            }
            else
            {
                //MessageBox.Show("没有可用的串口!");    
            }
            return productNumList;
        }

猜你喜欢

转载自www.cnblogs.com/xunyiHe/p/10538794.html