项目中要用到C#调用com端口读取数据的操作,不能用控件。本人写了小例子,但是发送是OK的,读取的时候程序就处于假死状态。代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;namespace RS232Test
{
public partial class Form1 : Form
{
SerialPort serialPort1 = new SerialPort(); public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{ } private void button1_Click(object sender, EventArgs e)
{
serialPort1.PortName = "COM1";
serialPort1.BaudRate = 19200;
serialPort1.Parity = 0; //奇偶校验
serialPort1.ReadTimeout = 1000; //读超时
serialPort1.Open();
byte[] data = Encoding.Unicode.GetBytes(textBox1.Text);
string str = Convert.ToBase64String(data);
serialPort1.WriteLine(str);
MessageBox.Show("数据发送成功!", "系统提示");
} private void button2_Click(object sender, EventArgs e)
{
byte[] data = Convert.FromBase64String(serialPort1.ReadLine());
textBox2.Text = Encoding.Unicode.GetString(data);
serialPort1.Close();
MessageBox.Show("数据接收成功!", "系统提示");
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;namespace RS232Test
{
public partial class Form1 : Form
{
SerialPort serialPort1 = new SerialPort(); public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{ } private void button1_Click(object sender, EventArgs e)
{
serialPort1.PortName = "COM1";
serialPort1.BaudRate = 19200;
serialPort1.Parity = 0; //奇偶校验
serialPort1.ReadTimeout = 1000; //读超时
serialPort1.Open();
byte[] data = Encoding.Unicode.GetBytes(textBox1.Text);
string str = Convert.ToBase64String(data);
serialPort1.WriteLine(str);
MessageBox.Show("数据发送成功!", "系统提示");
} private void button2_Click(object sender, EventArgs e)
{
byte[] data = Convert.FromBase64String(serialPort1.ReadLine());
textBox2.Text = Encoding.Unicode.GetString(data);
serialPort1.Close();
MessageBox.Show("数据接收成功!", "系统提示");
}
}
}
解决方案 »
- BinaryWriter类,怎么没写文件?
- 谁教我一下怎么样用c#做个简单的画图板
- C#+ACCESS 分类汇总结果输出到GridView1中
- 请问这个webservice代理类为什么连接老是有错误
- *****VSS高手有关备份的问题,来一下呀********
- WMI管理的问题
- 高分寻求Installhield Developer使用详解电子版图书
- 修改了web.config文件,querystring里的中文为什么仍显示不出来?
- 关闭窗体(WinForm)之前的事件是什么(BeforeClosing?)?C#中找不到这个事件.
- winform+cefsharp的问题
- 求救~~~WINFORM程序每办法关闭了~
- 发传真(高手请进)
我一般用DataReceived 事件来读数据,如下,可以将读到的数据放在一个buffer里,用的时候再调用,这样做没有出现过你说的那个问题。
public System.IO.Ports.SerialPort m_CommPort =new System.IO.Ports.SerialPort();
m_CommPort.DataReceived += new SerialDataReceivedEventHandler(m_CommPort_DataReceived); private void m_CommPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
try {
byte[] buffer; // To obtain the number of bytes waiting in the port's buffer
Int32 iByteCount;
buffer = new byte[iByteCount];
m_CommPort.Read(buffer, 0, buffer.Length);
..... WaitResetEvent.Set(); }
catch (Exception ex)
{ }
}
不支持115200吗?
另外,读的时候有假死,我是用新开一个线程专门读数据,来解决的。
本机调试时,COM口的2,3号针需要连接起来才行
否则不能读取
能不能把你的程序给我看看
[email protected]