使用System.IO.Ports.SerialPort.GetPortNames()枚举可用串口并验证是否被占用,空闲串口添加到comobox1控件中显示。代码如下:
erialPort中串口数据的读取与写入有较大的不同。由于串口不知道数据何时到达,因此有两种方法可以实现串口数据的读取。一、线程实时读串口;二、事件触发方式实现。
由 于线程实时读串口的效率不是十分高效,因此比较好的方法是事件触发的方式。在SerialPort类中有DataReceived事件,当串口的读缓存有 数据到达时则触发DataReceived事件,其中SerialPort.ReceivedBytesThreshold属性决定了当串口读缓存中数据 多少个时才触发DataReceived事件,默认为1。
另外,SerialPort.DataReceived事件运行比较特殊,其运行在辅线程,不能与主线程中的显示数据控件直接进行数据传输,必须用间接的方式实现。如下:
SerialPort spSend; //spSend,spReceive用虚拟串口连接,它们之间可以相互传输数据。spSend发送数据
SerialPort spReceive; //spReceive接受数据
TextBox txtSend; //发送区
TextBox txtReceive; //接受区
Button btnSend; //数据发送按钮
delegate void HandleInterfaceUpdateDelegate(string text); //委托,此为重点
HandleInterfaceUpdateDelegate interfaceUpdateHandle;
public void InitClient() //窗体控件已在初始化
{
interfaceUpdateHandle = new HandleInterfaceUpdateDelegate(UpdateTextBox); //实例化委托对象
spSend.Open(); //SerialPort对象在程序结束前必须关闭,在此说明
spReceive.DataReceived += Ports.SerialDataReceivedEventHandler(spReceive_DataReceived);
spReceive.ReceivedBytesThreshold = 1;
spReceive.Open();
}
public void btnSend_Click(object sender,EventArgs e)
{
spSend.WriteLine(txtSend.Text);
}
public void spReceive_DataReceived(object sender,Ports.SerialDataReceivedEventArgs e)
{
byte[] readBuffer = new byte[spReceive.ReadBufferSize];
spReceive.Read(readBuffer, 0, readBuffer.Length);
this.Invoke(interfaceUpdateHandle, new string[] { Encoding.Unicode.GetString(readBuffer) });
}
private void UpdateTextBox(string text)
{
txtReceive.Text = text;
}
private string StringToHexString(string s,Encoding encode)
{
byte[] b = encode.GetBytes(s);//按照指定编码将string编程字节数组
string result = string.Empty;
for (int i = 0; i < b.Length; i++)//逐字节变为16进制字符,以%隔开
{
result += "%"+Convert.ToString(b[i], 16);
}
return result;
}
private string HexStringToString(string hs, Encoding encode)
{
//以%分割字符串,并去掉空字符
string[] chars = hs.Split(new char[]{'%'},StringSplitOptions.RemoveEmptyEntries);
byte[] b = new byte[chars.Length];
//逐个字符变为16进制字节数据
for (int i = 0; i < chars.Length; i++)
{
b[i] = Convert.ToByte(chars[i], 16);
}
//按照指定编码将字节数组变为字符串
return encode.GetString(b);
}
url:http://greatverve.cnblogs.com/archive/2011/06/29/csharp-SerialPort.html