做了下边的小测试程序:1.选择端口;2在textbox写信息;3点button进行发送;程序选择了端口后会启动监听线程接收com口发送的信息。
现在的问题是:
1:我发送的信息,自己的程序收不了。但是通过模拟com软件(模拟串口-VPSN)能接收到我发送的信息。
2:我在模拟com软件(模拟串口-VPSN)发送信息,自己写的程序居然能收到。
这就奇怪了,就是自己不能给自己发?并且我再开一个工程企图打开COM口(之前的程序已经在监听这个COM)失败。namespace Com_theory
{
public partial class Form1 : Form
{
System.IO.Ports.SerialPort com=null;
public Form1()
{
InitializeComponent();
//需要手动添加引用 Microsoft.VisualBasic
Microsoft.VisualBasic.Devices.Computer pc = new Microsoft.VisualBasic.Devices.Computer();
foreach (string s in pc.Ports.SerialPortNames)
{
this.comboBox1.Items.Add(s);
}
} private void button1_Click(object sender, EventArgs e)
{ com.WriteLine(this.textBox1.Text);
} private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
com = new System.IO.Ports.SerialPort(this.comboBox1.SelectedItem.ToString());
com.Open();
//com.ReadTimeout = 5 * 1000;//设置超时
ThreadPool.QueueUserWorkItem(new WaitCallback(thread_read), "t");
} private void thread_read(object state)
{ while (true)
{
try
{
textBox2.Text = com.ReadLine();
}
catch (Exception e)
{ MessageBox.Show(com.BytesToRead+e.ToString());
}
Thread.Sleep(1000*1);
}
//com.Close();
}
}
}
现在的问题是:
1:我发送的信息,自己的程序收不了。但是通过模拟com软件(模拟串口-VPSN)能接收到我发送的信息。
2:我在模拟com软件(模拟串口-VPSN)发送信息,自己写的程序居然能收到。
这就奇怪了,就是自己不能给自己发?并且我再开一个工程企图打开COM口(之前的程序已经在监听这个COM)失败。namespace Com_theory
{
public partial class Form1 : Form
{
System.IO.Ports.SerialPort com=null;
public Form1()
{
InitializeComponent();
//需要手动添加引用 Microsoft.VisualBasic
Microsoft.VisualBasic.Devices.Computer pc = new Microsoft.VisualBasic.Devices.Computer();
foreach (string s in pc.Ports.SerialPortNames)
{
this.comboBox1.Items.Add(s);
}
} private void button1_Click(object sender, EventArgs e)
{ com.WriteLine(this.textBox1.Text);
} private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
com = new System.IO.Ports.SerialPort(this.comboBox1.SelectedItem.ToString());
com.Open();
//com.ReadTimeout = 5 * 1000;//设置超时
ThreadPool.QueueUserWorkItem(new WaitCallback(thread_read), "t");
} private void thread_read(object state)
{ while (true)
{
try
{
textBox2.Text = com.ReadLine();
}
catch (Exception e)
{ MessageBox.Show(com.BytesToRead+e.ToString());
}
Thread.Sleep(1000*1);
}
//com.Close();
}
}
}
你针对com1的收发肯定是COM2收到,COM2发的com1收到。正常啊。
public delegate void Updata(string str);private void btnSend_Click(object sender, EventArgs e) //串口发送数据
{
myserialport.Write(rtbSend.Text.ToString());
rtbSend.Text = string.Empty; }
private void myserialport_DataReceived(object sender, SerialDataReceivedEventArgs e) //串口接收数据
{
int bytetoread = 0;
string str = "";
bytetoread = myserialport.BytesToRead;
byte[] ch = new byte[bytetoread];
myserialport.Read(ch, 0, bytetoread);
str = this.ByteArrayToString(ch);
str = str.Replace(" ","").Trim ();
rtbReceive.BeginInvoke(new Updata(UpdateReceive),str ); }private void UpdateReceive(string str) //读取串口数据并显示在窗体上
{
rtbReceive.AppendText(str);
rtbReceive.ScrollToCaret();
} private string ByteArrayToString(byte[] ch) //将字节数组转换为字符串
{
string str = "";
int i;
for (i = 0; i < ch.Length; i++)
{
str += System.Text.Encoding.Default.GetString(ch, i, 1); }
return str;
}
我现在是都在COM2里发送和接收数据,简单来说句是对COM2发送,下一句就是COM2接收。但居然都失败。
你的意思是在COM1向COM2发送数据(跨端口)?
有必要对那么低的硬件层面操作吗?就算是这样做,我也觉得不符合高级语言编程的思想(毕竟底层不应该由我们来考虑吧)。