DLScanSocket.cs
4.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
using OnlineStore.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OnlineStore.DeviceLibrary
{
public class DLScanSocket: ScanManager
{
/// <summary>
/// 扫码枪链接
/// </summary>
private TcpClient scannerSocket;
/// <summary>
/// 扫描枪 是否开始运行
/// </summary>
private bool isScannerRun = false;
private string ScannerIP = "";
private int ScannerPort = 0;
public event OnCodeRevice CodeReviceEvent;
public DLScanSocket(string serverIp, int port,OnCodeRevice revicePro)
{
this.ScannerIP = serverIp;
this.ScannerPort = port;
this.CodeReviceEvent += revicePro;
}
/// <summary>
/// 开启扫码枪
/// </summary>
public override bool StartConnect( bool isMustCon = false)
{
bool result = true;
try
{
if (!isScannerRun || isMustCon)
{
if (scannerSocket == null)
{
scannerSocket = new TcpClient();
}
result = scannerSocket.StartConnect(ScannerIP, ScannerPort, new TcpClient.HandleMessage(onCodeReceived),2000);
if (result)
{
isScannerRun = true;
}
}
}
catch (Exception ex)
{
LogUtil.error("出错:" + ex);
}
return result;
}
public override void Close()
{
try
{
scannerSocket.close();
isScannerRun = false;
}
catch (Exception ex)
{
LogUtil.error("出错:" + ex);
}
}
public override void BeginScan()
{
try
{
scannerSocket.send(StoreManager.Config.StartScan_CMD);
}
catch (Exception ex)
{
LogUtil.error("【" + ScannerIP + "】开始扫码错误:" + ex.ToString());
}
}
/// <summary>
/// 扫码枪数据接收
/// </summary>
/// <param name="message"></param>
protected void onCodeReceived(string message)
{
try
{
message = message.Trim();
message = message.Replace("\r", "");
message = message.Replace("\n", "");
char a = (char)02;
message = message.Replace(a.ToString(), "");
message = message.Trim();
System.Text.ASCIIEncoding asciiEncoding = new System.Text.ASCIIEncoding();
byte[] bytes = asciiEncoding.GetBytes(message);
List<byte> newBytes = new List<byte>();
foreach (byte by in bytes)
{
if (!by.Equals(24))
{
newBytes.Add(by);
}
}
message = asciiEncoding.GetString(newBytes.ToArray());
string[] codeList = message.Split(new string[] { spiltStr},StringSplitOptions.RemoveEmptyEntries);
CodeReviceEvent?.Invoke(codeList);
// return message;
}
catch (Exception ex)
{
LogUtil.error(ex.ToString());
}
}
/// <summary>
/// 处理接收后的二维码
/// </summary>
/// <param name="message"></param>
/// <returns></returns>
public static string ReplaceCode(string message)
{
message = message.Trim();
message = message.Replace("\r", "");
message = message.Replace("\n", "");
char a = (char)02;
message = message.Replace(a.ToString(), "");
message = message.Trim();
System.Text.ASCIIEncoding asciiEncoding = new System.Text.ASCIIEncoding();
byte[] bytes = asciiEncoding.GetBytes(message);
List<byte> newBytes = new List<byte>();
foreach (byte by in bytes)
{
if (!by.Equals(24))
{
newBytes.Add(by);
}
}
message = asciiEncoding.GetString(newBytes.ToArray());
return message;
}
}
}