|
|
using System;
|
|
|
using System.Collections.Generic;
|
|
|
using System.Linq;
|
|
|
using System.IO.Ports;
|
|
|
using System.Data;
|
|
|
using System.Text;
|
|
|
using System.Threading;
|
|
|
using System.Threading.Tasks;
|
|
|
using System.Runtime.InteropServices;
|
|
|
using System.Net;
|
|
|
using System.Net.Sockets;
|
|
|
using System.Configuration;
|
|
|
|
|
|
namespace AUCMA_Air.Business
|
|
|
{
|
|
|
|
|
|
public class ControlOffLineScan
|
|
|
{
|
|
|
[DllImport("kernel32.dll", CallingConvention = CallingConvention.Winapi)]
|
|
|
extern static int GetTickCount();
|
|
|
public Action<string> RecAutoDataAction;
|
|
|
public string m_ComPort = ConfigurationManager.AppSettings["ComPort"];
|
|
|
|
|
|
#region 串口扫码器变量
|
|
|
//条码
|
|
|
public static SerialPort BarScanPort;
|
|
|
public static bool BarScanConn = false; //条码扫描设备连接状态
|
|
|
#endregion
|
|
|
|
|
|
#region 初始化
|
|
|
public void SystemInitialization()//初始化
|
|
|
{
|
|
|
InitBarScanPortProperty();//初始化串口扫码器
|
|
|
}
|
|
|
#endregion
|
|
|
|
|
|
public void ScanProvider(string m_ComPort)
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
BarScanPort = new SerialPort();
|
|
|
//AGVPort.PortName = BaseSystemInfo.SerialPortName;//设置串口名 默认COM1
|
|
|
BarScanPort.PortName = m_ComPort;
|
|
|
BarScanPort.BaudRate = 115200;//设置串口的波特率
|
|
|
BarScanPort.StopBits = StopBits.One;
|
|
|
BarScanPort.DataBits = 8;//设置数据位
|
|
|
BarScanPort.Parity = Parity.None;
|
|
|
BarScanPort.ReadTimeout = -1;//设置超时读取时间
|
|
|
//sp.RtsEnable=true; //定义DataReceived事件,当串口收到数据后触发事件
|
|
|
BarScanPort.ReceivedBytesThreshold = 1;
|
|
|
}
|
|
|
catch (Exception)
|
|
|
{
|
|
|
|
|
|
throw;
|
|
|
}
|
|
|
}
|
|
|
public bool Open()
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
BarScanPort.Open();
|
|
|
return true;
|
|
|
}
|
|
|
catch (Exception)
|
|
|
{
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
}
|
|
|
public void InitBarScanPortProperty()//设置串口的属性
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
BarScanPort = new SerialPort();
|
|
|
//AGVPort.PortName = BaseSystemInfo.SerialPortName;//设置串口名 默认COM1
|
|
|
BarScanPort.PortName = m_ComPort;
|
|
|
BarScanPort.BaudRate = 9600;//设置串口的波特率
|
|
|
BarScanPort.StopBits = StopBits.One;
|
|
|
BarScanPort.DataBits = 8;//设置数据位
|
|
|
BarScanPort.Parity = Parity.None;
|
|
|
BarScanPort.ReadTimeout = -1;//设置超时读取时间
|
|
|
//sp.RtsEnable=true; //定义DataReceived事件,当串口收到数据后触发事件
|
|
|
BarScanPort.ReceivedBytesThreshold = 1;
|
|
|
BarScanPort.DataReceived -= new SerialDataReceivedEventHandler(sp_DataReceived);
|
|
|
BarScanPort.DataReceived += new SerialDataReceivedEventHandler(sp_DataReceived);
|
|
|
BarScanPort.Open();
|
|
|
BarScanConn = true;
|
|
|
ICSharpCode.Core.LoggingService.Info("手持扫码枪连接成功!");
|
|
|
}
|
|
|
catch(Exception ex)
|
|
|
{
|
|
|
ICSharpCode.Core.LoggingService.Error("error:接收返回消息异常!具体原因:" + ex.Message);
|
|
|
BarScanConn = false;
|
|
|
}
|
|
|
}
|
|
|
public void Dispose()
|
|
|
{
|
|
|
if (BarScanPort!= null)
|
|
|
{
|
|
|
if (BarScanPort.IsOpen)
|
|
|
{
|
|
|
BarScanPort.Close();
|
|
|
}
|
|
|
BarScanPort.Dispose();
|
|
|
BarScanPort = null;
|
|
|
}
|
|
|
|
|
|
}
|
|
|
#region 串口扫码器数据获取
|
|
|
private void sp_DataReceived(object sender, SerialDataReceivedEventArgs e)
|
|
|
{
|
|
|
string g_s_Data = "";
|
|
|
try
|
|
|
{
|
|
|
|
|
|
#region
|
|
|
Thread.Sleep(100);
|
|
|
do
|
|
|
{
|
|
|
g_s_Data = BarScanPort.ReadExisting().Trim();
|
|
|
}
|
|
|
while (BarScanPort.BytesToRead > 0);
|
|
|
|
|
|
if (g_s_Data.Length > 0)
|
|
|
{
|
|
|
ICSharpCode.Core.LoggingService.Info("获取手持条码:" + g_s_Data);
|
|
|
//区分箱体码
|
|
|
|
|
|
RecAutoDataAction?.Invoke(g_s_Data);
|
|
|
|
|
|
|
|
|
//逻辑操作,g_s_Data为条码数据
|
|
|
}
|
|
|
GC.Collect();
|
|
|
#endregion
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
ICSharpCode.Core.LoggingService.Error("error:接收返回消息异常!具体原因:" + ex.Message);
|
|
|
//SysBusinessFunction.WriteLog();
|
|
|
}
|
|
|
}
|
|
|
#endregion
|
|
|
|
|
|
}
|
|
|
}
|