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
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?