Pyserial + Arduino

Hey all, I need major help with pyserial and Arduino…

I am able to directly control a multi-color LED in Arduino through the python in terminal by executing:

import serial
ser = serial.Serial (’/dev/tty.usbserial-A6004p1R’, 9600)
ser.write(‘1’)

However, when I import a python script with the exact same commands, it does not work!

This is my Arduino Code:

// Output
int redPin   = 9;   // Red LED
int greenPin = 10;  // Green LED
int bluePin  = 11;  // Blue LED

// Program variables
int serVal = 0;       // variable to store the data from the serial port
int redVal   = 0; // Variables to store the values to send to the pins
int greenVal = 0;  
int blueVal  = 0;

void setup()
{
  pinMode(redPin, OUTPUT);   // sets the pins as output
  pinMode(greenPin, OUTPUT);  
  pinMode(bluePin, OUTPUT);
  Serial.begin(9600);
  Serial.flush();
}

// Main program
void loop() {
serVal = Serial.read();      // read the serial port

if (serVal >= '0' && serVal <= '6' ) {
  serVal = serVal - '0';          // convert from character to number
  for(int i=0; i < serVal; i++) {
{
  if (serVal == 1)
  {
    redVal = 10;
    greenVal = 255;
    blueVal = 0;
  }
   else if (serVal == 2)
    {
      redVal = 15;
      greenVal = 165;
      blueVal = 170;
    }
    else if (serVal == 3)
    {
      redVal = 255;
      greenVal = 230;
      blueVal = 0;
    }
    else if (serVal == 4)
    {
      redVal = 200;
      greenVal = 80;
      blueVal = 0;
    }
    else if (serVal == 5)
    {
      redVal = 255;
      greenVal = 0;
      blueVal = 0;
    }
    else
    {
      redVal = 0;
      greenVal = 0;
      blueVal = 0;
    }
  }
  analogWrite(redPin,   redVal);   // Write current values to LED pins
  analogWrite(greenPin, greenVal);
  analogWrite(bluePin,  blueVal);
    }
  }

}

This is my Python Code:

import serial 
ser = serial.Serial('/dev/cu.usbserial-A9003UPq', 9600)
ser.write('1')

What gives???
:’(
Please help!!!

Here is a theory: when you open the serial port it resets the Arduino. In the terminal there is plenty of time for this reset to happen, but when you import the python sketch the serial write happens so fast it occurs during the reset.

Try adding a delay of a couple seconds using time.sleep() between opening the serial port and sending the data.

-br

Having played with Python and Arduinos before, I can verify that billroy has a high chance of being correct here.

There is another solution to this problem.

You can prevent the arduino from resetting on connection my modifying pySerial.

Open the serialWin32.py file from the pyserial install directory (usually located in lib\site-packages\serial)

Delete the compiled version of the file (serialWin32.pyc) and then edit the serialWin32.py file, changing the line that reads: self._dtrState = win32.DTR_CONTROL_ENABLE to: self._dtrState = win32.DTR_CONTROL_DISABLE

This should now open the serial port without altering the DTR state on Windows machines.

I created a new module by copying the serial directory and renaming it serialArduino. That way, I could change my import serialArduino as serial without affecting other applications that might be expecting the serial module to operate with dtrState = DTR_CONTROL_ENABLE