关于检测com串口通讯是否正常 问题的描述是这样的:我写了一个程序,但是缺少一个模块,就是用来检测com串口通讯是否正常工作的,而且这个检测一定要在程序运行的过程中,不断的检测。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我以前写了一个读写串的程序 ,我是在读或是写的时候检查串口是否能正常通信的,其它不用专门检测的!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 如果你用的是C#下的SerialPort控件的话,在发送数据前判断一下serialPort1.CtsHolding属性时候可有发送就可以了。 猴王算法 如何获取dataGridView里单元格的值,在线! 如何实现对用户自定义控件的键盘操作? 这个字符串怎么截取? 100分请教有关gif压缩问题 用C#如何访问pda的本地数据库(sdf格式) 一个数据库的小问题 如何修改word宏的安全级? 求C# 系统设计文档,及开发资料 水晶报表与数据库连接问题??? 如何实现弹出一个对话框,既能选择文件,也能选择文件夹?? 这个函数怎么快速知道结果
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