Python2写的代码,Python3运行不报错却不出结果

weixin_40101242 2018-09-05 11:01:47
#!/usr/bin/python
# coding=utf-8
# "DATASHEET": http://cl.ly/ekot
# https://gist.github.com/kadamski/92653913a53baf9dd1a8
from __future__ import print_function
import serial, struct, sys, time, json

DEBUG = 0
CMD_MODE = 2
CMD_QUERY_DATA = 4
CMD_DEVICE_ID = 5
CMD_SLEEP = 6
CMD_FIRMWARE = 7
CMD_WORKING_PERIOD = 8
MODE_ACTIVE = 0
MODE_QUERY = 1

ser = serial.Serial()
ser.port = "/dev/ttyUSB0"
ser.baudrate = 9600

ser.open()
ser.flushInput()

byte, data = 0, ""

def dump(d, prefix=''):
print(prefix + ' '.join(x.encode('hex') for x in d))

def construct_command(cmd, data=[]):
assert len(data) <= 12
data += [0,]*(12-len(data))
checksum = (sum(data)+cmd-2)%256
ret = "\xaa\xb4" + chr(cmd)
ret += ''.join(chr(x) for x in data)
ret += "\xff\xff" + chr(checksum) + "\xab"

if DEBUG:
dump(ret, '> ')
return ret

def process_data(d):
r = struct.unpack('<HHxxBB', d[2:])
pm25 = r[0]/10.0
pm10 = r[1]/10.0
checksum = sum(ord(v) for v in d[2:8])%256
return [pm25, pm10]
#print("PM 2.5: {} μg/m^3 PM 10: {} μg/m^3 CRC={}".format(pm25, pm10, "OK" if (checksum==r[2] and r[3]==0xab) else "NOK"))

def process_version(d):
r = struct.unpack('<BBBHBB', d[3:])
checksum = sum(ord(v) for v in d[2:8])%256
print("Y: {}, M: {}, D: {}, ID: {}, CRC={}".format(r[0], r[1], r[2], hex(r[3]), "OK" if (checksum==r[4] and r[5]==0xab) else "NOK"))

def read_response():
byte = 0
while byte != "\xaa":
byte = ser.read(size=1)

d = ser.read(size=9)

if DEBUG:
dump(d, '< ')
return byte + d

def cmd_set_mode(mode=MODE_QUERY):
ser.write(construct_command(CMD_MODE, [0x1, mode]).encode())
read_response()

def cmd_query_data():
ser.write(construct_command(CMD_QUERY_DATA).encode())
d = read_response()
values = []
if d[1] == "\xc0":
values = process_data(d)
return values

def cmd_set_sleep(sleep=1):
mode = 0 if sleep else 1
ser.write(construct_command(CMD_SLEEP, [0x1, mode]).encode())
read_response()

def cmd_set_working_period(period):
ser.write(construct_command(CMD_WORKING_PERIOD, [0x1, period]).encode())
read_response()

def cmd_firmware_ver():
ser.write(construct_command(CMD_FIRMWARE).encode())
d = read_response()
process_version(d)

def cmd_set_id(id):
id_h = (id>>8) % 256
id_l = id % 256
ser.write(construct_command(CMD_DEVICE_ID, [0]*10+[id_l, id_h]).encode())
read_response()

if __name__ == "__main__":
while True:
cmd_set_sleep(0)
cmd_set_mode(1);
for t in range(15):
values = cmd_query_data();
if values is not None:
print("PM2.5: ", values[0], ", PM10: ", values[1])
time.sleep(2)

# open stored data
with open('/var/www/html/aqi2.json') as json_data:
data = json.load(json_data)

# check if length is more than 100 and delete first element
if len(data) > 100:
data.pop(0)

# append new values


data.append({'pm25': values[0], 'pm10': values[1], 'time': time.strftime("%d.%m.%Y %H:%M:%S")})


# save it
with open('/var/www/html/aqi2.json', 'w') as outfile:
json.dump(data, outfile)

print("Going to sleep for 1min...")
cmd_set_mode(0)
cmd_set_sleep()
time.sleep(60)
...全文
1010 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
嘻哈路过 2018-09-19
  • 打赏
  • 举报
回复
py2和py3的区别,以及你使用的解释器是不是合适的,最好办法是一步步调试出来看结果
欢乐的小猪 2018-09-06
  • 打赏
  • 举报
回复
Python2出结果吗?
调试按步跟踪
oyljerry 2018-09-06
  • 打赏
  • 举报
回复
多在代码中增加一些print等打印输出结果,看你的代码在哪一步有问题。
weixin_40101242 2018-09-05
  • 打赏
  • 举报
回复
之前Python2在write时没有encode,encode以后不报错却读不出数据。
weixin_40101242 2018-09-05
  • 打赏
  • 举报
回复
这是一个传感器写入数据的程序,运行的时候不报错,但就是读不出数据

37,720

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧