来源: (2条消息)SerialPort实现与地磅通信_JustLovePro的专栏-CSDN博客
1.拖一个serialPort 命名sp。
2.
.关键在于DataReceived事件的处理
.波特率设定与仪表设置一致:1200
.ReadBufferSize可设置为通信协定的数据长度:12
.注意起始与结束编码:0x02, 0x03
.Invoke实现委托,使得辅助线程修改主线程控件内容(也可以设置CheckForIllegalCrossThreadCalls直接跨线程作业)
(参考:http://www.cnblogs.com/worldreason/archive/2008/06/09/1216127.html )
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.Threading;
namespace WMS.SD.DeliveryGoods
{
public partial class frmUpLoad : Form
{
private delegate void HandleInterfaceUpdateDelegate(string aString);
HandleInterfaceUpdateDelegate interfaceUpdateHandle;
static string strReceive; // 接收到数据的缓冲区
public frmUpLoad()
{
InitializeComponent();
}
private void frmUpLoad_Load(object sender, EventArgs e)
{
//是否允许跨线程访问
// CheckForIllegalCrossThreadCalls = false;
interfaceUpdateHandle = new HandleInterfaceUpdateDelegate(ShowText);
try
{
if (!sp.IsOpen)
{
sp.Open();//打开串口方法
}
btnGetWeight.Enabled = false;
}//抛出异常
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
private void ShowText(string aString)
{
//根据通信数据规格显示数据
this.lblData.Text = aString.Substring(1, 5) + “.” + aString.Substring(5, 1);
}
private void btnGetWeight_Click(object sender, EventArgs e)
{
try
{
btnGetWeight.Enabled = false;
if (!sp.IsOpen)
{
Thread.Sleep(5000);
sp.Open();//打开串口方法
}
}//抛出异常
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
private void sp_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
try
{
//取得第一个字节
if (sp.IsOpen)
{
byte firstbyte = Convert.ToByte(sp.ReadByte());
//判断第一个字节是否是起始位:16进制的0x02
if (firstbyte == 0x02)
{
//定义接收数据长度
int bytesRead = sp.ReadBufferSize;
//数据接收字节数组
byte[] bytesData = new byte[bytesRead];
//接收字节
byte byteData;
for (int i = 0; i <= bytesRead – 1; i++)
{
try
{
if (sp.IsOpen)
{
byteData = Convert.ToByte(sp.ReadByte());
//判断数据结束字节
if (byteData == 0x03)
{
break;
}
bytesData[i] = byteData;
}
}
catch (Exception ex)
{
// MessageBox.Show( ex.Message + ex.GetType().FullName);
}
}
//将字节数组转换成字符串
strReceive = System.Text.Encoding.Default.GetString(bytesData);
//this.lblData.Text = strReceive.Substring(1, 5) + “.” + strReceive.Substring(5, 1);
// Invoke(interfaceUpdateHandle, strczData);
BeginInvoke(interfaceUpdateHandle, strReceive);
}
}
}
catch (Exception ex)
{
// MessageBox.Show(ex.Message + ex.GetType().FullName);
}
}
private void btnexit_Click(object sender, EventArgs e)
{
if (sp.IsOpen)
{
sp.DiscardInBuffer();
sp.Close();
}
this.Close();
}
private void btnreset_Click(object sender, EventArgs e)
{
try
{
if (sp.IsOpen)
{
sp.DiscardInBuffer();
sp.Close();
}
this.lblData.Text = “”;
btnGetWeight.Enabled = true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void frmUpLoad_FormClosing(object sender, FormClosingEventArgs e)
{
if (sp.IsOpen)
{
sp.DiscardInBuffer();
sp.Close();
}
}
}
}
————————————————
版权声明:本文为CSDN博主「JustLovePro」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/JustLovePro/article/details/3485106