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.
Aucma.Scada/Aucma.Scada.Business/GunBusiness.cs

162 lines
5.6 KiB
C#

using Aucma.Scada.Model.domain;
using Aucma.Scada.Model.dto;
using HighWayIot.Common;
using HighWayIot.Config;
using HighWayIot.Log4net;
using HighWayIot.Plc;
using HighWayIot.Repository.service;
using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Aucma.Scada.Business
{
/// <summary>
/// 入库任务处理
/// </summary>
public sealed class GunBusiness
{
private static AppConfig appConfig = AppConfig.Instance;
private static SerialPort serialPort = new SerialPort();
public static List<System.IO.Ports.SerialPort> serialPorts = new List<System.IO.Ports.SerialPort>();
// 扫码枪绑定模型
public static List<GunBindModel> gunBindModels = new List<GunBindModel>();
#region 单例实现
private static readonly GunBusiness lazy = new GunBusiness();
/// <summary>
/// 扫码委托 ,箱壳码1,内胆码2
/// </summary>
/// <param name="materialCodeStr"></param>
/// <param name="ip"></param>
public delegate void RefreshMaterialCodeStr(string shellCode,string linerCode);
public static event RefreshMaterialCodeStr RefreshMaterialCodeStrEvent;
public static GunBusiness Instance
{
get
{
return lazy;
}
}
#endregion
//初始化串口并启动接收数据
public static void InstanceSerialPort3()
{
try
{
// string port = System.IO.Ports.SerialPort.GetPortNames().FirstOrDefault();
// string port = appConfig.Port;
var portList = appConfig.GetPortList();
if (portList == null || portList.Count == 0)
{
Console.WriteLine("端口列表为空,无法初始化串口。");
return;
}
//获取系统所有可打开的串口
var availablePorts = System.IO.Ports.SerialPort.GetPortNames();
foreach (var port in portList)
{
// 检查端口是否存在于可用端口列表中
if (!availablePorts.Contains(port))
{
LogHelper.Instance.Info($"端口 {port} 不存在于设备管理器中,跳过初始化。");
continue;
}
var serialPort = new System.IO.Ports.SerialPort();
// 设置串口属性
serialPort.PortName = port;
serialPort.BaudRate = int.Parse(appConfig.BaudRate);
serialPort.Parity = Parity.None;
serialPort.StopBits = StopBits.One;
serialPort.DataBits = 8;
serialPort.DiscardNull = true;
#region 将串口绑定模型添加到列表
GunBindModel model = new GunBindModel();
model.Port = port;
gunBindModels.Add(model);
#endregion
// 为每个串口设置独立的数据接收事件
serialPort.DataReceived += (sender, e) => SerialPort_DataReceived(sender, e, model);
// 将串口添加到列表
serialPorts.Add(serialPort);
// 开启串口
serialPort.Open();
LogHelper.Instance.Info($"端口 {port} 实例化开启成功");
}
}
catch (Exception ex)
{
LogHelper.Instance.Info($"实例化串口异常:{ex.Message}");
}
}
private static void SerialPort_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e, GunBindModel model)
{
try
{
var serialPort = (System.IO.Ports.SerialPort)sender; // 获取引发事件的串口对象
Thread.Sleep(50);
int nums = serialPort.BytesToRead;
byte[] receiveBytes = new byte[nums];
serialPort.Read(receiveBytes, 0, nums);
StringBuilder sb = new StringBuilder();
string str = Encoding.ASCII.GetString(receiveBytes).Replace("\r\n", "").Replace("\r", "").Replace("\n", "");
if (str.Substring(0, 1) == "B")
{
model.ShellCode = str;
}
else if (str.Substring(0, 1) == "L")
{
model.LinerCode = str;
}
if (!string.IsNullOrEmpty(model.LinerCode) && !string.IsNullOrEmpty(model.ShellCode))
{
try
{
// 绑定事件
RefreshMaterialCodeStrEvent(model.ShellCode, model.LinerCode);
}
catch (Exception ex)
{
LogHelper.Instance.Info($"串口{model.Port}绑定异常:{ex.Message}");
}
finally
{
model.ShellCode = "";
model.LinerCode = "";
}
}
sb.Clear();
}
catch (Exception ex)
{
LogHelper.Instance.Info($"串口{model.Port}接收数据异常:{ex.Message}");
}
}
}
}