PySerial Resulting in Different Outputs Between Two Macs

Hey All!

I'm having difficulty with a python/arduino script I've written on one Mac to work on a new Mac I'm migrating to. The first computer is running OS X Yosemite 10.10.1, Python 2.7.10, PySerial 2.7, and Arduino IDE 1.6.7. The second computer, the one that is having problems running the script is OS X El Capitan 10.11.3, Python 2.7.11, PySerial 2.7, and Arduino IDE 1.6.7. I'm using an Arduino Micro with the code being:

String readString;
char character;

void setup() {

  Serial.begin(9600);
  
  while (!Serial);

}

void loop() {
  
  while (!Serial.available()){}

  while(Serial.available() > 0)
  {
    character = Serial.read();
    readString += character;
  }

  if (readString.length() >= 0) {
    readString.trim();
    readString = FunctionDecision(readString);
  }

}

String FunctionDecision(String ss)
{
  if (ss == "YY") {
    Serial.println("Yes");
  } else {
    Serial.println("No");
  }
  return "";
}

And the Python code is:

from time import sleep
import serial

ser = serial.Serial('/dev/cu.usbmodem14211', baudrate = 9600) # Establishing connection

sleep(10) # Way too long, but making sure connection is up

for x in range(5):

	ser.write("YY")

	sleep(0.1)

	x = ser.readline()

	print x

ser.close()

On my first Mac, the python print results in:

Yes

Yes

Yes

Yes

Yes

And repeats like that all day, which is the expected output. On my second Mac, after uploading the code to my Arduino Micro, it runs perfectly the first time. But trying to run it a second time the code hangs on ser.readline(). This led me to believe that there is an EOL error. Changing the serial port configuration to have a timeout of 2 results in my code skipping the first two 'Yes' outputs, and receiving the last three.

So far I've tried upgrading my second Mac to PySerial 3.0.1 and Arduino IDE 1.6.8, but to no avail. I'm sure there's an error somewhere in my code but I can't seem to find it. I was hoping to use your guy's Arduino super powers to help out. Any advice would be greatly appreciated!

Don't use ser.readline() ? ?
Read the data character by character with ser.read()

You may find something useful in this Python - Arduino demo

...R

I don't have a clear idea what the program is working now, but after switching to Yosemite 10.10.5 the serial.readline() function is working as it should. If anyone has a better explanation as to why this is the case, I would love to know it. Thanks for the tip btw Robin2!

It's a little late, but I was able to find a semi-definitive answer to what problems I was having, explained in the article below:

http://blog.othermachine.co/the-othermill-and-mac-os-x-10-11-el-capitan

Basically, OS X El Capitan had the USB communication code re-written, introducing bugs when communicating with products like Otherplan, Arduino's, and serial libraries. The bugs have been fixed in OS X version 10.11.3 and higher. I was able to communicate with an Arduino Micro using the most up to date version of PySerial. Hope this helps someone out there!