|
|
|
@ -0,0 +1,140 @@
|
|
|
|
|
using Admin.Core.Common;
|
|
|
|
|
using Aucma.Core.ProductOffLine.Config;
|
|
|
|
|
using Aucma.Core.Scanner;
|
|
|
|
|
using log4net;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO.Ports;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace Aucma.Core.ProductOffLine.Business
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 扫码枪补扫
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class GunBusiness
|
|
|
|
|
{
|
|
|
|
|
private static AppConfig appConfig = AppConfig.Instance;
|
|
|
|
|
private static SerialPort serialPort = new SerialPort();
|
|
|
|
|
|
|
|
|
|
private static List<ScannerModel> allScanners = Appsettings.app<ScannerModel>("ScannerServer").ToList();
|
|
|
|
|
// 内胆扫码器ip
|
|
|
|
|
private static string LinerScannerIp = allScanners.First(x => x.Name == "内侧扫码器1").Ip;
|
|
|
|
|
// 箱壳扫码器ip
|
|
|
|
|
private static string ShellScannerIp = allScanners.First(x => x.Name == "外侧扫码器2").Ip;
|
|
|
|
|
|
|
|
|
|
#region 单例实现
|
|
|
|
|
private static readonly GunBusiness lazy = new GunBusiness();
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 扫码委托
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="materialCodeStr"></param>
|
|
|
|
|
/// <param name="ip"></param>
|
|
|
|
|
public delegate void RefreshMaterialCodeStr(string materialCodeStr, string ip);
|
|
|
|
|
public static event RefreshMaterialCodeStr RefreshMaterialCodeStrEvent;
|
|
|
|
|
|
|
|
|
|
public static GunBusiness Instance
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return lazy;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static readonly log4net.ILog logHelper = LogManager.GetLogger(typeof(GunBusiness));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//初始化串口并启动接收数据
|
|
|
|
|
public static void InstanceSerialPort3()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
string port = System.IO.Ports.SerialPort.GetPortNames().FirstOrDefault();
|
|
|
|
|
//实例化串行端口
|
|
|
|
|
|
|
|
|
|
//端口名 注:因为使用的是USB转RS232 所以去设备管理器中查看一下虚拟com口的名字
|
|
|
|
|
serialPort.PortName = port;// portName;
|
|
|
|
|
//波特率
|
|
|
|
|
serialPort.BaudRate = 115200;
|
|
|
|
|
//奇偶校验
|
|
|
|
|
serialPort.Parity = Parity.None;
|
|
|
|
|
//停止位
|
|
|
|
|
serialPort.StopBits = StopBits.One;
|
|
|
|
|
//数据位
|
|
|
|
|
serialPort.DataBits = 8;
|
|
|
|
|
//忽略null字节
|
|
|
|
|
serialPort.DiscardNull = true;
|
|
|
|
|
|
|
|
|
|
//接收事件
|
|
|
|
|
serialPort.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(serialPort1_DataReceived);
|
|
|
|
|
|
|
|
|
|
//开启串口
|
|
|
|
|
serialPort.Open();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
logHelper.Error(ex.Message.ToString());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
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);
|
|
|
|
|
string ip = DirectionToIP();
|
|
|
|
|
if (!string.IsNullOrEmpty(ip))
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
RefreshMaterialCodeStrEvent?.Invoke(str,ip);
|
|
|
|
|
int direction = -1;
|
|
|
|
|
appConfig.GunDirection = direction.ToString();
|
|
|
|
|
}
|
|
|
|
|
sb.Clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 将用户选择的放行方向转换为对应位置的扫码器ip
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
private static string DirectionToIP()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
// 外侧方向转换为外侧扫码器ip
|
|
|
|
|
if (int.Parse(appConfig.GunDirection) == 0)
|
|
|
|
|
{
|
|
|
|
|
return ShellScannerIp;
|
|
|
|
|
}
|
|
|
|
|
// 内侧方向转换为内侧扫码器ip
|
|
|
|
|
else if (int.Parse(appConfig.GunDirection) == 1)
|
|
|
|
|
{
|
|
|
|
|
return LinerScannerIp;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|