使用MSComm读串口,发送的时候是无握手的设置,刚开始的时候使用无握手的方式接收能正常接收.
最近使用无握手的方式接收到的是乱码,使用任意一种握手协议都能正常接收到数据,但是接收一段时间之后
就不触发接收事件了,代码如下,大家帮我分析下:
private bool InitComPort()
{
//通讯端口
com.CommPort = short.Parse(tbComPort.Text.Substring(3,tbComPort.Text.Length-3).Trim()); if (com.PortOpen)
return false;
string baudrate = txtBOT.Text;
//
com.RThreshold = 1;
//设置通讯端口
com.Settings = baudrate+",N,8,1";
//
com.DTREnable = true;
////
//com.RTSEnable =true;
////这里是设定握手协议
com.Handshaking=HandshakeConstants.comRTSXOnXOff;
//
com.InputMode = MSCommLib.InputModeConstants.comInputModeText;
//
com.InputLen = 0;
//
com.NullDiscard = false;
// com.OnComm += new MSCommLib.DMSCommEvents_OnCommEventHandler(this.OnComm);
//
com.PortOpen = true;
return true;
}
private void OnComm()
{
System.Threading.Thread.Sleep(1000);
a = com.Input.ToString();
//rtbComMsg.AppendText((string)com.Input);
string f = a.Trim().Substring(0, 1);
if (f == "$")
{
if (b == null)
{
b = a;
}
else
{
string bb = b.Trim().Replace("\r\n", "|");
//string[] aaa = b.Trim().Split(new char[2] { '\r', '\n' });
string[] aaa = bb.Split('|');
if (aaa.Length % 2 == 1)
{
b = aaa[aaa.Length - 1] = "\r\n" + a; for (int i = 0; i < aaa.Length - 1; i = i + 2)
{
//对记录进行处理
rtbComMsg.AppendText(aaa[i] + "\r\n");
rtbComMsg.AppendText(aaa[i + 1] + "\r\n");
if (aaa[i].Substring(0, 1) == "$")
{
resolv(aaa[i], aaa[i + 1]); } }
}
else
{
for (int i = 0; i < aaa.Length; i = i + 2)
{
//对记录进行处理
rtbComMsg.AppendText(aaa[i] + "\r\n");
rtbComMsg.AppendText(aaa[i + 1] + "\r\n");
if (aaa[i].Substring(0, 1) == "$")
{
resolv(aaa[i], aaa[i + 1]); }
}
b = null;
}
}
//MessageBox.Show(b);
}
else
{ //if (b != null)
//{
b = b + a;
//MessageBox.Show(d);
//}
}
}
最近使用无握手的方式接收到的是乱码,使用任意一种握手协议都能正常接收到数据,但是接收一段时间之后
就不触发接收事件了,代码如下,大家帮我分析下:
private bool InitComPort()
{
//通讯端口
com.CommPort = short.Parse(tbComPort.Text.Substring(3,tbComPort.Text.Length-3).Trim()); if (com.PortOpen)
return false;
string baudrate = txtBOT.Text;
//
com.RThreshold = 1;
//设置通讯端口
com.Settings = baudrate+",N,8,1";
//
com.DTREnable = true;
////
//com.RTSEnable =true;
////这里是设定握手协议
com.Handshaking=HandshakeConstants.comRTSXOnXOff;
//
com.InputMode = MSCommLib.InputModeConstants.comInputModeText;
//
com.InputLen = 0;
//
com.NullDiscard = false;
// com.OnComm += new MSCommLib.DMSCommEvents_OnCommEventHandler(this.OnComm);
//
com.PortOpen = true;
return true;
}
private void OnComm()
{
System.Threading.Thread.Sleep(1000);
a = com.Input.ToString();
//rtbComMsg.AppendText((string)com.Input);
string f = a.Trim().Substring(0, 1);
if (f == "$")
{
if (b == null)
{
b = a;
}
else
{
string bb = b.Trim().Replace("\r\n", "|");
//string[] aaa = b.Trim().Split(new char[2] { '\r', '\n' });
string[] aaa = bb.Split('|');
if (aaa.Length % 2 == 1)
{
b = aaa[aaa.Length - 1] = "\r\n" + a; for (int i = 0; i < aaa.Length - 1; i = i + 2)
{
//对记录进行处理
rtbComMsg.AppendText(aaa[i] + "\r\n");
rtbComMsg.AppendText(aaa[i + 1] + "\r\n");
if (aaa[i].Substring(0, 1) == "$")
{
resolv(aaa[i], aaa[i + 1]); } }
}
else
{
for (int i = 0; i < aaa.Length; i = i + 2)
{
//对记录进行处理
rtbComMsg.AppendText(aaa[i] + "\r\n");
rtbComMsg.AppendText(aaa[i + 1] + "\r\n");
if (aaa[i].Substring(0, 1) == "$")
{
resolv(aaa[i], aaa[i + 1]); }
}
b = null;
}
}
//MessageBox.Show(b);
}
else
{ //if (b != null)
//{
b = b + a;
//MessageBox.Show(d);
//}
}
}
解决方案 »
- datagridview 编辑 单元格后怎样保存?
- openxml2.0中的 BestFit 如何使用
- 如何实现财务软件中的帐套备份?
- 请大侠们帮帮手,关于网络字节序的问题(HostToNetworkOrder、NetworkToHostOrder)
- C# Directx编程
- 关于datagridview
- 完了完了我刚安装 Windos2003Server,再安装VS.net1.1 现在不能新建也不能打开web应用程序和web服务
- 在线诚问:哪位大虾能提供下列环境下c#编写的查询界面源代码(例子)?
- 关于网络开发中同步和异步理解的入门问题
- 怎样检测一个表是否存在?
- 警报处理
- 公司想在dev_GridView上做这样的效果!
http://blog.csdn.net/wuyazhe/article/category/695097可以看看