远程读卡器web客户端(nodejs+websocket实现实时指令交互)
? ? 工具目的:在公司對現場的卡片進行測卡,讀寫卡測試,遠程診斷卡片問題,快速定位和解決現場問題。
之前的那一個版本的小工具,遠程讀卡器web客戶端,實現原理是把讀寫卡服務裝在遠程(現場)的電腦上,這樣有一些缺點,比如現場電腦必須開啟端口映射,讓客戶端能否訪問到。只能寫好腳本,執行結束后才能看到結果。必能實時的看到指令與卡片的交互過程。
? ? 這次用nodejs的express web框架,實現一個簡易的讀寫卡客戶端,并且做成聊天室的樣式,且允許多人觀看和操作執行結果。 讀寫卡服務不再裝在了現場,所以現場網絡只需要能訪問外網即可,無需開端口映射。
? ? 現場電腦只需要裝一個客戶端,這個客戶端完成功能是串口操作讀卡器,通過socket連接服務端。等于是服務端放到了公司這邊來了。只需公司這邊開啟一個外網映射,全國各地的終端讀卡器都可以接入進來??吹降男Ч褪?“讀卡器XXX進入了聊天室”,就可以發指令和它聊天啦
?附:運行結果截圖
截圖2:
截圖3:客戶端的顯示
客戶端用python實現,完成串口轉TCP。
#coding=utf-8 #author:yangyongzhen #QQ:534117529 #'CardTest TcpServer - Simple Test Card Tool 1.00' import sys,threading,time; import serial; import binascii,encodings; import re; import os; from socket import * from struct import *; #from myutil import *; #name: myutil.pymylock = threading.RLock() Server_IP = '' Srever_Port = ''def print_hex1(s,prev='0x'):for c in s:print '%s%02x' %(prev,ord(c)),print def print_hex(s):for c in s:print '%02x' %(ord(c)),printdef hexto_str(s):r =''for c in s:r += '%02x' %(ord(c))return r def strto_hex(s):r = s.decode('hex')return r #''代表服務器為localhost#在一個非保留端口號上進行監聽class ComThread:def __init__(self, Port=0):self.l_serial = None;self.alive = False;self.waitEnd = None;self.port = Port;#TCP部分#self.sockobj = socket.socket(socket.AF_INET, socket.SOCK_STREAM)self.connection = None#數據self.snddata = ''self.rcvdata = ''def waiting(self):if not self.waitEnd is None:self.waitEnd.wait();def SetStopEvent(self):if not self.waitEnd is None:self.waitEnd.set();self.alive = False;self.stop();def start(self):self.l_serial = serial.Serial();self.l_serial.port = self.port;self.l_serial.baudrate = 115200;self.l_serial.timeout = 2; #秒self.l_serial.open();if self.l_serial.isOpen():self.waitEnd = threading.Event();self.alive = True;print 'open serial port %d ok!\n' %(self.port+1)print 'baudrate:115200 \n'self.thread_read = None;self.thread_read = threading.Thread(target=self.FirstReader);self.thread_read.setDaemon(1);self.thread_read.start();self.thread_write = None;self.thread_write = threading.Thread(target=self.FirstWriter);self.thread_write.setDaemon(1);self.thread_write.start();#TCP部分self.thread_TcpClient = None;self.thread_TcpClient = threading.Thread(target=self.TcpClient);self.thread_TcpClient.setDaemon(1);self.thread_TcpClient.start();self.thread_TcpSend = None;self.thread_TcpSend = threading.Thread(target=self.TcpSend);self.thread_TcpSend.setDaemon(1);self.thread_TcpSend.start();return True;else:return False;def FirstReader(self):while self.alive:# 接收間隔time.sleep(0.1);try:data = '';n = self.l_serial.inWaiting();if n:data = data+self.l_serial.read(n);#for l in xrange(len(data)):#print '%02X' % ord(data[l]),# 發送數據print u'->請求:'print data;mylock.acquire() self.snddata = datamylock.release()#print_hex(data);# 判斷結束except Exception, ex:print str(ex);self.waitEnd.set();self.alive = False;def FirstWriter(self):while self.alive:# 接收間隔time.sleep(0.1);try:#snddata = raw_input('\nenter data send:\n')if self.rcvdata!='':self.l_serial.write(self.rcvdata); print u'-<應答:'print self.rcvdata;mylock.acquire() self.rcvdata = '';mylock.release()#print_hex(snddata);except Exception, ex:print str(ex);self.waitEnd.set();self.alive = False;def TcpClient(self):while True:# 接收間隔time.sleep(0.1);self.connection = socket(AF_INET, SOCK_STREAM);self.connection.connect((Server_IP, int(Server_Port)));print 'Connect to Server OK!';self.snddata = ''self.rcvdata = ''while True:#讀取客戶端套接字的下一行data = self.connection.recv(1024)#如果沒有數量的話,那么跳出循環if not data: break#發送一個回復至客戶端mylock.acquire() self.snddata = ''self.rcvdata = datamylock.release()#connection.send('Echo=>' + data)self.connection.close()self.waitEnd.set();self.alive = False;def TcpSend(self):while True:# 接收間隔time.sleep(0.1);while True:time.sleep(0.1);try:if not self.connection is None:if self.snddata != '':self.connection.send(self.snddata)mylock.acquire() self.rcvdata = ''self.snddata = ''mylock.release()except Exception, ex:pass def stop(self):self.alive = False;self.thread_read.join();if self.l_serial.isOpen():self.l_serial.close();#測試用部分 if __name__ == '__main__':print 'Serial to Tcp Tool 1.00\n' print 'Author:yangyongzhen\n'print 'QQ:534117529\n'print 'Copyright (c) ***** 2015-2016.\n'Server_IP = raw_input('please enter ServerIP:')print 'Server_IP: %s' %(Server_IP)Server_Port = raw_input('please enter ServerPort:')print 'Server_Port: %s' %(Server_Port)com =raw_input('please enter com port(1-9):')rt = ComThread(int(com)-1);try:if rt.start():rt.waiting();rt.stop();else:pass; except Exception,se:print str(se);if rt.alive:rt.stop();os.system("pause")print '';print 'End OK .';del rt;-------------------------------------------------------------------------------------------
成功不是追求別人眼中的最好,而是把自己能做的事情做得最好。
每個人都應該有夢想,這才是生命的意義。
做事情貴在堅持,只有這份堅持,才實踐了意義。
處處留心皆學問,愛學習,愛思考。
在這里分享學習,分享感悟,共同進步。
凝聚學習的圈子,思考的圈子。
掃碼關注個人微信公眾號:aazhen1987
凝聚學習和思考的圈子。
--------------------------------------------------------------------------------------------
總結
以上是生活随笔為你收集整理的远程读卡器web客户端(nodejs+websocket实现实时指令交互)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 传感器集成温度传感器(DS1820)温度
- 下一篇: Kerberos协议内容详解