DIY个人智能家庭网关——python篇之读取USB串口,获取门磁报警信息

把433M无线解码器的USB串口插入到路由器USB口,默认会出现ttyUSB0串口设备,python读取串口数据代码如下

#!/usr/bin/env python
# -*- coding: utf-8 -*- 
import serial  
from time import sleep  
  
ser = serial.Serial('/dev/ttyUSB0', 9600, timeout=0.5)   
  
print ser.port  
print ser.baudrate  
  
def recv(serial):    
  while True:    
    data =serial.read(64)    
    if data == '':    
      continue  
    else:  
      break  
    sleep(0.02)   
  return data   
  
while True:    
  data =recv(ser)   
  if data != '':   
    print data  

运行后,触发433M无线门磁,输出结果如下

root@OpenWrt:/tmp# ./serialread.py 
/dev/ttyUSB0
9600
{"type":"trigger", "addr":"0x5555","data":"0xc0"}


你可能感兴趣的:(DIY个人智能家庭网关)