问题的描述是这样的:我写了一个程序,但是缺少一个模块,就是用来检测com串口通讯是否正常工作的,而且这个检测一定要在程序运行的过程中,不断的检测。

解决方案 »

  1.   

    我以前写了一个读写串的程序 ,我是在读或是写的时候检查串口是否能正常通信的,其它不用专门检测的!
    if (sb.ToString().Length== 0)
                    {
                        nReadCount++;
                    }                if (nReadCount == 3)
                    {
                        nReadCount = 0;
                        throw new Exception("设置不正确或没有联接设备!");
                    }           
    如果发了命令后,连续三次读不到数据的话程序就认为是通信失败的!     
       #region Write command to OPW
            /// <summary>
            /// 发操作命令给OPW设备
            /// 并返回状态
            /// </summary>
            /// <param name="sCommand"></param>
            /// <returns></returns>
            public string WriteCommand(string sCommand)
            {
                StringBuilder sb = new StringBuilder();
                bool bRead = true;
                try
                {
                    ss_port.DiscardInBuffer();
                    ss_port.Write(sCommand);
                    Thread.Sleep(1500);
                    while (bRead)
                    {
                        _ReadBuffer = new byte[ss_port.BytesToRead];
                        ss_port.Read(_ReadBuffer, 0, _ReadBuffer.Length);
                        sb.Append(Encoding.ASCII.GetString(_ReadBuffer));
                        Thread.Sleep(500);
                        if (ss_port.BytesToRead <= 0)
                        {
                            bRead= false;
                        }
                    }
                    if (sb.ToString().Length== 0)
                    {
                        nReadCount++;
                    }                if (nReadCount == 3)
                    {
                        nReadCount = 0;
                        throw new Exception("设置不正确或没有联接设备!");
                    }               
                }
                catch (Exception ex)
                {
                    throw new Exception("从设备获取数据失败!\r\n错误信息:" + ex.Message);
                }
                return sb.ToString(); ;
            }        public string WriteCommand(byte[] bCommand)
            {
                StringBuilder sb = new StringBuilder();
                bool bRead = true;
                try
                {
                    ss_port.DiscardInBuffer();
                    ss_port.Write(bCommand,0,bCommand.Length);
                    Thread.Sleep(1500);
                    while (bRead)
                    {
                        _ReadBuffer = new byte[ss_port.BytesToRead];
                        ss_port.Read(_ReadBuffer, 0, _ReadBuffer.Length);
                        sb.Append(Encoding.ASCII.GetString(_ReadBuffer));
                        Thread.Sleep(500);
                        if (ss_port.BytesToRead <= 0)
                        {
                            bRead = false;
                        }
                    }
                    if (sb.ToString().Length == 0)
                    {
                        nReadCount++;
                    }                if (nReadCount == 3)
                    {
                        nReadCount = 0;
                        throw new Exception("设置不正确或没有联接设备!");
                    }               
                }
                catch (Exception ex)
                {
                    throw new Exception("从设备获取数据失败!\r\n错误信息:" + ex.Message);
                }
                return sb.ToString();
            }
            #endregion
      

  2.   

    如果你用的是C#下的SerialPort控件的话,在发送数据前判断一下serialPort1.CtsHolding属性时候可有发送就可以了。