-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
176 lines (147 loc) · 7.03 KB
/
Copy pathmain.py
File metadata and controls
176 lines (147 loc) · 7.03 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
168
169
170
171
172
173
174
175
176
import os
import re
import time
from prettytable import PrettyTable
import win32com.client
import serial
from datautils.excellreader import ExcelToGenerator
from datautils.db import MachineMSSQLServer
from commutils.machine import NG_PLC, OK_PLC, PRINT_SIG, RFID_MSG, SCAN_BCODE, STATUS_MSG, MachineCommLink
from rfidutils.rfid_helper import RFIDHelper
LABEL_PATH = "C:\\Users\\PT-PC\\Desktop\\machinelabel.btw"
PRINTER_NAME = "TSC PEX-1231"
RFID_DELAY = 0.2
def print_btw_label(filename, printer_name, data_dict, quantity=1):
# Create an instance of BarTender Application
bt_app = win32com.client.Dispatch('BarTender.Application')
# Open the label template
bt_format = bt_app.Formats.Open(filename, False, '')
# Set printer
bt_format.Printer = printer_name
# Specify the number of copies
bt_format.IdenticalCopiesOfLabel = quantity
# Set field values
for field, value in data_dict.items():
bt_format.SetNamedSubStringValue(field, value)
# Print the label
bt_format.PrintOut(False, False)
# Call the function
if __name__ == '__main__':
_machine_link = MachineCommLink(parity="O")
response = _machine_link.send_and_receive(RFID_MSG)
time.sleep(.5)
response = _machine_link.send_and_receive(SCAN_BCODE)
time.sleep(.5)
response = _machine_link.send_and_receive(NG_PLC)
os.system('cls' if os.name == 'nt' else 'clear')
_barcode_link = serial.Serial("COM1", 9600, timeout=1,parity="N")
_rfid_link = RFIDHelper(device='COM3')
#_rfid_link.inventory()
# file_name = 'a.xlsx'
# sheet_name = 'Sheet1'
# excel_gen = ExcelToGenerator(file_name, sheet_name)
# # Create a generator
# gen = excel_gen.data_generator()
_machinedb = MachineMSSQLServer('172.16.20.1', 'ActiveSooperWizerNCL', 'sa', 'wimetrix')
_lbldata = _machinedb.load_data()
_cltp = True
_card_mappings = {}
pattern = re.compile(r'^(01|02)\d{6}$')
table = PrettyTable(['RFID', 'QR', 'Status','Cut','Bundle','Part'])
# ANSI color codes
RED = "\033[1;31m"
GREEN = "\033[0;32m"
BLUE = "\033[1;34m"
RESET = "\033[0;0m"
_stats = {"Total":len(_lbldata),"printed":0,"Failed":0}
# Use the generator
for row in _lbldata:
_lbl = row._asdict()
print(f"new label printing {_lbl['GroupID']}")
_qr = None
while(True):
response = _machine_link.send_and_receive(STATUS_MSG)
e_val = _machine_link.extract_status_value(response)
if e_val is None:
print(f"Invalid response = {response} Extracted value = {e_val}")
time.sleep(1)
continue
if int(e_val[1])==1 and _cltp == True:
print(f"Printing {_lbl['GroupID']}")
response = _machine_link.send_and_receive(PRINT_SIG)
params = {"WorkOrder":_lbl['OrId'],"PO":_lbl['ProductionOrderCode'],"Bundle":_lbl['BundleCode'],"CutNo":_lbl['CutNo'],"Color":_lbl['Color'],"Part":_lbl['GarPanelDesc'],"QTY":_lbl['BundleQuantity'],"Size":_lbl['Size'],"LOT":_lbl['Lotno'],"QRCode":_lbl['GroupID']}
print_btw_label(LABEL_PATH, PRINTER_NAME,params, 1)
_cltp = False
#time.sleep(1)
#continue
if int(e_val[2]):
print("RFID Signal is High !")
response = _machine_link.send_and_receive(RFID_MSG)
time.sleep(RFID_DELAY)
if _qr is None or _qr=="no ready":
print("QR Error Throwing Out")
response = _machine_link.send_and_receive(NG_PLC)
_stats["Failed"] += 1
_cltp = True
continue
else:
_st = _rfid_link.inventory()
if _st[0] == 1:
print("No Card Trying again")
time.sleep(RFID_DELAY)
_st = _rfid_link.inventory()
if _st[0] == 1:
print("No Card Throwing out")
response = _machine_link.send_and_receive(NG_PLC)
_stats["Failed"] += 1
_cltp = True
continue
if _st[0] == 2:
print("Multi Read Throwing Out")
response = _machine_link.send_and_receive(NG_PLC)
_stats["Failed"] += 1
_cltp = True
continue
if _st[1] in _card_mappings:
print(f"Already Initialized Card {_st[1]} Read Again Throwing Out")
response = _machine_link.send_and_receive(NG_PLC)
_stats["Failed"] += 1
_cltp = True
continue
if not pattern.match(_st[1]):
print("Bad EPC !")
response = _machine_link.send_and_receive(NG_PLC)
_stats["Failed"] += 1
_cltp = True
continue
if not _qr == str(_lbl['GroupID']):
print(f"Bad QR ! Read = {_qr} Expected {_lbl['GroupID']}")
response = _machine_link.send_and_receive(NG_PLC)
_stats["Failed"] += 1
_cltp = True
continue
else:
print (f"QR {_qr} RFID = {_st[1]}")
_card_mappings[_st[1]] = _qr
#params = {"WorkOrder":_lbl['OrId'],"PO":_lbl['ProductionOrderCode'],"Bundle":_lbl['BundleCode'],"CutNo":_lbl['CutNo'],"Color":_lbl['Color'],"Part":_lbl['GarPanelDesc'],"QTY":_lbl['BundleQuantity'],"Size":_lbl['Size'],"LOT":_lbl['Lotno'],"QRCode":_lbl['GroupID']}
table.add_row([RED + _st[1] + RESET, _qr, 'Success',_lbl['CutNo'],_lbl['BundleCode'],_lbl['GarPanelDesc']])
_stats["printed"] += 1
os.system('cls' if os.name == 'nt' else 'clear')
print(table)
print(f"Total = {_stats['Total']} Printed = {_stats['printed']} Failed = {_stats['Failed']}")
if _machinedb.upload_data(_st[1],_qr):
print("Error Uploading to Database! ")
response = _machine_link.send_and_receive(NG_PLC)
response = _machine_link.send_and_receive(OK_PLC)
_machinedb.save_id_to_file('id.txt',_lbl['ID'])
_qr = None
_st = None
_cltp = True
break
if int(e_val[3]):
response = _machine_link.send_and_receive(SCAN_BCODE)
_qr = _barcode_link.readline().decode().strip()
#print(f"Barcode Read = {_qr}")
#
time.sleep(.1)
# _rfid_transport.close()