Control robot via serial port

Connect to Arduino board via USB-to-UART adapter

1. Connect TX pin of the USB-to-UART adapter to RX input of Arduino board

2. Connect RX pin of the USB-to-UART adapter to TX input of the Arduino board

3. Connect Arduino board to computer by USB cable

USB2UARTAdapterCP2102ConnectedToArduino

4. Start the Arduino IDE, check if a device is selected for Arduino board in menu “Tools”/”Serial Port” (e.g. “/dev/ttyACM0”)

5. Type, copy/paste or download from GitHub a program in the Arduino IDE

Code on GitHub

/*
Awaiting text commands via serial port #1 - digital pins 0 (RX1), 1 (TX1)
Max command length - 64 chars
Command ends with "new-line" char
Diagnostics messages are sent back to serial port #1

This code is in the public domain.
Copyright © foreglance.com
*/
//--- constants ---
const int SerialPortBaudRate = 9600;
const int MaxCommandLength = 64;
const char LF = '\n';
const String EmptyString = "";

//--- fields ---
String command = EmptyString;
int commandLength = 0;
char inputChar;
boolean awaitingCommand = true;

//--- setup ---
void setup() {
  command.reserve(MaxCommandLength);
  initSerialPort1();
  awaitCommand();
}

//--- main loop ---
void loop() {
  processCommand();
  readSerialData();
}

//--- commands ---
void commandStart(){
  Serial1.println("Start");
}

void commandStop(){
  Serial1.println("Stop");
}

//--- methods ---

//invoke command methods
void processCommand(){
  if(awaitingCommand)
    return;

  if(command == "start")
    commandStart();
  else if(command == "stop")
    commandStop();
  else
    Serial1.println("Unknown command:" + command);

  awaitCommand();
}

//initialize the serial port #1
void initSerialPort1(){
  Serial1.begin(SerialPortBaudRate);
  while(!Serial1){}//wait while the serial port is ready
}

//request for a new command
void awaitCommand(){
  command = EmptyString;
  commandLength = 0;
  awaitingCommand = true;
}

//read data for the buffer of the serial port #1
void readSerialData() {
  //when command is not awaited 
  // and data do not exist in the buffer of the serial port
  if(!awaitingCommand || !Serial1.available()) 
    return; // do not read data

  // take a char from the buffer
  inputChar = (char)Serial1.read(); 

  //if taken char is "new-line" 
  // and no other data were taken earlier
  if(LF == inputChar && 0 == commandLength)
    return; // do not change the command text

  commandLength++; // length of the command text with taken char

  //if taken char is not "new-line"
  // and the command text with this char not longer tham Max 
  if(LF != inputChar && MaxCommandLength > commandLength)
    command += inputChar; //add this char to the command text
  else
    awaitingCommand = false; //define the command as received
}

hint Why not to use 'serialEvent'

Method serialEvent is invoked every iteration or “loop”. But it is not supported by some of Arduino boards.

6. Upload the program to an Arduino board

7. Connect USB-to-UART adapter to computer – by its own USB cable

8. Find on which device the adapter has connected

Run following command in the Terminal

ll /sys/class/tty/ttyUSB*

UbuntuFindSerialPortsUSB2UARTCP2102Connected

9. Connect to USB-to-UART adapter in the 'CuteCom'

  1. Start the CuteCom
  2. Type in the field “Device” a device name for connection with an USB-to-UART adapter  (e.g. “/dev/ttyUSB0”)
  3. Change “Baud rate” to 9600 – this value is defined in the program uploaded to the Arduino board
  4. Clear the checkbox “Handshake:Hardware
  5. Click a button “Open device”

10. Type a text “start” (do not use capital letters) in the field “Input” and hit Enter – text “Start” should be printed in the output field – this is how commands are sent to the program working on the Arduino board

11. Send a command “stop” – text “Stop” should be printed

12. Send any other command – text “Unknown command:” followed by this command should be printed.

CuteComSendCommandsStartStopToUCB2UARTAdapter

This program supports only two commands which print back command names (started with capital letter). On any other commands the program responds with a message “Unknown command:command-name”. Maximum length of a command is 64 chars – longer commands are split to 64 chars commands.

Facebooktwittergoogle_plusredditpinterestlinkedintumblrmail