Serial communication between python and arduino

Discussion in 'Programmer's Corner' started by mnobeidat, Sep 8, 2018.

  1. mnobeidat

    Thread Starter New Member

    Dec 20, 2016
    20
    0
    I am working on object tracking robot. I am using python and OpenCV to detect the object and send the proper data to the arduino that controls two servo motors.
    The data which should be sent to the arduino are servo motors angles ranging between 0-180. I am using sample codes to understand how python and arduino communicate using serial bus.When I send a single digit, the arduino receives it and work as intended, but when I send more than one digit nothing happens.
    This is the arduino code:
    Code (C):
    1.  #include <Servo.h>
    2.     int data;
    3.     Servo myservo;  // create servo object to control a servo
    4.     // twelve servo objects can be created on most boards
    5.     int pos = 0;    // variable to store the servo position
    6.  
    7.     void setup() {
    8.       Serial.begin(9600); //initialize serial COM at 9600 baudrate
    9.       pinMode(LED_BUILTIN, OUTPUT); //make the LED pin (13) as output
    10.       digitalWrite (LED_BUILTIN, LOW);
    11.       myservo.attach(9);
    12.       Serial.println("Hi!, I am Arduino");
    13.     }
    14.  
    15.     void loop() {
    16.     while (Serial.available()){
    17.      //to receive more than one character
    18.      char buffer[] = {' ',' ',' ',' ',' ',' ',' '}; // Receive up to 7 bytes
    19.      while (!Serial.available()); // Wait for characters
    20.      Serial.readBytesUntil('n', buffer, 7);
    21.      data = atoi(buffer);
    22.  
    23.     }
    24.     myservo.write(data);
    25.     }
    And here is the python code:
    Code (Python):
    1.     import serial
    2.     import time  # Required to use delay functions
    3.  
    4.     # Create Serial port object called arduinoSerialData
    5.     arduinoSerialData = serial.Serial('com14', 9600)
    6.  
    7.     time.sleep(2)  # wait for 2 secounds for the communication to get established
    8.  
    9.     print arduinoSerialData.readline()  # read the serial data and print it as line
    10.     print ("Enter 1 to turn ON LED and 0 to turn OFF LED")
    11.  
    12.     while 1:  # Do this forever
    13.  
    14.         var = raw_input()  # get input from user
    15.         print "you entered", var  # print the intput for confirmation
    16.  
    17.         arduinoSerialData.write(var)
    Mod edit: added code tags [code] [/code]=
     
    Last edited: Sep 8, 2018
  2. xox

    Member

    Sep 8, 2017
    341
    72
    First of all, you need to detect the number of bytes received and then use that to null-terminate the string, ie: buffer[received] = 0 (just make sure that the "received" variable is less than the buffer size; in this case that would have to be 6 since the buffer size is 7). If you don't then string-handling routines like atoi() will basically get confused. Next, verify that you can send data directly (without using any Python routines).
     
Loading...