串口通讯 c#
想实现一个从串口接收并显示的功能,用serialport做的,但是无论如何都收不到,请大虾们看下,指点一下小的,多谢各位了。代码如下: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;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Threading;
using System.Runtime.InteropServices;
namespace livelabel
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
SerialPort sp=new SerialPort();
private void Form1_Load(object sender, EventArgs e)
{
this.serialPort.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(this.serialPort_DataReceived);
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
ComBoBox_Load();
}
private void ComBoBox_Load() //设置串口号,波特率和字节长度
{
this.comboBox1.Text = comboBox1.Items[0].ToString();
this.comboBox2.Text = comboBox2.Items[0].ToString();
this.comboBox3.Text = comboBox3.Items[2].ToString();
}
private void button2_Click(object sender, EventArgs e)//串口开的按键
{
try
{
sp.PortName = this.comboBox1.SelectedItem.ToString();
sp.BaudRate = (int.Parse)(this.comboBox2.SelectedItem.ToString());
sp.DataBits = (int.Parse)(this.comboBox3.SelectedItem.ToString());
sp.Parity = Parity.None;
sp.StopBits = StopBits.One;
sp.RtsEnable = true;
sp.ReadTimeout = 1500;
sp.WriteTimeout = 1500;
sp.Open();
MessageBox.Show("串口打开成功!");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void button3_Click(object sender, EventArgs e)//串口关的按键
{
try
{
if (sp.IsOpen)
{
sp.Close();
MessageBox.Show("串口已关闭!");
}
else
{
MessageBox.Show("串口没有打开");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)//串口接收的事件
{
if (sp.IsOpen)
{
byte[] data = Convert.FromBase64String(sp.ReadLine());
this.tital.Text = Encoding.Unicode.GetString(data); //tital是一个textbox控件,就想显示在这个
//textbox中,但是无论如何都没反应。。。
}
}
private void timer1_Tick(object sender, EventArgs e)
{
string str = DateTime.Now.ToString();
this.label1.Text = str;
} --------------------编程问答-------------------- 哥哥姐姐们,有用c#做过串口的帮看看,想了好几天,不得其解。 --------------------编程问答-------------------- this.serialPort.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(this.serialPort_DataReceived);
这一句是LZ打错了,还是本来就这么写的?你不会是定义了两个串口,把sp给打开了,而要用serialPort来接收? --------------------编程问答-------------------- 我认为这个只是注册了serialport的datareceived事件。其实这也是个问题。本人初学还没太弄清楚。比如我在这个界面里加了一个timer控件,并用一个Label显示当前系统的时间。但是只是写一个private void timer_tick()事件就没反应,非要在from_load()里写一句this.timer1.Tick += new System.EventHandler(this.timer1_Tick)才有用。不知道是为什么。
我开始的时候也没写this.serialPort.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(this.serialPort_DataReceived);
这句的,当时就是收不到串口的数据,以为像timer控件一样要注册一下,所以写了这么一句。不过还是收不到。我怀疑还是datareceived事件里面的方法用的不对吧。 --------------------编程问答-------------------- 我上次写过这样的例子...QQ 476343380 --------------------编程问答-------------------- 下午来再看了一下,才明白你的意思。这里的serialport是打错了。。。。应该是SP。我去把板子拿来,看现在能用不。 --------------------编程问答--------------------
恩恩,把写错的那个该国来了,但是还是又错误。错误如图片所示。我感觉问题出在我对datarecevied事件的过程理解的不对。我理解的是如果串口缓冲区中有数据了,那么就触发这个datarecevied事件,来一次数据,就触发一次。所以就在private void serialport_datarecevied()里面写个转换字符类型再显示的两句话。不知道是不是我这个理解和思路是不是根本就是错的。请楼下的继续指点。
不知道这个图片能不能看到,先试试。。。。。 --------------------编程问答-------------------- 你先查看一下你的代码中对串口通讯频率等参数是否正确。 --------------------编程问答-------------------- 各个参数应该是正确的,下位机是别人做好的。我用串口助手是可以收到下位机的数据的。图片没法发上来。我还是说一下吧。
private void serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)//串口接收的事件
{
if (sp.IsOpen)
{
byte[] data = Convert.FromBase64String(sp.ReadLine()); //现在程序卡在了这一句
this.tital.Text = Encoding.Unicode.GetString(data);
}
现在程序运行到byte[] data = Convert.FromBase64String(sp.ReadLine()); 这句显示出错。错误为“操作超时”。本来我设置sp.ReadTimeout = 1500;,以为是设置小了,就把sp.ReadTimeout = 15000;,但是还是不行,只不过是多过了几秒程序还是卡在这个地方。现在我怀疑我对serialport的datarecevied事件的理解不对。我的理解是当串口缓冲区里有数据的时候就会触发serialport_datarecevied事件,来一个数据就触发一次。所以我就在serialport_datarecevied事件里写了句读串口数据和显示。对了,说下下位机的工作方式。下位机读一个温度传感器的信息,间隔0.5秒不停的将测得的温度数值向串口发送。
请楼下的继续指导。 --------------------编程问答-------------------- this.serialPort.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(this.serialPort_DataReceived);
在这句代码下面写上: 设置SerialPort的ReceiveBytesThreshold的触发字节长度。 试试
--------------------编程问答-------------------- 我有成功案例,谁有读写并口的案例啊;有请+我QQ:441858994
补充:.NET技术 , C#