之前在网上看到大神用C#写了一个串口调试助手,自己也试了一下,就是发送的时候是对的(我设置的是16进制),但是接收到的和用串口程序接收到的不一样(我也是想接收16进制的数)请大神帮助看下~
[img=http://img.bbs.csdn.net/upload/201707/09/1499563146_195881.jpg][/img][img=http://img.bbs.csdn.net/upload/201707/09/1499563184_684768.png][/img]
发送A5 50时收到:
自己写的:3F3F3F3F3F3F043F3F0102C73F3FC13F839EF23FE69EF71F3F3D3F
串口调试助手的:A55A140000000424140102C7D296C1E8839EF2C0E69EF71F653D05
发送A5 52时收到:
自己写的:3F3F3F3F3F3F3F3F3F3F
串口调试助手的:A55A0300000006000000
发送A5 59时收到:
自己写的:3F3F043F3F3F15F2013F3F
串口调试助手的:A55A0400000015F201F900
[code=csharp]using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
namespace csharp串口通讯
{
public partial class Form1 : Form
{
SerialPort sp1;
public Form1()
{
InitializeComponent();
this.FormClosed += new FormClosedEventHandler(Form1_FormClosed);
if (sp1 == null)
{
Control.CheckForIllegalCrossThreadCalls = false; //这个类中我们不检查跨线程的调用是否合法(因为.net 2.0以后加强了安全机制,,不允许在winform中直接跨线程访问控件的属性)
sp1 = new SerialPort("COM8", 115200);
sp1.StopBits = StopBits.One;//
sp1.DataBits = 8;//
sp1.Parity = Parity.Even;//设置串口属性
sp1.Open();
sp1.DataReceived += new SerialDataReceivedEventHandler(sp1_DataReceived);
}
}
void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
if (sp1 != null && sp1.IsOpen)
{
sp1.Close();
}
}
///这一部分是发送部分,即在文本框中输入内容后,点击发送按钮,里面包含把内容转化为16进制,这部分应该是对的
private void btnSend_Click(object sender, EventArgs e)
{
if (!sp1.IsOpen) //如果没打开
{
MessageBox.Show("请先打开串口!", "Error");
return;
}
String strSend = txtSend.Text;
//处理数字转换
string sendBuf = strSend;
string sendnoNull = sendBuf.Trim();
string sendNOComma = sendnoNull.Replace(',', ' '); //去掉英文逗号
string sendNOComma1 = sendNOComma.Replace(',', ' '); //去掉中文逗号
string strSendNoComma2 = sendNOComma1.Replace("0x", ""); //去掉0x
strSendNoComma2.Replace("0X", ""); //去掉0X
string[] strArray = strSendNoComma2.Split(' ');
int byteBufferLength = strArray.Length;
for (int i = 0; i < strArray.Length; i++ )
{
if (strArray[i]=="")
{
byteBufferLength--;
}
}
// int temp = 0;
byte[] byteBuffer = new byte[byteBufferLength];
int ii = 0;
for (int i = 0; i < strArray.Length; i++) //对获取的字符做相加运算
{
Byte[] bytesOfStr = Encoding.Default.GetBytes(strArray[i]);
int decNum = 0;
if (strArray[i] == "")
{
//ii--; //加上此句是错误的,下面的continue以延缓了一个ii,不与i同步
continue;
}
else
{
decNum = Convert.ToInt32(strArray[i], 16); //atrArray[i] == 12时,temp == 18
}
try //防止输错,使其只能输入一个字节的字符
{
byteBuffer[ii] = Convert.ToByte(decNum);
}
catch (System.Exception ex)
{
MessageBox.Show("字节越界,请逐个字节输入!", "Error");
return;
}
ii++;
}
sp1.Write(byteBuffer, 0, byteBuffer.Length);
}
///这部分是接收部分,即在大框中显示串口接收到的数据,可能是这一块出问题,接收解析成16进制数字时出错,对比见前面的图片!!!!!
void sp1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
if (sp1.IsOpen) //此处可能没有必要判断是否打开串口,但为了严谨性,我还是加上了
{
//输出当前时间
DateTime dt = DateTime.Now;
txtReceive.Text += dt.GetDateTimeFormats('f')[0].ToString() + "\r\n";
txtReceive.SelectAll();
txtReceive.SelectionColor = Color.Blue; //改变字体的颜色
byte[] byteRead = new byte[sp1.BytesToRead]; //BytesToRead:sp1接收的字符个数
try
{
Byte[] receivedData = new Byte[sp1.BytesToRead]; //创建接收字节数组
sp1.Read(receivedData, 0, receivedData.Length); //读取数据
//string text = sp1.Read(); //Encoding.ASCII.GetString(receivedData);
sp1.DiscardInBuffer(); //清空SerialPort控件的Buffer
string strRcv = null;
//int decNum = 0;//存储十进制
for (int i = 0; i < receivedData.Length; i++) //窗体显示
{
strRcv += receivedData[i].ToString("X2"); //16进制显示
}
txtReceive.Text += strRcv + "\r\n";
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message, "出错提示");
txtSend.Text = "";
}
}
}
}
}
[/code]
[img=http://img.bbs.csdn.net/upload/201707/09/1499563146_195881.jpg][/img][img=http://img.bbs.csdn.net/upload/201707/09/1499563184_684768.png][/img]
发送A5 50时收到:
自己写的:3F3F3F3F3F3F043F3F0102C73F3FC13F839EF23FE69EF71F3F3D3F
串口调试助手的:A55A140000000424140102C7D296C1E8839EF2C0E69EF71F653D05
发送A5 52时收到:
自己写的:3F3F3F3F3F3F3F3F3F3F
串口调试助手的:A55A0300000006000000
发送A5 59时收到:
自己写的:3F3F043F3F3F15F2013F3F
串口调试助手的:A55A0400000015F201F900
[code=csharp]using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
namespace csharp串口通讯
{
public partial class Form1 : Form
{
SerialPort sp1;
public Form1()
{
InitializeComponent();
this.FormClosed += new FormClosedEventHandler(Form1_FormClosed);
if (sp1 == null)
{
Control.CheckForIllegalCrossThreadCalls = false; //这个类中我们不检查跨线程的调用是否合法(因为.net 2.0以后加强了安全机制,,不允许在winform中直接跨线程访问控件的属性)
sp1 = new SerialPort("COM8", 115200);
sp1.StopBits = StopBits.One;//
sp1.DataBits = 8;//
sp1.Parity = Parity.Even;//设置串口属性
sp1.Open();
sp1.DataReceived += new SerialDataReceivedEventHandler(sp1_DataReceived);
}
}
void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
if (sp1 != null && sp1.IsOpen)
{
sp1.Close();
}
}
///这一部分是发送部分,即在文本框中输入内容后,点击发送按钮,里面包含把内容转化为16进制,这部分应该是对的
private void btnSend_Click(object sender, EventArgs e)
{
if (!sp1.IsOpen) //如果没打开
{
MessageBox.Show("请先打开串口!", "Error");
return;
}
String strSend = txtSend.Text;
//处理数字转换
string sendBuf = strSend;
string sendnoNull = sendBuf.Trim();
string sendNOComma = sendnoNull.Replace(',', ' '); //去掉英文逗号
string sendNOComma1 = sendNOComma.Replace(',', ' '); //去掉中文逗号
string strSendNoComma2 = sendNOComma1.Replace("0x", ""); //去掉0x
strSendNoComma2.Replace("0X", ""); //去掉0X
string[] strArray = strSendNoComma2.Split(' ');
int byteBufferLength = strArray.Length;
for (int i = 0; i < strArray.Length; i++ )
{
if (strArray[i]=="")
{
byteBufferLength--;
}
}
// int temp = 0;
byte[] byteBuffer = new byte[byteBufferLength];
int ii = 0;
for (int i = 0; i < strArray.Length; i++) //对获取的字符做相加运算
{
Byte[] bytesOfStr = Encoding.Default.GetBytes(strArray[i]);
int decNum = 0;
if (strArray[i] == "")
{
//ii--; //加上此句是错误的,下面的continue以延缓了一个ii,不与i同步
continue;
}
else
{
decNum = Convert.ToInt32(strArray[i], 16); //atrArray[i] == 12时,temp == 18
}
try //防止输错,使其只能输入一个字节的字符
{
byteBuffer[ii] = Convert.ToByte(decNum);
}
catch (System.Exception ex)
{
MessageBox.Show("字节越界,请逐个字节输入!", "Error");
return;
}
ii++;
}
sp1.Write(byteBuffer, 0, byteBuffer.Length);
}
///这部分是接收部分,即在大框中显示串口接收到的数据,可能是这一块出问题,接收解析成16进制数字时出错,对比见前面的图片!!!!!
void sp1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
if (sp1.IsOpen) //此处可能没有必要判断是否打开串口,但为了严谨性,我还是加上了
{
//输出当前时间
DateTime dt = DateTime.Now;
txtReceive.Text += dt.GetDateTimeFormats('f')[0].ToString() + "\r\n";
txtReceive.SelectAll();
txtReceive.SelectionColor = Color.Blue; //改变字体的颜色
byte[] byteRead = new byte[sp1.BytesToRead]; //BytesToRead:sp1接收的字符个数
try
{
Byte[] receivedData = new Byte[sp1.BytesToRead]; //创建接收字节数组
sp1.Read(receivedData, 0, receivedData.Length); //读取数据
//string text = sp1.Read(); //Encoding.ASCII.GetString(receivedData);
sp1.DiscardInBuffer(); //清空SerialPort控件的Buffer
string strRcv = null;
//int decNum = 0;//存储十进制
for (int i = 0; i < receivedData.Length; i++) //窗体显示
{
strRcv += receivedData[i].ToString("X2"); //16进制显示
}
txtReceive.Text += strRcv + "\r\n";
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message, "出错提示");
txtSend.Text = "";
}
}
}
}
}
[/code]