hyer
Posts: 22
Joined: Fri Jun 24, 2016 12:33 pm

serialport write python 3

Tue Jul 05, 2016 5:43 pm

Hi!
I have a question about writing to the serialport with a python 3 script. My project is setting up a sensor on a RPi and sending the sensor data via serial port (with XRF radios). I have opened my serialport, no problems there, and I know that i can send data through (tried with other scripts).

Is the line: port.write("PM2.5 - ", float(pm2hb + pm2lb*256)/10.0 ," PM10 - ", float(pm10hb + pm10lb*256)/10.0) correct?

Can i write: port.write like this in Python 3?

CODE:

#!/usr/bin/env python3

import serial

# check for the existence of /dev/tty**** !!!
# drivers may be based on the CH34x USB SERIAL CHIP:
# https://tzapu.com/making-ch340-ch341-se ... capitan-os$
# http://www.microcontrols.org/arduino-un ... usb-driver$
#
ser = serial.Serial('/dev/ttyUSB0', baudrate=9600, stopbits=1, parity="N", tim$
port = serial.Serial("/dev/ttyAMA0", baudrate=9600, timeout=3.0)
#
while True:
s = ser.read(1)
if ord(s) == int("AA",16):
s = ser.read(1)
if ord(s) == int("C0",16):
s = ser.read(7)
a = []
for i in s:
a.append(i)
#print(a)
pm2hb= s[0]
pm2lb= s[1]
pm10hb= s[2]
pm10lb= s[3]
cs = s[6]
# we should verify the checksum... it is the sum of bytes 1-6 truncated...

try:
print("PM2.5 - ", float(pm2hb + pm2lb*256)/10.0 ," PM10 - ", float(pm10hb + pm10lb*256)/10.0)
port.write("PM2.5 - ", float(pm2hb + pm2lb*256)/10.0 ," PM10 - ", float(pm10hb + pm10lb*256)/10.0)

except:
pass
else:
pass

hyer
Posts: 22
Joined: Fri Jun 24, 2016 12:33 pm

Re: serialport write python 3

Wed Jul 06, 2016 5:10 pm

Dont bother. Solves it with bytearray.

Return to “Python”