0

I have a few Pi Zeros I'm working on for a project. I have the Pi connected to a PC with Windows 7 with a USB cable, and I need to send packets of text to it with information to control devices on the GPIO pins. The problem is that my python script on the Pi Zero doesn't seem to be receiving the packets.

I have the Pis set up as serial gadgets following this tutorial: https://gist.github.com/gbaman/50b6cca61dd1c3f88f41

Basically, running Jessie, added dtoverlay=dwc2 to config.txt, g_serial to modules, and enabled getty@ttyGS0.

The Pi comes up as a com port when I plug it into the computer, and I can access the Pi terminal from the PC using Tera Term.

My abbreviated python code is:

    import time
    import serial

    MAXBUFFER = 200
    NUMDATAFIELDS = 21
    PORTNAME = '/dev/ttyAMA0'
    BAUDRATE = 115200
    TIMEOUT = 0.02

def parse_serial_packet(incoming_data):
    # Remove leading or trailing white space and separate the fields.
    incoming_data = incoming_data.strip()
    fields = incoming_data.split(',')

    if len(fields) != NUMDATAFIELDS:
        return False
    else:
        # I'm leaving out the irrelevant parsing code here.
        return True

def main():
    ser = serial.Serial(port=PORTNAME, baudrate=BAUDRATE, timeout=TIMEOUT)

    while True:
        # Run the loop at roughly 100Hz.
        time.sleep(0.01)

        # Read in up to the maximum size of data per line.
        data_in = ser.read(MAXBUFFER)

        # Check that some data was received.
        if (len(data_in) == 0):
            continue
        # Check that packet was well formatted.
        if not parse_serial_packet(data_in):
            continue

if __name__=="__main__":
    main()

I've put some outputs throughout the script and it doesn't seem like it gets past the if (len(data_in) == 0): statement. Also the program sending the data stops running after about 20 seconds like it lost the com port and is waiting for it to reconnect, but it doesn't have any errors. What am I getting wrong? Why is the Pi not receiving the data packets?

  • Have you tried accessing the RPi via serial outside of your script, using something like PuTTY? I'd suggest verifying serial is working at all before trying to debug your script. – bobstro Feb 21 '18 at 21:24
  • Yes, serial is working. I used Tera Term to log into the RPi. I can access everything on the RPi over the serial connection just like I'm using the terminal or ssh over ethernet. – Nubian Feb 21 '18 at 21:58
  • Could you also have a getty process also reading from /dev/ttyAMA0? My rpi0 does: 00:00:00 /sbin/agetty --keep-baud 115200 38400 9600 ttyAMA0 vt102 Then again, that's because I have the console configured in /boot/cmdline: dwc_otg.lpm_enable=0 console=tty1 console=ttyAMA0,115200 kgdboc=ttyAMA0,115200 – HeatfanJohn Feb 21 '18 at 22:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.