请高手帮我看看下面的程序,为什么不能和串口通信啊?另外,在哪可以找到mscomm控件与串口通信的代码和详解啊?万分感谢!!!!! 用了2个mscomm,1个texbox,1个button using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms;
namespace WindowsApplication20 { public partial class Form1 : Form { public Form1() { InitializeComponent(); }
private void Form1_Load(object sender, EventArgs e) { try { axMSComm1.PortOpen = true; axMSComm2.PortOpen = true; } catch { MessageBox.Show("操作失败"); }
} //只发送1字节,发多字节可改byte[1]中的值 private void button1_Click(object sender, EventArgs e) { byte[] bytOut = new byte[1]; bytOut[0] = 255; axMSComm2.Output = bytOut; }
private void axMSComm1_OnComm(object sender, EventArgs e) { string strIn = ""; byte[] bytIn; object objIn; int i;
axMSComm1.InputMode = MSCommLib.InputModeConstants.comInputModeBinary; axMSComm1.InputMode = 0; objIn = axMSComm1.Input; //注意这里axMSComm1.Input返回的是个object的 bytIn =(byte[]) objIn; //类型,这里必须使用显示的类型转换,和vb不同
for(i=0;i<=(bytIn.Length-1);i++) { strIn +="\r\n"+" "+ bytIn[i].ToString("X"); //转换成16进制显示 } textBox1.Text += strIn; }
} }