DWigg
Posts: 4
Joined: Wed Jan 03, 2018 4:23 pm

Pi serial comms problem

Sat Mar 17, 2018 3:26 pm

Hi,

I am using a Pi to communicate with a GPS module via pyserial. This works but displays characters going to the GPS module as well as the response, which I don't want. I only want the response.

The code I'm using is:

import serial
from time import sleep

# Set up serial port. Baud rate etc. are the default for the GPS module
port=serial.Serial("/dev/serial0", baudrate=115200, timeout=3.0)

# Determine if GPS module is talking to us
port.write("AT".encode('utf-8'))
sleep(1)
response=port.readline().decode('utf-8') # Read result
print(response)
if response=="OK":
print("Communication OK")
else:
print("No response")

The result of this is:
==================== RESTART: /home/pi/Programmes/GPS.py ====================
AT
No response

The response to the AT command is "OK" which is what I'd expect and what I want to use. I can see the correct response if I use Minicom, which would show:

AT
OK


Any Ideas how to get rid of the sent stuff please?

rkn704
Posts: 30
Joined: Sun Dec 30, 2012 4:25 pm

Re: Pi serial comms problem

Sun Mar 18, 2018 10:49 am

When I read from the serial port I use read rather than readline. May be this has something to do with it.

Code: Select all


response=port.read(100)  #where the 100 is the number of characters to read.


DWigg
Posts: 4
Joined: Wed Jan 03, 2018 4:23 pm

Re: Pi serial comms problem

Sun Mar 18, 2018 11:50 am

I've tried read(10) with same result.
I'm using readline because the response could be 'OK' or 'ERROR' or, later in the code a long string.

rkn704
Posts: 30
Joined: Sun Dec 30, 2012 4:25 pm

Re: Pi serial comms problem

Sun Mar 18, 2018 1:00 pm

When you used Minicom was that using the a Pi? Did you use the same hardware, cable etc?

If you are using a USB to RS232 converter for the comms then the port should be something like /dev/ttyUSB0

Is the problem with the command AT not reaching the GPS device or is it a problem with the reply?

Does the GPS device need hardware handshaking, might need rtscts=1 in the port setup and an appropriate cable.

Try sending your AT command to a PC running Minicom or Hyperterminal. This would check your port and cable out.

Send a reply from a PC to check the return path.

Idahowalker
Posts: 404
Joined: Wed Jan 03, 2018 5:43 pm

Re: Pi serial comms problem

Sun Mar 18, 2018 2:19 pm

Disconnect the external device.
Connect a wire from Tx to Rx on the pi
Use the com thing and type AT.
Do you get back 'OK' or 'AT OK'?
If 'AT OK' try changing the echo setting to off.
Without knowing why you are deleting my postings, I will not know how...

DWigg
Posts: 4
Joined: Wed Jan 03, 2018 4:23 pm

Re: Pi serial comms problem

Sun Mar 18, 2018 5:18 pm

Yes, I'm using the same Pi for Minicom. I've tried loopback test, which works OK without hardware handshaking.
I see both the sent commands and the message received. If I turn on Echo, I see the sent characters duplicated, like 'AATT'.

Normally with Minicom the data looks like:

AT
OK

or:

AT+CGPSINFO
+CGPSINFO: 5675367537 etc....

Further efforts reveal I am receiving the correct response after the echo of the command which I can read with a second 'readline()' command.

Return to “Python”