Closed VVIERV00 closed 5 years ago
Hi Víctor,
Thank you so much for using mh-z19 ! As you've mentioned, there are lack of consideration about using with Python3.
I've confirmed that with following fix, I can read sensor value on the python3.5
>>> def mh_z19():
... try:
... ser = serial.Serial(serial_dev,
... baudrate=9600,
... bytesize=serial.EIGHTBITS,
... parity=serial.PARITY_NONE,
... stopbits=serial.STOPBITS_ONE,
... timeout=1.0)
... while 1:
... # result=ser.write("\xff\x01\x86\x00\x00\x00\x00\x00\x79")
... result=ser.write(b"\xff\x01\x86\x00\x00\x00\x00\x00\x79")
... s=ser.read(9)
... # if len(s) >= 4 and s[0] == "\xff" and s[1] == "\x86":
... if len(s) >= 4 and s[0] == 0xff and s[1] == 0x86:
... # return {'co2': ord(s[2])*256 + ord(s[3])}
... return {'co2': s[2]*256 + s[3]}
... break
... except:
... traceback.print_exc()
...
>>> mh_z19()
{'co2': 419}
I'll release soon a new version for considering both python2 & python3.
By the way,
Traceback (most recent call last):
File "/home/pi/Desktop/soul/mh_z19-0.1.6/mh_z19/__init__.py", line 73, in mh_z19
s=ser.read(9)
File "/usr/lib/python3/dist-packages/serial/serialposix.py", line 497, in read
raise SerialException('read failed: {}'.format(e))
serial.serialutil.SerialException: read failed: device reports readiness to read but returned no data (device disconnected or multiple access on port?)
For this message, most possible cause might be other process already using the same serial port. Would you please confirm there are no other process using the port? For ex: "python -m mh_z19" running on the other terminal, or set turn on as service...
Thank you for the answer! The Serial port error happens only in the case I explained: it's not due to multi-access (IMO), I also encountered that error in my program and at least in Linux, you can have multiple programs accessing it and it only breaks when 2 of them access it at the same time. (with a delay of 3 seconds it took a minute to collide). I will use that new version. Congrats for your work. ;)
Hi! Like your work on the mh-z19. It works flawless when using as command line (python -m mh_z19) and by downloading the file. I want to use your code with a program i have wrote on python3 (it performs different actions depending on the co2 value) but I always have problems with the import. After solving some of them (as using .encode() ) i cant move foward after this message:
I think this happens because the file is being imported (import init as mh_z19) from a python3 file. What can you suggest me?