0

I made simple custom arduino board using an atmega328p and the PL2303 serial to USB chip.

In my arduino program, I am in a loop that sends out a string continuously. I can read this in the Arduino serial monitor with a baudrate of 9600. However, in my python program using the pySerial library, the readline function doesn't return anything. I checked using PuTTY but it still doesn't do anything.

Now I tried messing with the serial settings and found out that these lines of code work (the readline returns the string) if I call it in the exact same order.

ser = serial.Serial(comm_port)
ser.xonxoff = True
ser.rtscts = True
ser.dsrdtr = True
ser.readline()

However, the code below doesn't work

ser = serial.Serial(comm_port, xonxoff=True, rtscts=True, dsrdtr=True)
ser.readline()

What could be causing this? My guess is somehow I've switched the datalines in my USB cable and that the arduino serial monitor handles this somehow.

Another thing to note is that I couldn't program the Arduino the normal way via USB. I always have to program it using the ICSP port and a USBasp programmer.

2 Answers 2

0

I wrote a python application which communicated with an arduino and for some reason I had to insert a delay after opening the serial for it to work correctly, if I didn't the first data I sent would always get dropped. Perhaps that's what's occuring here? Setting the attributes of the serial connection like that is probably slower than doing it in initialisation.

For myself I found that a delay of 3 seconds was what I needed for it to work 100% correctly on all computers (on some a second was enough). You only had to wait after opening it once open it worked fine.

To do a delay import the time module and use the sleep function which takes an argument of seconds.

import time

time.sleep(3)

Also worth noting depending on your application it's worth setting a timeout value for the serial that's appropriate or just keeping it open with the timeout paramter.

 ser = serial.Serial(com, 115200, timeout=0) 

If you do this make sure to close it when you finished with it though (I personally used a try finally block which makes sure its always closed).

try:
  ser = serial.Serial(com, 115200, timeout=0)
  #go nuts with whatever communicating your doing
finally:
  ser.close()

I'm afraid I can't really tell you what caused the issue though. I also realise that this question is quite old so I hope my answer is still helpful, if not to you then to someone else who stumbles across it. To note for future the people on the official Arduino forum may be of more help for arduino related questions in general.

2
  • 2
    The delay before it is ready is almost certainly caused by the auto reset on serial open behavior and the subsequent bootloader timeout. Oct 18, 2016 at 12:56
  • Yes, try connecting to the RX/TX pins instead.
    – user2497
    Aug 15, 2017 at 20:32
-1

"Another thing to note is that I couldn't program the Arduino the normal way via USB. I always have to program it using the ICSP port and a USBasp programmer." sounds like your atmega328p does not have a bootloader. Or the PL2303 does not have the correct driver.

I never used PL2303, but I have used FTDI USB TTL (ft232r) adapters. When I first used the adapter, my Windows computer installed the wrong driver for the adapter and I had to manually download the correct firmware from the FTDI website and install it manually. You can check if the driver is correct in the device manager, the author of the driver should be the manufacturer's name (not Microsoft).

Not the answer you're looking for? Browse other questions tagged or ask your own question.