-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlcdm_2000_api.go
522 lines (391 loc) · 11.5 KB
/
lcdm_2000_api.go
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
package lcdm_2000_api
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"strconv"
"time"
"github.com/tarm/serial"
)
const (
RequestStart byte = 0x04
ResponseStart byte = 0x01
CommunicationIdentify byte = 0x50
TextStart byte = 0x02
TextEnd byte = 0x03
)
type Baud int
const (
Baud9600 Baud = 9600
Baud19200 Baud = 19200
)
type ResponseType byte
const (
ErrorResponse ResponseType = 0x00
AckResponse ResponseType = 0x06
NackResponse ResponseType = 0x15
EotResponse ResponseType = 0x04
)
type StatusCode byte
const (
Good StatusCode = 0x30
NormalStop StatusCode = 0x31
PickupError StatusCode = 0x32
UpperCheckSensorJam StatusCode = 0x33
OverflowBill StatusCode = 0x34
JamExitOrEjectSensor StatusCode = 0x35
JamDivertSensor StatusCode = 0x36
UndefinedCommand StatusCode = 0x37
UpperBillEnd StatusCode = 0x38
CheckSensorEjectSensorCountMismatched StatusCode = 0x3A
BillCountZeroOrOverflow StatusCode = 0x3B
DivertTimeout StatusCode = 0x3C
BillCountError StatusCode = 0x3D
SensorError StatusCode = 0x3E
RejectTrayIsNotRecognised StatusCode = 0x3F
LowerBillEnd StatusCode = 0x40
MotorStop StatusCode = 0x41
TimeoutCheckEjectSensor StatusCode = 0x42
TimeoutDivertEjectSensor StatusCode = 0x43
NoUpperCashBox StatusCode = 0x45
NoLowerCashBox StatusCode = 0x46
DispensingTimeout StatusCode = 0x47
EjectSensorJam StatusCode = 0x48
DiverterNotOperatedNormallyOrSolenoidSensorError StatusCode = 0x49
BillsNotDispensedDiverterAbnormal StatusCode = 0x4A
CountingBillsDivertCheckSensorMismatched StatusCode = 0x4B
LowerCheckSensorJam StatusCode = 0x4C
CountingBillsEjectExitSensorMismatched StatusCode = 0x4D
ReverseJam StatusCode = 0x4E
BillDispensedFromWrongCashBox StatusCode = 0x4F
TimeoutCheckDivertSensor StatusCode = 0x50
)
type CashboxStatusCode byte
const (
Normal CashboxStatusCode = 0x30
NearEnd CashboxStatusCode = 0x31
)
type LCDMDispenser struct {
config *serial.Config
port *serial.Port
logging bool
open bool
timeout time.Duration
}
type SensorStatus struct {
CheckSensor1 bool
CheckSensor2 bool
CheckSensor3 bool
CheckSensor4 bool
DivertSensor1 bool
DivertSensor2 bool
EjectSensor bool
ExitSensor bool
SolenoidSensor bool
UpperNearEnd bool
LowerNearEnd bool
CashBoxUpper bool
CashBoxLower bool
RejectTray bool
}
type response struct {
data ResponseType
err error
}
type responseData struct {
data []byte
err error
}
func NewConnection(path string, baud Baud, logging bool, timeout time.Duration) (LCDMDispenser, error) {
if timeout == 0 {
timeout = 3 * time.Second
}
c := &serial.Config{Name: path, Baud: int(baud), ReadTimeout: timeout, Parity: serial.ParityNone, StopBits: serial.Stop1,
Size: 8}
o, err := serial.OpenPort(c)
res := LCDMDispenser{}
if err != nil {
return res, err
}
res.config = c
res.port = o
res.logging = logging
res.open = true
res.timeout = timeout
return res, nil
}
func (s *LCDMDispenser) Open() error {
if s.port == nil || !s.open {
return errors.New("port already opened")
}
p, err := serial.OpenPort(s.config)
if err != nil {
return err
}
s.port = p
s.open = true
return nil
}
func (s *LCDMDispenser) Close() error {
if s.port == nil || !s.open {
return errors.New("port not opened")
}
err := s.port.Close()
s.open = false
return err
}
func (s *LCDMDispenser) Status() (StatusCode, SensorStatus, error) {
status := SensorStatus{}
err := sendRequest(s, 0x46, []byte{})
if err != nil {
return 0, status, err
}
response, err := readResponse(s)
if err != nil {
return 0, status, err
}
status.CheckSensor1 = (response[2] & (1 << 0)) != 0
status.CheckSensor2 = (response[2] & (1 << 1)) != 0
status.CheckSensor3 = (response[3] & (1 << 3)) != 0
status.CheckSensor4 = (response[3] & (1 << 4)) != 0
status.DivertSensor1 = (response[2] & (1 << 2)) != 0
status.DivertSensor2 = (response[2] & (1 << 3)) != 0
status.EjectSensor = (response[2] & (1 << 4)) != 0
status.ExitSensor = (response[2] & (1 << 5)) != 0
status.SolenoidSensor = (response[3] & (1 << 0)) != 0
status.UpperNearEnd = (response[2] & (1 << 6)) != 0
status.LowerNearEnd = (response[3] & (1 << 5)) != 0
status.CashBoxUpper = (response[3] & (1 << 1)) != 0
status.CashBoxLower = (response[3] & (1 << 2)) != 0
status.RejectTray = (response[3] & (1 << 6)) != 0
return StatusCode(response[1]), status, err
}
func (s *LCDMDispenser) Reset() error {
err := sendRequest(s, 0x44, []byte{})
if err != nil {
return err
}
_, err = readResponse(s)
if err != nil {
return err
}
return nil
}
func (s *LCDMDispenser) UpperDispense(count byte) (StatusCode, CashboxStatusCode, byte, byte, error) {
err := sendRequest(s, 0x45, []byte(fmt.Sprintf("%02d", count)))
if err != nil {
return 0, 0, 0, 0, err
}
response, err := readResponse(s)
if err != nil {
return 0, 0, 0, 0, err
}
val, _ := strconv.ParseUint(string(response[0:2]), 10, 8)
checkSensor := byte(val)
val, _ = strconv.ParseUint(string(response[2:4]), 10, 8)
exitSensor := byte(val)
return StatusCode(response[4]), CashboxStatusCode(response[5]), checkSensor, exitSensor, nil
}
func (s *LCDMDispenser) LowerDispense(count byte) (StatusCode, CashboxStatusCode, byte, byte, error) {
err := sendRequest(s, 0x55, []byte(fmt.Sprintf("%02d", count)))
if err != nil {
return 0, 0, 0, 0, err
}
response, err := readResponse(s)
if err != nil {
return 0, 0, 0, 0, err
}
val, _ := strconv.ParseUint(string(response[0:2]), 10, 8)
checkSensor := byte(val)
val, _ = strconv.ParseUint(string(response[2:4]), 10, 8)
exitSensor := byte(val)
return StatusCode(response[4]), CashboxStatusCode(response[5]), checkSensor, exitSensor, nil
}
func (s *LCDMDispenser) Dispense(upperCount byte, lowerCount byte) (StatusCode, CashboxStatusCode, byte, byte, byte, byte, error) {
err := sendRequest(s, 0x55, []byte(fmt.Sprintf("%02d%02d", upperCount, lowerCount)))
if err != nil {
return 0, 0, 0, 0, 0, 0, err
}
response, err := readResponse(s)
if err != nil {
return 0, 0, 0, 0, 0, 0, err
}
val, _ := strconv.ParseUint(string(response[0:2]), 10, 8)
upperCheckSensor := byte(val)
val, _ = strconv.ParseUint(string(response[2:4]), 10, 8)
upperExitSensor := byte(val)
val, _ = strconv.ParseUint(string(response[0:2]), 10, 8)
lowerCheckSensor := byte(val)
val, _ = strconv.ParseUint(string(response[2:4]), 10, 8)
lowerExitSensor := byte(val)
return StatusCode(response[8]), CashboxStatusCode(response[9]), upperCheckSensor, upperExitSensor, lowerCheckSensor, lowerExitSensor, nil
}
func (s *LCDMDispenser) RomVersion() (string, string, error) {
err := sendRequest(s, 0x47, []byte{})
if err != nil {
return "", "", err
}
response, err := readResponse(s)
if err != nil {
return "", "", err
}
return string(response[2:4]), string(response[4:8]), nil
}
func (s *LCDMDispenser) Ack() {
_, _ = s.port.Write([]byte{0x06})
}
func (s *LCDMDispenser) Nack() {
_, _ = s.port.Write([]byte{0x15})
}
func timeout(timeout time.Duration, r chan response) {
time.Sleep(timeout)
r <- response{err: errors.New("timeout")}
}
func timeoutData(timeout time.Duration, r chan responseData) {
time.Sleep(timeout)
r <- responseData{err: errors.New("timeout")}
}
func readResponse(v *LCDMDispenser) ([]byte, error) {
resp, err := readRespCodeWithTimeout(v)
if err != nil {
return nil, err
}
if resp != AckResponse {
return nil, errors.New("Response not ACK")
}
data, err := readRespDataWithTimeout(v)
if err != nil {
return nil, err
}
v.Ack()
time.Sleep(time.Millisecond * 200)
return data, nil
}
func readRespCodeWithTimeout(s *LCDMDispenser) (ResponseType, error) {
inner := make(chan response)
go func() {
i, v := readRespCode(s)
inner <- response{data: i, err: v}
}()
go timeout(s.timeout, inner)
v := <-inner
return v.data, v.err
}
func readRespCode(v *LCDMDispenser) (ResponseType, error) {
var buf []byte
innerBuf := make([]byte, 256)
totalRead := 0
for ; ; {
n, err := v.port.Read(innerBuf)
if err != nil {
return ErrorResponse, err
}
totalRead += n
buf = append(buf, innerBuf[:n]...)
if totalRead < 1 {
continue
}
break
}
if buf[0] == 0x06 {
if v.logging {
fmt.Printf("lcdm_2000[%v]: <- ACK\n", v.config.Name)
}
return AckResponse, nil // TODO Ack
}
if buf[0] == 0x15 {
if v.logging {
fmt.Printf("lcdm_2000[%v]: <- NAK\n", v.config.Name)
}
return NackResponse, nil
}
if buf[0] == 0x04 {
if v.logging {
fmt.Printf("lcdm_2000[%v]: <- EOT\n", v.config.Name)
}
return EotResponse, nil
}
return ErrorResponse, nil
}
func readRespDataWithTimeout(s *LCDMDispenser) ([]byte, error) {
inner := make(chan responseData)
go func() {
i, v := readRespData(s)
inner <- responseData{data: i, err: v}
}()
go timeoutData(s.timeout, inner)
v := <-inner
return v.data, v.err
}
func readRespData(v *LCDMDispenser) ([]byte, error) {
var buf []byte
innerBuf := make([]byte, 256)
totalRead := 0
lastRead := false
for ; ; {
n, err := v.port.Read(innerBuf)
if err != nil {
return nil, err
}
totalRead += n
buf = append(buf, innerBuf[:n]...)
if len(buf) > 2 && buf[len(buf)-2] == TextEnd {
lastRead = true
}
if lastRead == false {
continue
}
break
}
if buf[0] != ResponseStart || buf[1] != CommunicationIdentify {
fmt.Printf("lcdm_2000[%v]: <- %X\n", v.config.Name, buf)
return nil, fmt.Errorf("Response format invalid")
}
crc := buf[len(buf)-1]
buf = buf[:len(buf)-1]
crc2 := getChecksum(buf)
if crc != crc2 {
return nil, fmt.Errorf("Response verification failed")
}
if buf[2] != TextStart || buf[len(buf)-1] != TextEnd {
return nil, fmt.Errorf("Response format invalid")
}
buf = buf[4 : len(buf)-1]
if v.logging {
fmt.Printf("lcdm_2000[%v]: <- %X\n", v.config.Name, buf)
}
return buf, nil
}
func sendRequest(v *LCDMDispenser, commandCode byte, bytesData ...[]byte) error {
if !v.open {
return errors.New("serial port is closed")
}
buf := new(bytes.Buffer)
length := 6
for _, b := range bytesData {
length += len(b)
}
_ = binary.Write(buf, binary.LittleEndian, RequestStart)
_ = binary.Write(buf, binary.LittleEndian, CommunicationIdentify)
_ = binary.Write(buf, binary.LittleEndian, TextStart)
_ = binary.Write(buf, binary.LittleEndian, commandCode)
for _, data := range bytesData {
_ = binary.Write(buf, binary.LittleEndian, data)
}
_ = binary.Write(buf, binary.LittleEndian, TextEnd)
crc := getChecksum(buf.Bytes())
_ = binary.Write(buf, binary.LittleEndian, crc)
if v.logging {
fmt.Printf("lcdm_2000[%v]: -> %X\n", v.config.Name, buf.Bytes())
}
_, err := v.port.Write(buf.Bytes())
return err
}
func getChecksum(data []byte) byte {
chksum := byte(0)
for _, b := range data {
chksum = chksum ^ b
}
return chksum
}