Monday, June 17, 2013

More about serial connection

The importance of communication

Our cubieboard have enough connection to control almost of our sensors, using I2C.
But in my case I'll have a few more analogical sensors, and I want to avoid that, and the cubieboard doesn't have to work with the simplest things.

For this, my cubieboard will be a brain, the arduino will be then the senses, and the connection will be made over a serial connection
I could make using I2C but the arduino is working with it as master for the sensors, and could get more complicated. (by the moment :) )

The arduino part.(Serial vs SoftwareSerial)

Arduino has a serial port inside of it, you can access through the USB, or the digital pins (Rx-0,Tx-1).
This option gave me two problems.

1º The USB - Serial driver (ftdi_sio) does not exit in my version of raspbian (Linux raspberrypi 3.4.24-a10-aufs+), and it's necessary to upgrade (I made it, but also it's necessary to install again the OpenCV, lib-jpeg-turbo, etc. This option is completely necessary if you don't want to work with arduinos sketches (to slow :( at the cubieboard )


2º The other issue is more a problem of design. I'm learning who to make drones, and if I want to debug the arduinos part and don't worry about the cubieboard, I should to make through the USB serial, but if it's used by the cubieboard, I could'nt have access to it.

Arduino give us a solution, and it's to use a software serial.
It's possible to use two different digital pin to serial communication.
Here it's an useful example (http://www.arduino.cc/en/Reference/SoftwareSerialExample)

Here it's the schema of connection, (quite simple)
The serial connection send each byte alone, is oriented to a character, so we have to work in our protocol.
Here it's a code it's a modification of the arduino example.
///BEGIN .ino
#include <SoftwareSerial.h>

//Define for the new pins for the Software-serial
#define rxPin 2
#define txPin 3


String buffer="";// In this buffer I'll receive the commands

// Create a new serial port
SoftwareSerial miSerie =  SoftwareSerial(rxPin, txPin);
void setup() 
{
  Serial.begin(9600);
  Serial.println("USB SERIAL UP");
 
  // Stablish the serial port
  pinMode(rxPin,INPUT);
  pinMode(txPin,OUTPUT);
  miSerie.begin(9600);
  miSerie.println("SOFT SERIAL UP");
}

void loop()
{
//isBufferComplete is to retrieve complet commands
  if (isBufferComplete())//this function read the date from my serial (softserial)
  {
    Serial.print("Recibido:");
    Serial.println(buffer);
    buffer="";
  }
  //From the arduino to the cubieboard (throught SoftSerial)
  if (Serial.available())//Just read from the USB serial
    miSerie.write(Serial.read());
}

//This function read from the softserial
boolean isBufferComplete()
{
  //In this function we recieve each caracter till
  // arrive ";" wich means end of command
  boolean completa =false;
  while (miSerie.available())
  {
    char inChar = (char)miSerie.read();
    buffer += inChar;
    if (inChar ==';') completa=true;
  }
  return completa;
}
///END .ino
note: SerialEvent from the arduino, doesn't works with software-serial
http://arduino.cc/en/Tutorial/SerialEvent

At the next post will be the code for the cubieboard.


2 comments:

  1. Hello,

    I have a strance situation, maybe you can help me:

    I have cubieboard and Arduino Due. These are linked with an USB cable.
    On the cubieboard, Arduino is detected as /dev/usbdev5.3. I have a java app on cubie which uses jssc to communicate with Arduino. The proglem is that I have an error like Invalid port message when the app is trying to establish communcation. It is strange because before (on another cubieboard alternative card) the Arduino worked and was detected as /dev/ttyACM0 and not like /dev/usbdevX.X
    Do you have a clue what's going on?

    Thanks,
    Andrei

    ReplyDelete
    Replies
    1. Hi Andrei.

      As I read, the arduino it's detected by the cubieboard (which is your system ? raspbian ? linaro ? android ?)
      I had problems with raspbian cause it couldn't detect the arduino, but it seem is not your case.

      have you tried another program of communication ? I don't like java, try to small program in C, to check if the problem is in your code or not.

      Or you can program the arduino with the computer and send by serial a value, and if you are in linux you can use the command "cu -s baudios -l /dev/tty..

      I hope this ideas could give you any solutions.
      Thanks to visit me

      Delete