You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
112 lines
3.4 KiB
C#
112 lines
3.4 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Runtime.Remoting.Messaging;
|
|
using System.Security.Cryptography.X509Certificates;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace ZWGXPICK_SYS.SerialPort
|
|
{
|
|
/// <summary>
|
|
/// 称重设备
|
|
/// </summary>
|
|
public class WeightEquipment
|
|
{
|
|
private SerialPortUtils serialPortUtils;
|
|
|
|
//发送请求
|
|
// private byte[] sendCode = new byte[] { 0xFF, 0x03, 0x00, 0x00, 0x00, 0x02, 0xD1, 0xD5 };
|
|
//置零
|
|
// private byte[] restZero = new byte[] { 0xFF, 0x06, 0x00, 0x07, 0x00, 0x02, 0xAC, 0x14 };
|
|
|
|
public event EventHandler<MessageEventArgs> MessageSend = null;
|
|
|
|
public event EventHandler<WeightModel> WeightSend = null;
|
|
|
|
public WeightEquipment(string comName, int comBaud, int ReceivedBytesThreshold = 18) : base()
|
|
{
|
|
serialPortUtils = SerialPortUtils.Builder(comName).ComBaud(comBaud).ReceivedBytesThreshold(ReceivedBytesThreshold).Build();
|
|
serialPortUtils.SerialDataReceived += SerialPortUtils_SerialDataReceived;
|
|
serialPortUtils.SerialMessage += SerialPortUtils_SerialMessage;
|
|
}
|
|
/// <summary>
|
|
/// 打开连接
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public bool Open(string comName = "", int comBaud = 0)
|
|
{
|
|
if (!string.IsNullOrEmpty(comName))
|
|
{
|
|
serialPortUtils.ComName = comName;
|
|
}
|
|
if (comBaud != 0)
|
|
{
|
|
serialPortUtils.ComBaud = comBaud;
|
|
}
|
|
var val = serialPortUtils.Open();
|
|
return val;
|
|
}
|
|
/// <summary>
|
|
/// 关闭连接
|
|
/// </summary>
|
|
public void Close()
|
|
{
|
|
serialPortUtils.Close();
|
|
}
|
|
|
|
public bool IsOpen()
|
|
{
|
|
return serialPortUtils.IsOpen;
|
|
}
|
|
/// <summary>
|
|
/// 置零
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
//public bool Rset()
|
|
//{
|
|
// return serialPortUtils.SendData(restZero);
|
|
//}
|
|
|
|
//public bool Send()
|
|
//{
|
|
// return serialPortUtils.SendData(sendCode);
|
|
//}
|
|
|
|
//接收数据
|
|
private void SerialPortUtils_SerialDataReceived(object sender, byte[] e)
|
|
{
|
|
if (e.Length != 18)
|
|
{
|
|
MessageSend?.Invoke(this, new MessageEventArgs(true, $"接收数据长度不正确,长度为{e.Length}字节 "));
|
|
return;
|
|
}
|
|
//转换为ASCII字符串
|
|
var str = ASCIIEncoding.ASCII.GetString(e);
|
|
var weightStr = str.Substring(9, 5);
|
|
if (!decimal.TryParse(weightStr, out var weight))
|
|
{
|
|
MessageSend?.Invoke(this, new MessageEventArgs(true, $"数据解析不正确,数据为:{str},重量解析为:{weightStr}"));
|
|
return;
|
|
}
|
|
if (weight > 0)
|
|
{
|
|
Math.Round(weight, 2);
|
|
WeightSend?.Invoke(this, new WeightModel
|
|
{
|
|
COMName = serialPortUtils.ComName,
|
|
Weight = weight,
|
|
Nuit = "克",
|
|
});
|
|
}
|
|
}
|
|
|
|
// 消息提醒
|
|
private void SerialPortUtils_SerialMessage(object sender, MessageEventArgs e)
|
|
{
|
|
MessageSend?.Invoke(this, e);
|
|
}
|
|
}
|
|
}
|