Serial communication. Sending DTR in the right way? - python

I'm dealing with a gm29 by Sony Ericsson.
The datasheet says that plugging the power is not sufficient to switch on the modem. It says:
activate the RS232 control line DTR, high for > 0.2s.
I'm writing some tests in python, but:
#!/usr/bin/env python
import serial
from time import sleep
socket = serial.Serial('/dev/ttyS0',
baudrate=9600,
bytesize=serial.EIGHTBITS,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
timeout=1,
xonxoff=0,
rtscts=0
)
socket.setDTR(True)
sleep(3)
socket.setDTR(False)
try:
while True:
socket.write('AT'+chr(13));
sleep(1)
print "Reading"
print socket.readlines()
except:
socket.close()
does not works... I there a way to get DTR high in other ways? Let's say minicom or some other stuff? Or, easily, am I missing something?
Thanks in advance.
Ok, that was driving me mad. The clue is that the power supplier was "broken", or better, it works good testing with a tester, but plugging on the modem some wires moves and does not carry voltage...
Thanks anyway for the answer, marked as correct 'couse it was :D

There are several things that occur to me here.
1) the spec says that DTR is active low, so you may need to swap the true and false values to setDTR(), depending on who is confused here.
2) You are setting DTR to false after you wake the modem. This tells the modem to go offline, and ignore all input till it goes true again. Try the following:
import serial
from time import sleep
conn = serial.Serial('/dev/ttyS0',
baudrate=9600,
bytesize=serial.EIGHTBITS,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
timeout=1,
xonxoff=0,
rtscts=0
)
# Wake Modem
conn.setDTR(True)
sleep(3)
conn.setDTR(False)
sleep(5)
# Start talking
conn.setDTR(True)
try:
while True:
conn.write('AT'+chr(13));
print conn.readline() # readlines() will probably never return.
finally:
conn.close()
3) socket is probably a bad name for your serial connection.

Related

Unable to send/receive data via HC-12/UART in Python

I've written some code to communicate between two Raspberry Pi's, using identical HC-12 433Mhz transceivers. I was able to successfully echo between the two Pi's using a direct serial connection and echo/cat, however am unable to replicate this using HC-12s, which theoretically work by a similar principal. I'm using the port ttyAMA0 on both for this example, but ttyS0 is also available and have tried every combination of these ports.
The following code is common to both the sending and receiving, just writing once for sake of brevity:
import serial
import time
ser = serial.Serial(
port = "/dev/ttyAMA0",
baudrate = 9600,
parity = serial.PARITY_NONE,
stopbits = serial.STOPBITS_ONE,
bytesize = serial.EIGHTBITS
)
print("Serial status: " + str(ser.isOpen()))
This is the sending program:
while True:
print("Sending...")
ser.write("hello\n".encode())
time.sleep(1)
And the receiving program:
while True:
print("Receiving...")
data = ser.readlines()
print(data.decode())
The sending program simply loops as expected, but the receiver prints "Receiving...", and then nothing.
When I keyboard interrupt the receiving program at that point, it says it is currently up to data = ser.readlines().
Any help would be much appreciated - I've spent the better part of the last week trawling and exhausting forums and READMEs to no avail, and this is literally my last option. Am close to insanity on this one!
The pyserial readlines() function relies on the timeout parameter to know when end-of-file is reached - this is warned about in the doco. So with no timeout, the end never occurs, so it keeps buffering all lines read forever.
So you can just add a timeout to the serial port open, and your existing code will begin to work.
ser = serial.Serial(
port = "/dev/ttyAMA0",
baudrate = 9600,
parity = serial.PARITY_NONE,
stopbits = serial.STOPBITS_ONE,
bytesize = serial.EIGHTBITS,
timeout = 2 # seconds # <-- HERE
)
A better approach might be to use readline() (note singular, no 's'), for each line in turn:
print( "Receiving..." )
while True:
try:
data = ser.readline()
print( data.decode() )
# TODO - something with data
except:
print( "Error reading from port" )
break
As that will allow the code to act on the input line-by-line.
Use Serial.read_until method. The default termination character is \n.
For example,
data = ser.read_until()
print(data)

Python raspberry pi serial sim868 Login incorrect

why sometime it is working?
I try to call at command.
How can I fix it?
import serial
import time
port = serial.Serial("/dev/ttyS0", baudrate=115200, timeout=1)
port.write('AT'+'\r\n')
print port.read(10)
time.sleep(.1)
output
Perhaps the serial port is kept open and you need to close it before accessing it again?
Try putting the delay before the read:
time.sleep(.1)
print port.read(10)

pyserial: I received that I send

I'm tried to get the answer from a machine throught serial's com.
but i dont why i am receiving that i send!
with serial.Serial(port,
baudrate=9600,
bytesize=serial.EIGHTBITS,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
#timeout=0.5,
xonxoff=False,
rtscts=False,
dsrdtr=False
) as ser:
print(port,'opened!')
ser.reset_input_buffer()
o1='at+cgmr\r\n'
x=ser.write(o1.encode()) #without encode() error!
print('sended',x,'bytes','-->',o1)
sleep(1)
y=ser.readline(10) #ser.readline() is the same and read(10) as well
print('answer',y)
And the output is:
COM3 opened!
sended 8 bytes --> at+cgmr
respuesta b'at+cgmr\r\r\n'
Teorically i have to receive 'ok' or 'not ok'.
any idea?
Probably the device echoes back what it got and then sends its reply. Have you tried to increase the number of bytes you receive?
There is a chance that if you do y = ser.read(100) (with timeout enabled), you will get everything you asked for.
An alternative is to read exactly as many bytes as there are available:
y = ser.read(ser.in_waiting)
or
y = ser.read(ser.inWaiting())
(depending on the version of pyserial).

Python send data Serial to Microcontroller STM32F4

So this is the script I am using to send each 2 seconds for four time the Time in microsecond to the microcontroller stm32f4 but unfoturnately it only sends some numbers(from 1-->4) which are not the same as when I do a print,it is like random numbers .
import time
import serial from datetime
import datetime from time
import gmtime, strftime
ser = serial.Serial(
port='/dev/ttyACM0',
baudrate=115200,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS
)
ser.writeTimeout = 0
ser.isOpen()
TM1 = int(round(time.time()*1000000))
ser.write(str(TM1).encode())
#ser.write( str(TM1)+" \r\n")
time.sleep(2)
TM2 = int(round(time.time()*1000000))
ser.write(str(TM2)+" \r\n")
time.sleep(2)
TM3 = int(round(time.time()*1000000))
ser.write(str(TM3)+" \r\n")
time.sleep(2)
TM4 = int(round(time.time()*1000000))
ser.write(str(TM4)+" \r\n")
I cannot see anything obviously wrong at first sight. I have a almost identical snippet of code running here that works. My guess would be that the settings of the serial port do not match. Double check that the baudrate, parity and stopbit settings match.
Second guess would be a mess-up with encodings. Have you set your default encoding in python to utf-8? If so you could try
ser.write(str(TM1).encode('ascii'))
Posting the output you get would help as well.
Edit: to avoid the microcontroller skipping some characters. You could use a small function like this. (I used that to send commands to a sensor that had the same issues. When I sent a command like this `ser.write("start logging") it would receive something like "sart lgging".
def write_safe(cmd):
for x in cmd:
ser.write(x)
sleep(0.05)
ser.write('\r\n')

Python PySerial changing settings on timeout

I have a question about Python and PySerial.
On my Raspberry Pi i want to read a serial port from a device. I got 2 types of devices i want to read from. They both got different settings:
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
and:
ser = serial.Serial()
ser.baudrate = 115200
ser.bytesize=serial.EIGHTBITS
ser.parity=serial.PARITY_NONE
ser.stopbits=serial.STOPBITS_ONE
ser.xonxoff=1
ser.rtscts=0
ser.timeout=20
ser.port="/dev/ttyUSB0
This is the code to read the serial port:
try:
ser.open()
except:
sys.exit ("Error opening %s." % ser.name)
t_count = 0
while t_count < 20:
t_line = ''
try:
t_raw = ser.readline()
except serial.SerialException:
sys.exit ("Serial port %s could not be read." % ser.name )
t_str = str(t_raw)
t_line = t_str.strip()
print (t_line)
t_count += 1
try:
ser.close()
except:
sys.exit ("Oops %s. Program aborted. Could not close serial port." % ser.name )
So when i connect to a device with 115200 but the device runs 9600, i get timeouts ofcourse. But in my program it will just time out twenty times (times the for loop will run), and no exception to be thrown. Not even just before the program exits after looping 20 times. No error message nothing.
What i want to achieve is the following, i want to make the python script self detecting what he is connected to. When the readline() times out 20 times it should change settings. (by running another function or something).
I cant check if the readline returns something empty, because there are empty lines in the serial message.
Is there any way to get the right exception? Or any other smart way to solve this?
(By the way, i am sure the settings work. As i tested them both and run fine.)
Thanks in advance.
Cheers!
Easiest way would be to create a handshake function,
send something with one serial handler that you know the device will respond to correctly. If the answer is jibberish then change the handler and try the other one until you succceed.

Categories

Resources