C# 串口接收不到数据 请教 我用串口调试精灵往串口发送数据时侯能够接收到数据,但自己写的代码发送给串口数据后,没有任何数据返回。用CommMonitor监听下来的结果是正常情况IOCTL_SET_CHAR :Chars Eof: 26, Error: 0, break: 0, Event: 0, Xon: 17, Xoff: 19,我的代码Event:26。请教大家有没有人碰到过这些情况,该怎么解决,谢谢 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 这跟串口接收不到数据有关系吗? private void write_Click(object sender, EventArgs e) { mute.WaitOne(); int n = 0; try { com.DiscardOutBuffer(); com.DiscardInBuffer(); com.Write(new byte[] {0x04,0x33,0x31,0x72}, 0, 4); while (n < 104) { com.Write(new byte[] { 0x04, 0x33, 0x31, 0x05 }, 0, 4); n++; } } catch { MessageBox.Show("串口通讯错误"); } } private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e) { try { Thread.Sleep(100); int bytes = com.BytesToRead; byte[] buffer = new byte[bytes]; // 读取缓冲区的数据到数组 com.Read(buffer, 0, bytes); } // 显示读取的数据到数据窗口 catch { } } 我发送数据没有问题的,按理应该每发送一个数据,都能收到一个返回的数据,但是串口调试工具可以收到,我用received事件不行。 瞎写,首先发送的地方最好组成一个大数组,一次调用write,让wirte自己去分解,其次,data_received是中断触发后,触发线程调用的事件,这里不需要等待。 先确定下DataReceived事件已经注册了另外你用串口助手也是一次性发104组数据吗?在你的button_Click事件里,一下子发了好多组数据啊,这样能达到效果? SerialPort comPort;comPort = new SerialPort(cmbPort.Text, baudRate, tempParity, dataBits, stopBit); comPort.DataReceived += new SerialDataReceivedEventHandler(comPort_DataReceived); /// <summary> /// 串口接收 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void comPort_DataReceived(object sender, SerialDataReceivedEventArgs e) { if (ClosingPort) return; byte[] readBuffer = new byte[comPort.ReadBufferSize + 1]; int strLength = comPort.Read(readBuffer, 0, readBuffer.Length); recCount += strLength; string msg = ""; msg += recEncode.GetString(readBuffer, 0, strLength); if (rdoHex.Checked) msg = BaseHelper.ConvertToHex(msg, recEncode); if (ckNext.Checked) msg += "\r\n"; if (ckPauseRec.Checked)//暂停接收 return; this.Invoke(new MethodInvoker(delegate { if (!ckSave.Checked) txtRec.Text += msg; tsblRecCount.Text = recCount.ToString(); })); if (ckSave.Checked) { FileStream fs = new FileStream(saveFileName, FileMode.Append); fs.Write(readBuffer, 0, strLength); fs.Flush(); fs.Close(); } } 我也遇到同样问题:vb.net里面,commMonitor监测仪器厂家软件的IOCTL_SET_CHARS里面Event是0,但我自己写的软件Event=26,不知道怎么修改这个数值,还有IOCTL_SET_HANDFLOW里面的几个数值怎么修改?麻烦讲清楚一点 VS2008打开设计窗口提示:分析EntityName时出错。 C#控制台如何重定向输入输出流? 多线程UI卡死问题 读取的Excel放到dataset中的时候有些数据怎么丢失?, treeview 的事件 让窗体自动隐藏 有人在吗,myGridTextBoxColumn.Format = "D";D是什么意思? 重分之下,就不信这么简单的问题,没人会做??datagrid美化问题! 如何让两个字段同时排序 免费下载《使用 Microsoft .NET 的企业解决方案模式》一书! 求大虎大牛来解决关于C#大批量数据插入MySQL数据库的问题 如何使用C#生成json数据
private void write_Click(object sender, EventArgs e)
{
mute.WaitOne();
int n = 0;
try
{
com.DiscardOutBuffer();
com.DiscardInBuffer();
com.Write(new byte[] {0x04,0x33,0x31,0x72}, 0, 4);
while (n < 104)
{
com.Write(new byte[] { 0x04, 0x33, 0x31, 0x05 }, 0, 4);
n++;
}
}
catch
{
MessageBox.Show("串口通讯错误");
}
} private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
try
{
Thread.Sleep(100);
int bytes = com.BytesToRead;
byte[] buffer = new byte[bytes]; // 读取缓冲区的数据到数组 com.Read(buffer, 0, bytes);
}
// 显示读取的数据到数据窗口
catch { }
} 我发送数据没有问题的,按理应该每发送一个数据,都能收到一个返回的数据,但是串口调试工具可以收到,我用received事件不行。
另外你用串口助手也是一次性发104组数据吗?在你的button_Click事件里,一下子发了好多组数据啊,这样能达到效果?
SerialPort comPort;
comPort = new SerialPort(cmbPort.Text, baudRate, tempParity, dataBits, stopBit);
comPort.DataReceived += new SerialDataReceivedEventHandler(comPort_DataReceived);
/// <summary>
/// 串口接收
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void comPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
if (ClosingPort)
return;
byte[] readBuffer = new byte[comPort.ReadBufferSize + 1];
int strLength = comPort.Read(readBuffer, 0, readBuffer.Length);
recCount += strLength;
string msg = ""; msg += recEncode.GetString(readBuffer, 0, strLength);
if (rdoHex.Checked)
msg = BaseHelper.ConvertToHex(msg, recEncode);
if (ckNext.Checked)
msg += "\r\n";
if (ckPauseRec.Checked)//暂停接收
return;
this.Invoke(new MethodInvoker(delegate
{
if (!ckSave.Checked)
txtRec.Text += msg;
tsblRecCount.Text = recCount.ToString();
}));
if (ckSave.Checked)
{
FileStream fs = new FileStream(saveFileName, FileMode.Append);
fs.Write(readBuffer, 0, strLength);
fs.Flush();
fs.Close();
}
}