我自己写了个小程序,通过串口让电脑来读取机器上过来的测试铜线的数据,当机器测试完成后按一个键数据就可以传到我的程序并显示出来,但是当机器进行第2次测试的时候数据就传不过来了,怎么按键都没有反映,要我重启电脑后又可以接收该次的数据.就这样要重启一次才能接收一次数据,但是我把我的电脑的2个串口对接后来测试我的程序时,通信都是很正常的。请问这是什么原因,是串口的设置问题还是什么?请大家帮忙,谢谢!
解决方案 »
- 现在datagridview中得列都是文本框,如何只给一列加上只能提示呢
- winform 谁会重写datatimepicker控件?
- WinFrom 屏蔽右键
- 随即加载.txt里面的内容(最后分求,天啊搞不定的话要炒鱿鱼了)
- 跪求大侠帮忙:updatepanel使用后怎么重复出现原来的控件
- C#/为记事本中数据标序
- 版主呀,帮帮忙! 关于Graphics对象(答对马上结帖)
- C# VS2005调试问题求助
- C#调用MediaPlayer出现的奇怪问题
- 给一个正则表达式讲的比较好的网站.
- 如何取出用openFileDialog选定的文件名?不含路径?
- vs2005中ReportViewer的用法(Web)
{
// This method will be called when there is data waiting in the port's buffer // Determain which mode (string or binary) the user is in
if (CurrentDataMode == DataMode.Text)
{
// Read all the data waiting in the buffer
string data = comport.ReadExisting(); // Display the text to the user in the terminal
Log(LogMsgType.Incoming, data);
}
else
{
// Obtain the number of bytes waiting in the port's buffer
int bytes = comport.BytesToRead; // Create a byte array buffer to hold the incoming data
byte[] buffer = new byte[bytes]; // Read the data from the port and store it in our buffer
comport.Read(buffer, 0, bytes); // Show the user the incoming data in hex format
Log(LogMsgType.Incoming, ByteArrayToHexString(buffer));
}
}
在第二次会不会是0
最有可能的是计算机处理com通讯问题
看来要去换电脑看看了。
2.看看你的发送接收缓冲区是单字节接收还是每次全部接收,如果是全部接收的话是否把临时变量清楚干净了。
3.看看接收事件是否触发,或判断串口的ctr状态是否可以发送状态或有其他异常
4.接收到的数据是否有乱码?一般情况下是有乱码容易引起接收异常
因为不同的组件,清除缓冲区方法不一样。
象mscomm控件,调用 input方法获取数据后,它自动将读取的数据从缓冲区清除。
详细代码请看http://showlie.blogcn.com