C# SerialPort控件做串口编程中,多个串口同时通信的问题,请大家帮忙
我是初学者,对这方面的知识不太了解,希望高手指点。用不用多线程呀?为什么我的总是出问题,不是发送不了,就是给COM8发送数据,COM3也能收到,不知道是怎么回事,我的代码如下,希望高手指点。或者给一段多个串口同时通信的代码,让小弟参考一下,不胜感激![code=C#]namespace NETTEST
{
public partial class Form_Main : Form
{
SerialPort spcom = new SerialPort();
SerialPort spcom2 = new SerialPort();
delegate void HandleInterfaceUpdateDelegate(string text); //委托
HandleInterfaceUpdateDelegate interfaceUpdateHandle;
HandleInterfaceUpdateDelegate interfaceUpdateHandle2;
public Form_Main()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
comboBox1.Items.AddRange(System.IO.Ports.SerialPort.GetPortNames());
comboBox6.Items.AddRange(System.IO.Ports.SerialPort.GetPortNames());
comboBox1.Text = comboBox1.Items[0].ToString();
comboBox2.Text = comboBox2.Items[0].ToString();
comboBox3.Text = comboBox3.Items[0].ToString();
comboBox4.Text = comboBox4.Items[0].ToString();
comboBox5.Text = comboBox5.Items[0].ToString();
comboBox6.Text = comboBox6.Items[0].ToString();
comboBox7.Text = comboBox7.Items[0].ToString();
comboBox8.Text = comboBox8.Items[0].ToString();
comboBox9.Text = comboBox9.Items[0].ToString();
comboBox10.Text = comboBox10.Items[0].ToString();
}
public void spcom_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
byte[] readBuffer = new byte[spcom.ReadBufferSize];
spcom.Read(readBuffer, 0, readBuffer.Length);
this.Invoke(interfaceUpdateHandle, new string[] { Encoding.ASCII.GetString (readBuffer) }
}
public void spcom2_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
byte[] readBuffer = new byte[spcom2.ReadBufferSize];
spcom2.Read(readBuffer, 0, readBuffer.Length);
this.Invoke(interfaceUpdateHandle2, new string[] { Encoding.ASCII.GetString(readBuffer) });
}
//字节数组转换为十六进制
private string ByteArrayToHexString(byte[] data)
{
StringBuilder sb = new StringBuilder(data.Length * 3);
foreach (byte b in data)
sb.Append(Convert.ToString(b, 16).PadLeft(2, '0').PadRight(3, ' '));
return sb.ToString().ToUpper();
}
//字符串转换为十六进制
private string StringToHexString(string str)
{
StringBuilder sb = new StringBuilder(str.Length * 3);
foreach (char b in str)
sb.Append(Convert.ToString(b, 16).PadLeft(2, '0').PadRight(3, ' '));
return sb.ToString().ToUpper();
}
private void UpdateTextBox1(string text)
{
txtrev1.Text += text;
}
private void UpdateTextBox2(string text)
{
txtrev2.Text += text;
}
//发送字符串数据
private void SendStringData(SerialPort serialPort)
{
try
{
SerialPort i = serialPort;
//if (!spcom.IsOpen)
//{ spcom.Open(); }
string str = txtsend.Text.Trim();
//serialPort.WriteLine(CharToIntStr(str));// .Write //txtsend.Text.ToString().Trim()
// byte[] data= Convert .ToByte (str);
serialPort.WriteLine(StringToHexString(str));
serialPort.Write(txtsend.Text);
//spcom2.WriteLine(CharToIntStr(str));// .Write //txtsend.Text.ToString().Trim()
//// byte[] data= Convert .ToByte (str);
//spcom2.WriteLine(StringToHexString(str));
//spcom2.Write(txtsend.Text);
}
catch(Exception ex)
{
MessageBox.Show("没有找到串口,请先打开串口", "警告"+ex.Message );
}
}
//发送二进制数据
private void SendBytesData(SerialPort serialPort)
{
byte[] bytesSend = Encoding.Default.GetBytes(txtsend.Text);
spcom.Write(bytesSend, 0, bytesSend.Length);
}
private void txtrev_TextChanged(object sender, EventArgs e)
{
txtrev1.SelectionStart = txtrev1.Text.Length;
txtrev1.SelectionLength = 0;
txtrev1.ScrollToCaret();
}
private void button1_Click(object sender, EventArgs e)
{
SerialPortInitial();
SerialPortInitial2();
try
{
if (!spcom.IsOpen) spcom.Open();
if (!spcom2.IsOpen) spcom2.Open();
}
catch (Exception ex)
{
MessageBox.Show("没有找到串口,请检查后再试……", "警告" + ex.Message);
}
interfaceUpdateHandle = new HandleInterfaceUpdateDelegate(UpdateTextBox1);
spcom.DataReceived += new SerialDataReceivedEventHandler(spcom_DataReceived);
spcom.ReceivedBytesThreshold = 1;
interfaceUpdateHandle2 = new HandleInterfaceUpdateDelegate(UpdateTextBox2);
spcom2.DataReceived += new SerialDataReceivedEventHandler(spcom2_DataReceived);
spcom2.ReceivedBytesThreshold = 1;
}
private void button3_Click(object sender, EventArgs e)
{
try
{
SendStringData(spcom);
SendStringData(spcom2);
}
catch (Exception ex)
{
MessageBox.Show("没有找到串口,请检查后再试……", "警告" + ex.Message);
}
}