Go Down

Topic: Multiple Characters by Serial (Read 721 times) previous topic - next topic

viveksri

I'm having trouble receiving multiple characters from serial.

I'm trying to send an integer from Python (which I get from a particular sequence of binary that I want). It's basically a value between 0 and 255 that I'm sending to the arduino. After every value, I send a "*", which is what I'm using as my delimiting value, and then in the arduino code, I'm trying to read each number into a 2D array (there are 64 numbers sent total).

I know the code is fine on the python side, I've printed the values that I'm sending and they're correct (numbers between 0 and 255 with a * at the end). It's just not working properly on the arduino side.

I'm using this to drive an LED cube, each integer value corresponds to the states of 8 LEDs in a row. If I hardcode the array into arduino, it works fine, so I'm pretty sure it's a problem with the serial communication. I can't print the current array out to the serial monitor to debug though, because python's using the serial code.

Arduino code:
Code: [Select]
void loop() {
  String currentValue = "";
 
  for (int layer = 0; layer < 8; layer++) {
    for (int row = 0; row < 8; row++) {
      while (!Serial.available()) {
        ;
      }
      char c = Serial.read();
      if (c != '*') {
        currentValue += c;
      }
      else {
        currentCube[layer][row] = currentValue.toInt();
        currentValue = "";
      }
    }
  }
}


Python code:
Code: [Select]
def sendCube(cube):
    (layers) = len(cube)
    (rows, cols) = (len(cube[0]), len(cube[0][0]))

    currentBin = ""
    currentInt = 0
   
    for layer in xrange(layers):
        for row in xrange(rows):
            for col in xrange(cols):
                if (cube[layer][row][col] == 1):
                    currentBin += '1'
                else:
                    currentBin += '0'
            currentInt = int(currentBin, 2)
            arduino.write(str(currentInt) + '*')
            currentBin = ""
            currentInt = 0


Any ideas on why it's not working?

zoomkat

Some servo control test code that may be close to what you are looking for.

Code: [Select]

//zoomkat 3-5-12 simple delimited ',' string parse
//from serial port input (via serial monitor)
//and print result out serial port

String readString;
#include <Servo.h>
Servo myservo;  // create servo object to control a servo

void setup() {
  Serial.begin(9600);

  myservo.writeMicroseconds(1500); //set initial servo position if desired
  myservo.attach(7);  //the pin for the servo control
  Serial.println("servo-delomit-test-22-dual-input"); // so I can keep track of what is loaded
}

void loop() {

  //expect a string like 700, or 1500, or 2000,
  //or like 30, or 90, or 180,

  if (Serial.available())  {
    char c = Serial.read();  //gets one byte from serial buffer
    if (c == ',') {
      if (readString.length() >0) {
        Serial.println(readString); //prints string to serial port out

        int n = readString.toInt();  //convert readString into a number

        // auto select appropriate value, copied from someone elses code.
        if(n >= 500)
        {
          Serial.print("writing Microseconds: ");
          Serial.println(n);
          myservo.writeMicroseconds(n);
        }
        else
        {   
          Serial.print("writing Angle: ");
          Serial.println(n);
          myservo.write(n);
        }

        //do stuff with the captured readString
        readString=""; //clears variable for new input
      }
    } 
    else {     
      readString += c; //makes the string readString
    }
  }
}

Google forum search: Use Google Advanced Search and use Http://forum.arduino.cc/index in the "site or domain:" box.

PaulS

Quote
I can't print the current array out to the serial monitor to debug though, because python's using the serial code.

Sure you can. Use the Serial Monitor to send what you think the python code is sending. Have the Arduino echo what it gets. If the Arduino is receiving what you think Python is sending, and is processing that correctly, then the problem is that Python isn't sending what you think it is. If the problem is that the Arduino is not processing the input correctly, stop using the stupid String class.

Khalid

#3
Nov 22, 2013, 08:09 pm Last Edit: Nov 23, 2013, 03:30 am by Khalid Reason: 1
i suggest you to add a minor delay (20ms) in the python code between the two characters and just do what PaulS described in above thread.
Simply...You can't afford me..

Author Of:
http://my-woodcarving.blogspot.com/
http://www.free3dscans.blogspot.com/
http://my-diysolarwind.blogspot.com/

Oops..some one gave me Karma...:)

Go Up