c#的串口通信问题,大大们帮忙看下是怎么回事啊!
using System;using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using
using
namespace _05
{
public partial class Reader : Form
{
public Reader()
{
InitializeComponent();
}
private void CCOM_SelectedIndexChanged(object sender, EventArgs e)
{
serialPort1.Close();
button1.Text = "打 开";
}
private void button1_Click(object sender, EventArgs e)
{
if (button1.Text == "打 开")
{
serialPort1.Open();
button1.Text = "关 闭";
}
else
{
serialPort1.Close();
button1.Text = "打 开";
}
}
private void button2_Click(object sender, EventArgs e)
{
richTextBox1.Clear();
serialPort1.Close();
button1.Text = "打 开";
}
private void Baud_SelectedIndexChanged(object sender, EventArgs e)
{
serialPort1.Close();
button1.Text = "打 开";
}
private void serialPort1_DataReceived(object sender, e)
{
string serialReadString = serialPort1.ReadExisting();
this.richTextBox1.Invoke
(
new MethodInvoker
(
delegate
{
this.richTextBox1.AppendText(serialReadString);
}
)
);
}
private void Reader_Load(object sender, EventArgs e)
{
chuankou();
botelv();
serialPort1.PortName = CCOM.Text;
serialPort1.BaudRate = int.Parse(CBaud.Text);
serialPort1.DataBits = 8;
serialPort1.Parity =
serialPort1.StopBits =
serialPort1.ReadBufferSize = 1024;
}
private void chuankou()
{
CBaud.Items.Clear();
foreach (string portName in ())
{
SerialPort handler = new SerialPort(portName);
try
{
handler.Open();
CCOM.Items.Add(portName);
//CCOM.Text = "COM1";
//comcom.Items.Add(portName);
CCOM.Text = portName;
}
catch (Exception)
{
}
finally
{
handler.Close();
}
}
}
private void botelv()
{
CBaud.Items.Add("2400");
CBaud.Items.Add("4800");
CBaud.Items.Add("9600");
CBaud.Items.Add("38400");
CBaud.Text = "38400";
}
}
}
下面是接收并且显示到richbox中的数据,每个数据是11字节,数据格式是:02开头(1字节),读头编号(1字节),数据类型(1字节),数据长度(1字节),通信数据(6字节),校验位(1字节)。
0384EB,03D2FA{,03638Ct,0278F3q,01CF83,03E116y。
但是当接收数据格式为02 32 45 41 44(例)的时候显示的就全是“???????”这是什么原因啊?请各位大大们解惑,还有,如何将接收到的信息(如02 32 45 41 44)原封不动的显示到richbox里啊(就是也显示02 32 45 41 44)