You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

85 lines
1.7 KiB

3 years ago
using EC.AutoWeightServer.Model.EventArgsEx;
using EC.Utils.SerialPortHelper;
using System;
using EC.AutoWeightServer.Model.Config;
namespace EC.AutoWeightServer.IfManager.Scales
{
public class ScalesManager
{
public Scales Scales;
private string _comName;
private int _baudRate;
private int _receivedBytesThreshold;
private ECSerialPort _transmitPort;
public ScalesManager()
{
}
public ScalesManager(string comName, int baudRate)
{
_comName = comName;
_baudRate = baudRate;
}
public ScalesManager(string comName, int baudRate, int receivedBytesThreshold)
{
_comName = comName;
_baudRate = baudRate;
_receivedBytesThreshold = receivedBytesThreshold;
}
#region Start & Stop
public bool StartServer()
{
//Scales = new ScalesDefault(_comName, _baudRate);
Scales = new ScalesZhuHai(_comName, _baudRate, _receivedBytesThreshold);
Scales.OnRecData += RecData;
if (ServerConfig.WtTransmitFlag > 0)
{
_transmitPort = SerialPortManager.CreateSerialPort(ServerConfig.WtTransmitCom, _baudRate);
((ScalesZhuHai)Scales).OnRecTransmit += RecTransmit;
}
Scales.StartServer();
return true;
}
public bool StopServer()
{
Scales?.StopServer();
Scales = null;
return true;
}
public bool IsOpen()
{
return Scales?.IsOpen() ?? false;
}
#endregion Start & Stop
#region Rec Data
public event EventHandler<decimal> OnRecData;
private readonly object _lockWeightObj = new object();
public void RecData(object sender, ScaleEventArgs scaleArgs)
{
lock (_lockWeightObj)
{
var weight = scaleArgs.Weight;
OnRecData?.Invoke(sender, weight);
}
}
public void RecTransmit(object sender, byte[] buf)
{
_transmitPort?.SendHex(buf);
}
#endregion Rec Data
}
}