2串口用串口助手测试正常.现用mscomm控件在c#上编的2串口互发信息的测试程序,但运行不显示2串口互发的信息.using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace com
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
timer1.Interval = 5000;
timer2.Interval = 5000;
} private void Form1_Load(object sender, EventArgs e)
{
com_1_ini();
com_3_ini();
timer1.Enabled = true;
timer2.Enabled = true;
try
{
axMSComm2.Output = " from test3";axMSComm1.Output = " from test1";
}
catch (InvalidCastException ee)
{
string st = ee.ToString();
}
} private void com_1_ini()
{
try
{
axMSComm1._CommPort = 1;
if (axMSComm1.PortOpen) axMSComm1.PortOpen = false;
axMSComm1.Settings = "9600,n,8,1";
axMSComm1.Handshaking = MSCommLib.HandshakeConstants.comNone;
axMSComm1.InputMode = MSCommLib.InputModeConstants.comInputModeText;//文本形式取回
axMSComm1.InputLen = 0; //当设置为零时,在Input可用时读取所有的数据
axMSComm1.OnComm += new System.EventHandler(this.OnComm1); //OnComm事件发生,加入列表
axMSComm1.PortOpen = true; //打开端口
}
catch (InvalidCastException e)
{
string st = e.ToString();
}
}
private void com_3_ini()
{
try
{
axMSComm2._CommPort = 3;
if (axMSComm2.PortOpen) axMSComm2.PortOpen = false;
axMSComm2.Settings = "9600,n,8,1";
axMSComm2.Handshaking = MSCommLib.HandshakeConstants.comNone;
axMSComm2.InputMode = MSCommLib.InputModeConstants.comInputModeText;//文本形式取回
axMSComm2.InputLen = 0; //当设置为零时,在Input可用时读取所有的数据
axMSComm2.OnComm += new System.EventHandler(this.OnComm2); //OnComm事件发生,加入列表
axMSComm2.PortOpen = true; //打开端口
}
catch (InvalidCastException e)
{
string st = e.ToString();
}
}
private void OnComm1(object sender, System.EventArgs e)
{
MessageBox.Show("com1 "+axMSComm1.CommEvent.ToString());
// if (axMSComm1.CommEvent == 2)//接收事件
{
string recievemsg; //检查接收到的数据长度是否大于0,若大于0表示接收到数据
if (axMSComm1.InBufferCount > 0)
{
for (int i = 0; i < 1000; i++) { int pp = 0; }//延时
recievemsg = (string)axMSComm1.Input;
textBox1.Text += recievemsg + "\n";
}
}
} private void OnComm2(object sender, System.EventArgs e)
{
MessageBox.Show("OnCom com2 in");
// if (axMSComm2.CommEvent == 2)//接收事件
{
string recievemsg; //检查接收到的数据长度是否大于0,若大于0表示接收到数据
if (axMSComm2.InBufferCount > 0)
{
for (int i = 0; i < 1000; i++) { int pp = 0; }//延时
recievemsg = (string)axMSComm2.Input;
textBox2.Text += recievemsg + "\n";
}
}
} private void timer1_Tick(object sender, EventArgs e)
{
MessageBox.Show("time1 com1out");
axMSComm1.Output = "This is from test1"; //发送的数据字符串
}
private void timer2_Tick(object sender, EventArgs e)
{
MessageBox.Show("time2 com2out");
axMSComm2.Output = "This is from test3"; //发送的数据字符串
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace com
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
timer1.Interval = 5000;
timer2.Interval = 5000;
} private void Form1_Load(object sender, EventArgs e)
{
com_1_ini();
com_3_ini();
timer1.Enabled = true;
timer2.Enabled = true;
try
{
axMSComm2.Output = " from test3";axMSComm1.Output = " from test1";
}
catch (InvalidCastException ee)
{
string st = ee.ToString();
}
} private void com_1_ini()
{
try
{
axMSComm1._CommPort = 1;
if (axMSComm1.PortOpen) axMSComm1.PortOpen = false;
axMSComm1.Settings = "9600,n,8,1";
axMSComm1.Handshaking = MSCommLib.HandshakeConstants.comNone;
axMSComm1.InputMode = MSCommLib.InputModeConstants.comInputModeText;//文本形式取回
axMSComm1.InputLen = 0; //当设置为零时,在Input可用时读取所有的数据
axMSComm1.OnComm += new System.EventHandler(this.OnComm1); //OnComm事件发生,加入列表
axMSComm1.PortOpen = true; //打开端口
}
catch (InvalidCastException e)
{
string st = e.ToString();
}
}
private void com_3_ini()
{
try
{
axMSComm2._CommPort = 3;
if (axMSComm2.PortOpen) axMSComm2.PortOpen = false;
axMSComm2.Settings = "9600,n,8,1";
axMSComm2.Handshaking = MSCommLib.HandshakeConstants.comNone;
axMSComm2.InputMode = MSCommLib.InputModeConstants.comInputModeText;//文本形式取回
axMSComm2.InputLen = 0; //当设置为零时,在Input可用时读取所有的数据
axMSComm2.OnComm += new System.EventHandler(this.OnComm2); //OnComm事件发生,加入列表
axMSComm2.PortOpen = true; //打开端口
}
catch (InvalidCastException e)
{
string st = e.ToString();
}
}
private void OnComm1(object sender, System.EventArgs e)
{
MessageBox.Show("com1 "+axMSComm1.CommEvent.ToString());
// if (axMSComm1.CommEvent == 2)//接收事件
{
string recievemsg; //检查接收到的数据长度是否大于0,若大于0表示接收到数据
if (axMSComm1.InBufferCount > 0)
{
for (int i = 0; i < 1000; i++) { int pp = 0; }//延时
recievemsg = (string)axMSComm1.Input;
textBox1.Text += recievemsg + "\n";
}
}
} private void OnComm2(object sender, System.EventArgs e)
{
MessageBox.Show("OnCom com2 in");
// if (axMSComm2.CommEvent == 2)//接收事件
{
string recievemsg; //检查接收到的数据长度是否大于0,若大于0表示接收到数据
if (axMSComm2.InBufferCount > 0)
{
for (int i = 0; i < 1000; i++) { int pp = 0; }//延时
recievemsg = (string)axMSComm2.Input;
textBox2.Text += recievemsg + "\n";
}
}
} private void timer1_Tick(object sender, EventArgs e)
{
MessageBox.Show("time1 com1out");
axMSComm1.Output = "This is from test1"; //发送的数据字符串
}
private void timer2_Tick(object sender, EventArgs e)
{
MessageBox.Show("time2 com2out");
axMSComm2.Output = "This is from test3"; //发送的数据字符串
}
}
}
和
private void OnComm2(object sender, System.EventArgs e)
是响应COMM的事件。
你这里面不应该出现MessageBox。因为当你响应MessageBox的时候,可能正好有1个字节在COMM2口接收,那么就无法执行了。
和
private void OnComm2(object sender, System.EventArgs e)
里面的 MessageBox是我后加的,想看下这2个OnComm1 OnComm2是否有响应。 也没有响应.
private void Form1_Load(object sender, EventArgs e)
{
LoadSerialPortConfiguration();
cmbPortName.SelectedIndex = 0;
cmbBaudRate.SelectedIndex = 6;
cmbDataBits.SelectedIndex = 4;
cmbStopBits.SelectedIndex = 1;
cmbParity.SelectedIndex = 0;
}
private void LoadSerialPortConfiguration()
{
foreach (string s in SerialPort.GetPortNames())
{
cmbPortName.Items.Add(s);
} foreach (string s in Enum.GetNames(typeof(Parity)))
{
cmbParity.Items.Add(s);
} foreach (string s in Enum.GetNames(typeof(StopBits)))
{
cmbStopBits.Items.Add(s);
}
} private void btnOpenSerialPort_Click(object sender, EventArgs e)
{
if (serialPort1.IsOpen == false)
{
try
{
设置serialPort1的串口属性
serialPort1.PortName = cmbPortName.Text;
serialPort1.BaudRate = int.Parse(cmbBaudRate.Text);
serialPort1.DataBits = int.Parse(cmbDataBits.Text);
serialPort1.StopBits = (StopBits)Enum.Parse(typeof(StopBits), cmbStopBits.Text);
serialPort1.Parity = (Parity)Enum.Parse(typeof(Parity), cmbParity.Text);
//打开串口
serialPort1.Open();
btnOpenSerialPort.Text = "关闭串口";
lblState.ImageIndex = 0;
bComOpen = true; cmbPortName.Enabled = false;
cmbBaudRate.Enabled = false;
cmbDataBits.Enabled = false;
cmbStopBits.Enabled = false;
cmbParity.Enabled = false; }
catch (System.IO.IOException) { } //可以进一步扩展
}
else
{
serialPort1.Close();
btnOpenSerialPort.Text = "打开串口";
lblState.ImageIndex = 1;
cmbPortName.Enabled = true;
cmbBaudRate.Enabled = true;
cmbDataBits.Enabled = true;
cmbStopBits.Enabled = true;
cmbParity.Enabled = true;
bComOpen = false; //关闭串口时要做的处理
tmrAutoSend.Enabled = false;
chkAutoSend.Checked = false;
chkSendHex.Checked = false;
chkReceiveHex.Checked = false;
}
} //用于检测发送数据区的数据格式的过程。
private Boolean CheckGetSendData(String strSendData)
{
if (strSendData != "")
{
foreach (char chrTemp in strSendData) //检测数据的合法性
{
if (chrTemp == 0x20)
continue;
else if (chrTemp >= 0x30 && chrTemp <= 0x39)
continue;
else if (chrTemp >= 0x41 && chrTemp <= 0x46)
continue;
else if (chrTemp >= 0x61 && chrTemp <= 0x66)
continue;
else
{
return (false);
} }
try
{
String[] strData = strSendData.Split(Convert.ToChar(0x20));
int index, intCount = strData.Length;
byte[] bytDataTemp = new byte[intCount];
for (index = 0; index < intCount; index++) //16进制数据的字符串长度不能超过2
{
if (strData[index].Length > 2)
{
return (false);
}
}
for (index = 0; index < intCount; index++)
{
bytDataTemp[index] = Convert.ToByte(strData[index], 16);//把字符串转化成对应的字节数组
}
bytSendData = bytDataTemp;
return (true);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return (false);
}
finally
{
}
}
return (false); } private void chkSendHex_CheckedChanged(object sender, EventArgs e)
{
if (chkSendHex.Checked == true)
bSendHex = true;
else
bSendHex = false;
}
//把收到的16进制数据转化成字符串
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 void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{ if (bReceiveHex == true)
{
DataReceivedEventHandler DataReceive = new DataReceivedEventHandler(this.HexDataReceive);
DataReceive();
}
else
{
DataReceivedEventHandler DataReceive = new DataReceivedEventHandler(this.TextDataReceive);
DataReceive();
} }
private void chkReceiveHex_CheckedChanged(object sender, EventArgs e)
{
if (chkReceiveHex.Checked == true)
bReceiveHex = true;
else
bReceiveHex = false;
}
//以文本格式显示
private void TextDataReceive()
{
String strReceiveData = serialPort1.ReadExisting();
intReceiveDataCount += 1;//strReceiveData.Length;
Log(strReceiveData);
} //以16进制数据显示
private void HexDataReceive()
{
// Obtain the number of bytes waiting in the port's buffer
int bytes = serialPort1.BytesToRead; // Create a byte array buffer to hold the incoming data
byte[] buffer = new byte[bytes]; intReceiveDataCount += 1;// bytes; // Read the data from the port and store it in our buffer
serialPort1.Read(buffer, 0, bytes);
// Show the user the incoming data in hex format
Log(ByteArrayToHexString(buffer));
} //在接收区显示数据
private void Log(string msg)
{
if (bStopShow == false)
{
txtReceive.Invoke(new EventHandler(delegate
{
txtReceive.AppendText(msg);
txtReceive.ScrollToCaret();
}));
}
txtReceiveDataCount.Invoke(new EventHandler(delegate { txtReceiveDataCount.Text = intReceiveDataCount.ToString(); }));
txtSendDataCount.Invoke(new EventHandler(delegate { txtSendDataCount.Text = intSendDataCount.ToString(); }));
}