我有一个网站和一个USB RFID阅读器。 python脚本监视与rfid阅读器的USB连接。如果在脚本的开头未连接阅读器,则网站内容向右滑动,并显示说明连接rfid阅读器的usb电缆的说明。如果已连接,它将向左滑动,并向用户显示使用rfid卡进行身份识别。当视口中有某些内容时,阅读器应仅开始读取rfid数据。但是我无法执行此步骤,因为串行通信似乎被阻止。
import serial
import mysql.connector
import time
import datetime
from serial.tools import list_ports
from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.common.keys import Keys
capabilities = webdriver.DesiredCapabilities().FIREFOX
capabilities["marionette"] = True
binary = FirefoxBinary('C:/Program Files/Mozilla Firefox/firefox.exe')
driver = webdriver.Firefox(firefox_binary=binary, capabilities=capabilities, executable_path="C:/Python37x64/geckodriver.exe")
# ------------------ USB monitorloop -------------------------------------
stop = 0
swipe = 0
driver.get('https://website.php')
while True:
try:
myports = [tuple(p) for p in list(serial.tools.list_ports.comports())]
arduino_port = [port for port in myports if 'COM3' in port ][0]
def check_presence(correct_port, interval=0.1):
global swipe
global stop
while True:
myports = [tuple(p) for p in list(serial.tools.list_ports.comports())]
if arduino_port not in myports:
stop = 1
swipe = swipe + 1
if swipe == 1:
print ("Arduino has been disconnected!")
driver.execute_script("$('.in_viewport,#usb_connect, #header_usb_connect').animate({ left: '+='+'100vw'});");
time.sleep(1.0)
else:
continue
else:
if swipe >= 1 and stop == 1:
swipe = 0
print ("Arduino connected!")
driver.execute_script("$('.in_viewport,#usb_connect, #header_usb_connect').animate({ left: '-='+'100vw'});");
time.sleep(1.0)
else:
continue
import threading
port_controller = threading.Thread(target=check_presence, args=(arduino_port, 0.5,))
port_controller.setDaemon(True)
port_controller.start()
break
except:
stop = 1
if swipe == 0:
print("Connect USB cable")
driver.execute_script("$('.in_viewport,#usb_connect, #header_usb_connect').animate({ left: '+='+'100vw'});");
time.sleep(1.0)
swipe = 1
continue
else:
time.sleep(1.0)
# --------- connecting to COM 3 and database -----------------------
device_port = 'COM3'
baud = 9600
while True:
try:
print ("Trying...",device_port)
connect_arduino = serial.Serial(device_port, baud)
print ("Successfully connected to",device_port)
print ("Try to connect to database")
db = mysql.connector.connect(host="",port="",user="",passwd="",db="")
print ("Successfully connected to database")
break
except mysql.connector.Error as err:
print("Something went wrong: {}".format(err))
print ("failed to connect to database")
time.sleep(1)
continue
# ------- reading the card identification number and current time -------------
while True:
try:
print ("Reading USB device")
rfid_data = connect_arduino.readline()
now = datetime.datetime.now()
print (rfid_data.decode('utf-8'),"read on", now.strftime("%d-%m-%Y"), "at", now.strftime("%H:%M:%S"))
time.sleep(2)
break
except:
time.sleep(2)
continue
我希望能够serial.readline()
rfid_data
,但我认为监视器循环正在阻止串行端口以及与端口的通信。
答案 0 :(得分:0)
我认为您最大的问题是阅读循环中的break
语句。它正在按预期方式工作并破坏了正常的循环流程,因此您只需调用一次串行端口读取功能。
改善循环的一种方法是检查RX缓冲区是否为空:
while True:
if connect_arduino.inWaiting() != 0:
print ("Reading USB device")
rfid_data = connect_arduino.readline()
now = datetime.datetime.now()
print (rfid_data.decode('utf-8'),"read on", now.strftime("%d-%m-%Y"), "at", now.strftime("%H:%M:%S"))
如果仅在确定缓冲区中将有数据并且您知道RFID读取器将readline()
作为终止字符发送时才调用\r\n
,请确保您将始终读取标签。
编辑:看来您要嗅探的是端口。您不能直接从两个不同的应用程序中打开同一端口。如果您使用的是Windows,则可以尝试this solution。
答案 1 :(得分:0)
很抱歉在这里介绍了基础知识,但是您是否检查过可以使用终端仿真器读取其他端口的串行端口?
我在串行端口(尤其是USB版本)上遇到了无尽的问题。当我不想要它时,可能是其他原因抢占了该端口,或者是由于将USB映射到COM号而出现了某些行为。有时您需要其他一些条件来仔细检查它们。