Serial communication works in minicom but not in python

Viewed 5923

I am trying to communicate with my smartmeter, which works, partly. Basicly, I get the serial data from my meter through a FT232 USB-Serial cable. The smartmeter has a data logger port which sends out a telegram message every 10 seconds (9600 baud, 7 databit, even parity, 1 stopbit). The configuration file I use with minicom is

pu port             /dev/ttyUSB0
pu baudrate         9600
pu bits             7
pu parity           E
pu stopbits         1
pu minit            ~^M~AT S7=45 S0=0 L1 V1 X4 &c1 E1 Q0^M

This works, and I am able to succesfully recieve my data. But my goal was to recieve the data in python so I tried the following script

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.")

This does not seem to work, it just times out after 20 seconds. I have also tried 'cu' with:

cu -l /dev/ttyUSB0 -s 9600 -oe

I found out that when running python script, and then run the minicom script, the python script would get the required data. I've also tried to change the Initialization string to ' ' which works, but if I don't set it at all, minicom doesn't get the data either

Does anyone have any idea ?

2 Answers
Related