Failed serial communication between a laptop and Arduino neo

Hello, I am new to arduino and working on a project which enables the bi-direction serial communication between a laptop (python employed) and an Arduino neo. The laptop will pass some information to Arduino and then Arduino will control the movement of motors. After that done, Arduino should send some information back to laptop. I connect the Arduino neo board with laptop by USB. However I run into serveral problems.

  • Even though I don't use no Serial.write() in Arduino IDE, I kept receiving some strings in laptop end when I print out the serial.Serial('/dev/ttyACM0', 9600, timeout=0.1).readline(). The number of output lines is the round I tried to send information to Arduino neo with arduino.write('B'.encode('utf-8')).
  • If I tried to send information to Arduino neo for n times and the motor should move n times, however, the motor always move n-1 times while n times feed back to laptop.

The following is my code in Arduino IDE and python separately.

#include <SoftwareSerial.h>
#include <Sabertooth.h>

SoftwareSerial SWSerial(NOT_A_PIN, 11);
Sabertooth ST1(128);// The Sabertooth is on address 128. We'll name its object ST.
Sabertooth ST2(129);                    // If you've set up your Sabertooth on a different address, of course change
                    // that here. For how to configure address, etc. see the DIP Switch Wizard for
                    //   Sabertooth - http://www.dimensionengineering.com/datasheets/SabertoothDIPWizard/start.htm
                    //   SyRen      - http://www.dimensionengineering.com/datasheets/SyrenDIPWizard/start.htm
                    // Be sure to select Packetized Serial Mode for use with this library.
                    //
                    // On that note, you can use this library for SyRen just as easily.
                    // The diff-drive commands (drive, turn) do not work on a SyRen, of course, but it will respond correctly
                    // if you command motor 1 to do something (ST.motor(1, ...)), just like a Sabertooth.
                    //
                    // In this sample, hardware serial TX connects to S1.
                    // See the SoftwareSerial example in 3.Advanced for how to use other pins.


void setup() 
{
  Serial.begin(9600);
  SabertoothTXPinSerial.begin(9600); // 9600 is the default baud rate for Sabertooth packet serial.
  ST1.autobaud();// Send the autobaud command to the Sabertooth controller(s).
  ST2.autobaud();// NOTE: *Not all* Sabertooth controllers need this command.
                 //       It doesn't hurt anything, but V2 controllers use an
                 //       EEPROM setting (changeable with the function setBaudRate) to set
                 //       the baud rate instead of detecting with autobaud.
                 //
                 //       If you have a 2x12, 2x25 V2, 2x60 or SyRen 50, you can remove
                 //       the autobaud line and save yourself two seconds of startup delay.
  
  // Serial.println("<Arduino is ready>");
}

void myMotorStop()
{
  ST1.motor(1, 0);// stop
  ST1.motor(2, 0);
  ST2.motor(1, 0);
  ST2.motor(2, 0);
  delay(1000);
}


void myMotorFunction(char x)
{
  switch (x)
  {
    case 'F':
        ST1.motor(1, 127);// Go forward at full power.
        ST1.motor(2, 127);
        ST2.motor(1, 127);
        ST2.motor(2, 127);
        delay(2000);
        myMotorStop();
        break;
    case 'B':
        ST1.motor(1, -127);// Go backward at full power.
        ST1.motor(2, -127);
        ST2.motor(1, -127);
        ST2.motor(2, -127);
        delay(2000);
        myMotorStop();
        break;
    case 'L':
        ST1.motor(1, -127);// Vector left
        ST1.motor(2, 127);
        ST2.motor(1, 127);
        ST2.motor(2, -127);
        delay(2000);
        break;
    case 'R':
        ST1.motor(1, 127);// Vector right
        ST1.motor(2, -127);
        ST2.motor(1, -127);
        ST2.motor(2, 127);
        delay(2000);
        // Serial.write("2"); 
        break;
  }

}


void loop() 
{
  if(Serial.available()>0) 
  {
    char incomingByte = Serial.read();
    
    myMotorFunction(incomingByte);
    
    // Serial.write("a");
  }
}
import serial, time
arduino = serial.Serial('/dev/ttyACM0', 9600, timeout=0.1)
# print(arduino.name)   # check which port was really used
time.sleep(1) #give the connection a second to settle

for i in range(3):
    arduino.write('B'.encode('utf-8'))
    while True:
        data = arduino.readline()
        if data:
            print data.rstrip('\n') #strip out the new lines for now # (better to do .read() in the long run for this reason
            break
    time.sleep(1) #give the connection a second to settle

This Simple Python - Arduino demo should help get you started.

...R

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.