winform 串口16进制发送接收,要源码

winform 串口16进制发送接收
要源码,最好有注释,求高手帮忙

2个回答

static int buffersize = 18; //十六进制数的大小(假设为9Byte,可调整数字大小)
byte[] buffer = new Byte[buffersize]; //创建缓冲区

private void button1_Click(object sender, EventArgs e)
{
serialPort1.Read(buffer, 0, buffersize);
string ss;
ss = byteToHexStr(buffer); //用到函数byteToHexStr
textBox2.Text = ss;
serialPort1.Close();
MessageBox.Show("数据接收成功!", "系统提示");
}

//字节数组转16进制字符串
public static string byteToHexStr(byte[] bytes)
{
string returnStr = "";
if (bytes != null)
{
for (int i = 0; i < bytes.Length; i++)
{
returnStr += bytes[i].ToString("X2");
}
}
return returnStr;
}

private void Form1_Load(object sender, EventArgs e)
{
serialPort1.PortName = "COM1";
serialPort1.BaudRate = 9600;
serialPort1.DataBits = 8; //数据位
serialPort1.Parity = System.IO.Ports.Parity.None; //无奇偶校验位
serialPort1.StopBits = System.IO.Ports.StopBits.One; //一个停止位
serialPort1.ReadBufferSize = 1024; //接收缓冲区大小
serialPort1.Encoding = Encoding.BigEndianUnicode;
serialPort1.Open();
}

http://download.csdn.net/download/glen30/511463
完整代码

先采纳本回答留下邮箱,可以代下载

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问