0

I'm trying to communicate with an Arduino Uno via pyserial using a code that I wrote for Ubuntu. However, when I try to use the same code in OSX Yosemite after identifying the serial port for the case of OSX, it doesn't work. This is the offending line:

In [32]: ser = serial.Serial('/dev/cu.usbmodem1421', 9600)

If I run the above line when the Arduino IDE is not running there is no error message, but the Arduino doesn't do anything (the Arduino IDE Serial Monitor must be open in order for the code to work). However, if I open the Arduino IDE and use the Serial Monitor (which works fine, i.e. it shows data that the Arduino is sending), I get the following error message:

SerialException: [Errno 16] could not open port /dev/cu.usbmodem1421: [Errno 16] Resource busy: '/dev/cu.usbmodem1421'

If I run lsof | grep /dev/cu.usbmodem1421 then I get that the only program using said port is Arduino IDE:

Arduino 2012 germanchaparro 94u CHR 18,7 0t15249 1285 /dev/cu.usbmodem1421

Clearly OSX is not letting Arduino IDE and pyserial to use the serial port (to which the Arduino UNO is connected) at the same time. I don't know how to fix this, and it's especially frustrating since Ubuntu doesn't put up a fight.

Any ideas?

asked Nov 15, 2016 at 23:36
2
  • You need to work out why Python isn't working. There is no reason to need the IDE terminal open unless your code is doing things very wrong. Commented Nov 16, 2016 at 1:33
  • You're right, I can get pyserial to work without the IDE monitor open, although I'm having a hard time printing out the serial output from the Arduino. It seems it's mostly a matter of giving the code enough time between pyserial r/w instructions. It just baffles me how different (and simpler) pyserial works in Ubuntu. Commented Nov 16, 2016 at 5:35

1 Answer 1

1

Ok, I solved it. First I stopped using the IDE serial monitor as Majenko suggested, and focused on getting the Arduino readings within the python code. Besides trying to use the python code and the IDE monitor at the same time (which apparently you can do in Ubuntu but not in OSX), the original problem was that my python code was not clearing the serial buffer in the r/w process. In order to do this, I wrote the following function to be called before and after a serial write:

 ser = serial.Serial('/dev/cu.usbmodem1421', 9600)
 def readarduino(ser):
 did=True
 while ser.inWaiting(): # Check number of characters left in buffer
 if did and ser.inWaiting() < 490: # Select last 500 characters in buffer
 for i in range(6):
 print ser.readline() # Print 6 lines in buffer
 did = False
 ser.readline() # Clear buffer line by line until ser.inWaiting goes to 0

This ensures that the buffer is clear whenever I am writing to the Arduino.

This solution works for OSX Yosemite and Ubuntu 14.04

answered Nov 23, 2016 at 1:15

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.