using System;
using System.IO.Ports;
using System.Text;
using System.Threading;
namespace weight
{
/// <summary>
/// 電子秤接口信息類,封裝COM口數據
/// </summary>
public class WeightInformation
{
string _wdata;
string _wunit;
string _qdata;
string _qunit;
string _percentage;
/// <summary>
/// 獲取或設置重量
/// </summary>
public string WData { get { return this._wdata; } set { this._wdata = value; } }
/// <summary>
/// 獲取或設置重量單位
/// </summary>
public string WUnit { get { return this._wunit; } set { this._wunit = value; } }
/// <summary>
/// 獲取或設置數量
/// </summary>
public string QData { get { return this._qdata; } set { this._qdata = value; } }
/// <summary>
/// 獲取或設置數量單位
/// </summary>
public string QUnit { get { return this._qunit; } set { this._qunit = value; } }
/// <summary>
/// 獲取或設置百分數
/// </summary>
public string Percentage { get { return this._percentage; } set { this._percentage = value; } }
}
/// <summary>
/// 電子稱數據讀取類
/// </summary>
public class WeightReader : IDisposable
{
#region 字段、屬性與構造函數
SerialPort sp;
int _speed = 300;
/// <summary>
/// 獲取或設置電腦取COM數據緩沖時間,單位毫秒
/// </summary>
public int Speed
{
get
{
return this._speed;
}
set
{
if (value < 300)
throw new Exception("串口讀取緩沖時間不能小於300毫秒!");
this._speed = value;
}
}
public bool InitCom(string PortName)
{
return this.InitCom(PortName, 4800, 300);
}
/// <summary>
/// 初始化串口
/// </summary>
/// <param name="PortName">數據傳輸端口</param>
/// <param name="BaudRate">波特率</param>
/// <param name="Speed">串口讀數緩沖時間</param>
/// <returns></returns>
public bool InitCom(string PortName, int BaudRate,int Speed)
{
try
{
sp = new SerialPort(PortName, BaudRate, Parity.None, 8);
sp.ReceivedBytesThreshold = 10;
sp.Handshake = Handshake.RequestToSend;
sp.Parity = Parity.None;
sp.ReadTimeout = 600;
sp.WriteTimeout = 600;
this.Speed = Speed;
if (!sp.IsOpen)
{
sp.Open();
}
return true;
}
catch
{
throw new Exception(string.Format("無法初始化串口{0}!",PortName));
}
}
#endregion
#region 串口數據讀取方法
public WeightInformation ReadInfo()
{
string src = this.ReadCom();
WeightInformation info = new WeightInformation();
info.WData = this.DecodeWeightData(src);
info.WUnit = this.DecodeWeightUnit(src);
info.Percentage = this.DecodePercentage(src);
info.QData = this.DecodeQualityData(src);
info.QUnit = this.DecodeQualityUnit(src);
return info;
}
/// <summary>
/// 將COM口緩存數據全部讀取
/// </summary>
/// <returns></returns>
private string ReadCom()//返回信息
{
if (this.sp.IsOpen)
{
Thread.Sleep(this._speed);
string res = "";
//for (int i = 0; i < 5; i++)
//{
byte[] buffer = new byte[sp.BytesToRead];
sp.Read(buffer, 0, buffer.Length);
res = System.Text.Encoding.ASCII.GetString(buffer);
//if (res != "")
// break;
//}
if (res == "")
{
throw new Exception("串口讀取數據為空,參數設置是否正確!");
}
return res;
}
return "";
}
#endregion
#region 基本取數方法
/// <summary>
/// 從字符串中取值
/// </summary>
/// <param name="head">起始字符串</param>
/// <param name="intervalLen">間隔位長度</param>
/// <param name="valueLen">值長度</param>
/// <param name="src">源字符串</param>
/// <returns></returns>
private string DecodeValue(string head, int intervalLen, int valueLen,string src)
{
int index = src.IndexOf(head);
return src.Substring(index + intervalLen, valueLen);
}
/// <summary>
/// 取重量
/// </summary>
/// <param name="srcString">源字符串</param>
/// <returns></returns>
private string DecodeWeightData(string srcString)
{
return this.DecodeValue("GS,", 3, 8,srcString);
}
/// <summary>
/// 取重量單位
/// </summary>
/// <param name="srcString">源字符串</param>
/// <returns></returns>
private string DecodeWeightUnit(string srcString)
{
return this.DecodeValue("GS,", 12, 2, srcString);
}
/// <summary>
/// 取百分數
/// </summary>
/// <param name="srcString">源字符串</param>
/// <returns></returns>
private string DecodePercentage(string srcString)
{
return this.DecodeValue("U.W.", 4, 14, srcString);
}
/// <summary>
/// 取數量
/// </summary>
/// <param name="srcString">源字符串</param>
/// <returns></returns>
private string DecodeQualityData(string srcString)
{
return this.DecodeValue("PCS", 3, 9, srcString);
}
/// <summary>
/// 取數量單位
/// </summary>
/// <param name="srcString">源字符串</param>
/// <returns></returns>
private string DecodeQualityUnit(string srcString)
{
return this.DecodeValue("PCS", 12, 3, srcString);
}
#endregion
#region 釋放所有資源
public void Dispose()
{
if (sp != null && sp.IsOpen)
{
sp.Close();
}
}
#endregion
}
}
————————————————
版權聲明:本文為CSDN博主「maomaoawen」的原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/maomaoawen/article/details/4284514