【发布时间】:2019-08-02 12:50:11
【问题描述】:
我有一个网站和一个 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,但我认为监视器环路阻塞了串行端口以及与端口的通信。
【问题讨论】:
标签: python windows selenium arduino