DLScanSocket.cs
5.0 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
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
{ /// <summary>
/// 扫码枪链接
/// </summary>
private TcpClient scannerSocket;
/// <summary>
/// 扫描枪 是否开始运行
/// </summary>
private bool isScannerRun = false;
private string ScannerIP = "";
private int ScannerPort = 0;
public delegate void OnCodeRevice(string[] codeList);
public event OnCodeRevice CodeReviceEvent;
public DLScanSocket(string serverIp, int port,OnCodeRevice revicePro)
{
this.ScannerIP = serverIp;
this.ScannerPort = port;
this.CodeReviceEvent += revicePro;
}
/// <summary>
/// 开启扫码枪
/// </summary>
public 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 void TimerCheck()
//{
// if (!scannerSocket.IsConnected())
// {
// StartConnect( false );
// LogUtil.info( "重新连接扫码枪【" + ScannerIP + "】 ");
// }
//}
/// <summary>
/// 停止扫码枪
/// </summary>
public void Close()
{
try
{
scannerSocket.close();
isScannerRun = false;
}
catch (Exception ex)
{
LogUtil.error("出错:" + ex);
}
}
/// <summary>
/// 发送扫码命令开始扫码
/// </summary>
public void BeginScan()
{
try
{
string str = ConfigAppSettings.GetValue(Setting_Init.StartScan_CMD);
scannerSocket.send(str);
}
catch (Exception ex)
{
LogUtil.error("【" + ScannerIP + "】开始扫码错误:" + ex.ToString());
}
}
public string spiltStr = "##";
/// <summary>
/// 扫码枪数据接收
/// </summary>
/// <param name="message"></param>
protected virtual 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;
}
}
}