Page 2 of 2

Re: Two devices connected to a single UART

Posted: Wed May 18, 2016 9:16 am
by FiddlerJones
Try waiting a bit longer between tries, I mean an unnecessary long amount of time, and see if you can get the data in a correct format.

If not, maybe you don't have a fix? Have you tried testing on the outside? My GPS module was able to find the fix only after about 30 seconds outside, when I was testing on the inside I almost never got a fix, only sometimes, near a window, after couple of minutes.

Re: Two devices connected to a single UART

Posted: Wed May 18, 2016 11:22 am
by danjperron
The biggest problem you have is that the GPS is continuously sending data.

This mean that you could be in a middle of a transfer.

The thing to do will be to wait in a loop until you get a good package and then close it.

Code: Select all

import serial, time

def read_gps():

    gps = serial.Serial('/dev/ttyAMA0', 9600, timeout=1)
    gps.flush()

    for retry  in range(10):
        NMEA = gps.readline()
        line = NMEA.split(",")
        #print NMEA
        if(NMEA.startswith("$GPGGA")):
            gps.close()
            return line
      
    gps.close()
    return 0, 0, 0, 0

while True:
    gpsData = read_gps()
    print gpsData
    time.sleep(.5)
B.T.W. Change the range as you see fit.

Of course the best method will have the serial port always open and capture on a thread all of the data but this post is about two devices on the same port.

Re: Two devices connected to a single UART

Posted: Fri May 20, 2016 4:33 pm
by hydr5
Thanks for your answers.

You were right danjperron, the problem was that the GPS is continuously sending data.
The "retry" was the trick, it works fine and I get good data now. However when I call my function to send data over the NTX, it seemed the script freezes sometimes and nothing more happened. I fixed it by openning a new connection + flush to the NTX and added a timer between the flush and the write instruction as FiddlerJones did.

Here is my script:

Code: Select all

import serial, time

def sendData(data):

    NTX2 = serial.Serial('/dev/ttyAMA0', 300, serial.EIGHTBITS, serial.PARITY_NONE, serial.STOPBITS_TWO)
    NTX2.flush();

    time.sleep(.5)
    NTX2.write(data)
    NTX2.close()

    NTX2 = serial.Serial('/dev/ttyAMA0', 9600, timeout=1)
    NTX2.flush()

def read_gps():

  gps = serial.Serial('/dev/ttyAMA0', 9600, timeout=1)
  gps.flush()

  for retry  in range(10):
    NMEA = gps.readline()
    
    if(NMEA.startswith("$GPGGA")):
      gps.close()
      return NMEA

  gps.close()
  return 0, 0, 0, 0

while True:
  gpsData = read_gps()
  sendData(gpsData)
  print gpsData
  time.sleep(1)