'Drunk' input from readline, OK from other programs (reading smart meters P1 port)
Question: I'm new to Python and want to read my smart meters P1 port using a Raspberry
Pi and Python. Problem: the input looks like some component is drunk. I'm sure
it's pretty simple to fix, but after several hours of searching and trying,
had to seek help.

When reading the P1 port with CU etc. everything is fine so the hardware etc.
is OK. Using a serial to USB converter from dx.com ([this
one](http://dx.com/p/usb-to-rs232-serial-port-adapter-transparent-
green-24512?item=4 "this"))

Command and (part of) the output: **cu -l /dev/ttyUSB0 -s 9600 --parity=none**

    
    
    0-0:96.1.1(205A414246303031363631323463949271)
    1-0:1.8.1(03118.000*kWh)
    

However, when trying to read it from Python, the input becomes gibberish (but
at least sort of consistant):

    
    
    0-0:96.±.±(²05A´±´²´630303±39363±3²3´639·3±3²©
    ±-0:±.¸.±(03±±¸.000ªë×è©
    

How to fix this? The code I'm using is:

    
    
    import serial
    
    ser = serial.Serial()
    ser.baudrate = 9600
    ser.bytesize=serial.SEVENBITS
    ser.parity=serial.PARITY_EVEN
    ser.stopbits=serial.STOPBITS_ONE
    ser.xonxoff=0
    ser.rtscts=0
    ser.timeout=20
    ser.port="/dev/ttyUSB0"
    
    ser.close()
    ser.open()
    print ("Waiting for P1 output on "  + ser.portstr)
    
    counter=0
    #read 20 lines    
    while counter < 20:
        print ser.readline()
        counter=counter+1
    
    try:
        ser.close()
        print ("Closed serial port.")
    except:
        sys.exit ("Couldn't close serial port.")
    

Have already tried messing with baudrate etc. but that doesn't make any
difference.


Answer: I'm not very familiar with the `serial` module, but I noticed that your `cu`
command assumes there is no parity bit (`--parity=none`), but your python
script assumes there is an even parity bit (`ser.parity=serial.PARITY_EVEN`).
I would try

    
    
    ser.parity=serial.PARITY_NONE
    

And if there's no parity bit, you'll also probably want

    
    
    ser.bytesize=serial.EIGHTBITS
    

