我一直在代码中使用pySerial 3.4从串行端口(确切地说是RFID芯片读取器/写入器)提取数据。我需要向阅读器发送命令,然后从阅读器中读取结果。
基本上,我正在编写12个字节的命令,然后尝试接收24个字节的输出。
我的问题是:为什么port.read(24)
无法工作,但是res = bytearray(24); port.readinto(res);
成功地工作?
随附完整代码:
import serial
ser = serial.Serial('COM5',9600,timeout=5,rtscts=True,inter_byte_timeout=5)
def compose_find(port):
port.write(bytes.fromhex('555500000003020405'))
port.flush()
return port.read(12)[6] == 0
def compose_read(port,sec_loc=0,block_loc=0,key='F'*12):
assert sec_loc in range(0,16),'sector location is from 0~15'
assert block_loc in range(0,4),'block location is from 0~3'
assert compose_find(port),'cannot read card!'
sec_loc = '0'+str(hex(sec_loc))[2:]
block_loc = '0'+str(hex(block_loc))[2:]
command = '55 55 00 00 00 0E 03 07 00 00 {} {} 60
{}'.format(sec_loc,block_loc,key).replace(' ','')
parity = str(hex(reduce(lambda x,y:x^y,bytes.fromhex(command))))[2:]
if len(parity) == 1: parity = '0'+parity
command += parity
print(command)
port.write(bytes.fromhex(command))
port.flush()
res = bytearray(24)
port.readinto(res)
return res
compose_read(port=ser,sec_loc=8,block_loc=2)
答案 0 :(得分:0)
<VirtualHost MY_IP:443>
ServerName www.mydomain.com
SSLProxyEngine On
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
SSLCertificateFile /etc/letsencrypt/live/mydomain.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/mydomain.com/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
RewriteEngine On
RewriteCond %{HTTP:Upgrade} =websocket [NC]
RewriteRule /(.*) wss://localhost:8080/$1 [P,L]
ProxyPass / http://localhost:8081/
ProxyPassReverse / http://localhost:8081/
</VirtualHost>
<VirtualHost localhost:8081>
DocumentRoot /var/www/www.mydomain.com/
</VirtualHost>
最多读取readinto(buf)
个字节并返回,而len(buf)
将阻塞,直到接收到read(num)
个字节。如果要使num
即使没有足够的可用数据也要返回,则在打开端口时必须指定timeout
。