串口通讯,使用serialPort控件
首先通过SerialPort.GetPortNames()得到串口列表
选择一个串口,发送数据,我是自己发自己去读当前选择的串口
界面上有发送按钮,读取是SerialPort的数据到达事件
private void buttonSend_Click(object sender, EventArgs e)
{
strSend = textBoxSend.Text.Trim(); byte[] SendByte=new byte[9];
//SendByte=Encoding.Default.GetBytes(textBoxSend.Text.Trim());
SendByte = strToHexByte(strSend); //字符串转16进制函数
if (serialPort1.IsOpen)
{
if (checkBoxTimer.Checked)
{
timer1.Interval = Convert.ToInt32(textBoxTimer.Text.Trim());
//timer1.Tick += new EventHandler(timer1_Tick);
timer1.Start();
}
else
{
serialPort1.Write(SendByte, 0, SendByte.Length); //在这里SendByte是发送的内容没错
//serialPort1.WriteLine(strSend);
}
}
else
{
SetTipText("请打开串口!\r\n");
}
} private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
try
{
if (serialPort1.IsOpen)
{
if (serialPort1.BytesToRead < 9)
{
SetTipText("计算机时间:" + DateTime.Now.ToLocalTime() + "串口" + serialPort1.PortName + "数据长度<9错误\r\n");
}
else
{
try
{
serialPort1.Read(data, 0, serialPort1.BytesToRead); //这里读取结果错误
SetReadText(byteToHexStr(data));
//SetReadText(serialPort1.ReadLine());
}
catch (Exception ex)
{
SetTipText("计算机时间:" + DateTime.Now.ToLocalTime() + "串口" + serialPort1.PortName + ex.ToString() + "\r\n");
serialPort1.Close();
SetButtonEnable(buttonOpen);
return;
}
}
}
else
{
SetTipText("串口" + serialPort1.PortName + "已经关闭");
return;
}
}
catch (Exception ex)
{
// MessageBox.Show(ex.ToString());
return;
}
}例子:
发生数据 :00ff810A071E082614
接收数据:00FF810A071E0820082614
中间莫名的多个2008,使用readline函数同样结果
大家遇到过同样问题没
串口助手发送数据自己接收没有问题
首先通过SerialPort.GetPortNames()得到串口列表
选择一个串口,发送数据,我是自己发自己去读当前选择的串口
界面上有发送按钮,读取是SerialPort的数据到达事件
private void buttonSend_Click(object sender, EventArgs e)
{
strSend = textBoxSend.Text.Trim(); byte[] SendByte=new byte[9];
//SendByte=Encoding.Default.GetBytes(textBoxSend.Text.Trim());
SendByte = strToHexByte(strSend); //字符串转16进制函数
if (serialPort1.IsOpen)
{
if (checkBoxTimer.Checked)
{
timer1.Interval = Convert.ToInt32(textBoxTimer.Text.Trim());
//timer1.Tick += new EventHandler(timer1_Tick);
timer1.Start();
}
else
{
serialPort1.Write(SendByte, 0, SendByte.Length); //在这里SendByte是发送的内容没错
//serialPort1.WriteLine(strSend);
}
}
else
{
SetTipText("请打开串口!\r\n");
}
} private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
try
{
if (serialPort1.IsOpen)
{
if (serialPort1.BytesToRead < 9)
{
SetTipText("计算机时间:" + DateTime.Now.ToLocalTime() + "串口" + serialPort1.PortName + "数据长度<9错误\r\n");
}
else
{
try
{
serialPort1.Read(data, 0, serialPort1.BytesToRead); //这里读取结果错误
SetReadText(byteToHexStr(data));
//SetReadText(serialPort1.ReadLine());
}
catch (Exception ex)
{
SetTipText("计算机时间:" + DateTime.Now.ToLocalTime() + "串口" + serialPort1.PortName + ex.ToString() + "\r\n");
serialPort1.Close();
SetButtonEnable(buttonOpen);
return;
}
}
}
else
{
SetTipText("串口" + serialPort1.PortName + "已经关闭");
return;
}
}
catch (Exception ex)
{
// MessageBox.Show(ex.ToString());
return;
}
}例子:
发生数据 :00ff810A071E082614
接收数据:00FF810A071E0820082614
中间莫名的多个2008,使用readline函数同样结果
大家遇到过同样问题没
串口助手发送数据自己接收没有问题
这个方法看会不会有问题。
我打开同一个串口,比如Com3,点击发送按钮就往Com3发数据,SerialPort的数据到达事件处理数据读取
就是说读的也是Com3口的数据
不知道什么原因造成的这种错误拿串口助手打开比如Com3口,自己发自己收都没问题
正常接收
就是发送00ff810A08050C0500这样数据的时候出现接收中间出现2008
NND,真邪门了
波特率9600,偶校验,8位数据位,1位停止位
谁有心情的可以测测,这样开头的数据,要自己的程序打开串口,自己的程序去接收这个串口的数据
看看会不会出现我说的情况
以前用VB做这个的时候就发现过发送某个规律的数据,他就会出错。忘了是什么数据,即使用两台电脑用串口调试助手对发也是出错的。
http://download.cnet.com/ComTrace/3000-2218_4-75156892.html?tag=mncol感觉好别忘了给个review(评论),谢谢!