Scanner.cs
4.65 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
162
163
164
165
166
167
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;
namespace BarcodeScannerClient
{
public class BarCodeScanner
{
private SerialPort serialport = new SerialPort();
private string port = "COM1";
public string Port
{
get
{
return this.port;
}
set
{
this.port = value;
}
}
public BarCodeScanner()
{
this.serialport.DataReceived += new SerialDataReceivedEventHandler(serialport_DataReceived);
}
public void Listen(string port)
{
this.port = port;
Listen();
}
public void Listen()
{
try
{
if (this.serialport.IsOpen)
this.serialport.Close();
this.serialport.PortName = this.port;
this.serialport.Open();
buffertimer.Elapsed += new System.Timers.ElapsedEventHandler(buffertimer_Elapsed);
}
catch
{
System.Windows.Forms.MessageBox.Show("Cannot open barcode scanner on port" + this.serialport);
}
}
void buffertimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
buffertimer.Enabled = false;
Console.WriteLine(this.buffer);
// If input is not regular card flush it!
/*
if (!buffer.StartsWith("C") && !buffer.StartsWith("R")) buffer = "";
// if input is ready send it
if (buffer.EndsWith("E") || buffer.EndsWith("E\r"))
{
// Trim white space away
if (buffer.EndsWith("E\r"))
buffer = buffer.Trim();
onBarcodeReceived(buffer);
buffer = "";
}
*/
onBarcodeReceived(buffer.Trim());
buffer = "";
}
public void Close()
{
try
{
this.serialport.Close();
}
catch (Exception e)
{
}
}
public enum BarcodeType { Card, Receipt };
private string buffer;
System.Timers.Timer buffertimer = new System.Timers.Timer(500);
void serialport_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
// Add new data to buffer
this.buffer += serialport.ReadExisting();
buffertimer.Start();
}
private void onBarcodeReceived(string text)
{
if (BarcodeReceived == null) return;
text = text.TrimStart(new char[]{'0'});
BarcodeReceived(text);
//int data = 0 ;
/*
try
{
data = Convert.ToInt32(text.Substring(1, text.Length - 2));
}
catch
{
Console.WriteLine("Error in barcode parser! :" + text + ":");
}
if (text.StartsWith("C"))
{
BarcodeReceived(data, BarcodeType.Card);
}
else if (text.StartsWith("R"))
{
BarcodeReceived(data, BarcodeType.Receipt);
}
else
{
System.Windows.Forms.MessageBox.Show("Unknown barcode!");
}
*/
}
//public delegate void Barcode (int data, BarcodeType type);
public delegate void Barcode(string data);
public event Barcode BarcodeReceived;
public static int CalculateEanChecksum(string code)
{
if (code == null || code.Length != 12)
throw new ArgumentException("Code length should be 12, i.e. excluding the checksum digit");
int sum = 0;
for (int i = 0; i < 12; i++)
{
int v;
if (!int.TryParse(code[i].ToString(), out v))
throw new ArgumentException("Invalid character encountered in specified code.");
sum += (i % 2 == 0 ? v : v * 3);
}
int check = 10 - (sum % 10);
return check % 10;
}
private bool CheckEanCode(string code)
{
if (code == null || code.Length != 13)
return false;
int res;
foreach (char c in code)
if (!int.TryParse(c.ToString(), out res))
return false;
char check = (char)('0' + CalculateEanChecksum(code.Substring(0, 12)));
return code[12] == check;
}
}
}