Execute c++ on Udoo Linux and receive data on Udoo Arduino

Discussion in 'Linux Ubuntu' started by charles, May 9, 2014.

  1. charles

    charles New Member

    Joined:
    May 9, 2014
    Messages:
    1
    Likes Received:
    0
    Hello, I'm beginner on Udoo board. I have some questions on it.

    If I want to execute C++ on Udoo Linux (./filename) to send some datas for Udoo Arduino

    1. Does the Udoo OS, Linux, communicate to Udoo Arduino via by internal RS232?

    2. How to set hardware (Udoo board) and software (Linux) before executing?
    (I already reference this web page http://www.udoo.org/ProjectsAndTutorials/interaction-between-linux-and-arduino-on-udoo/)

    3. If possible, I need some example code both of C++ and Arduino C to know how to send and receive data.

    Thank you for help.
     
  2. gfasino

    gfasino New Member

    Joined:
    Jun 27, 2014
    Messages:
    13
    Likes Received:
    0
    Re: Execute c++ on Udoo Linux and receive data on Udoo Ardui

    Hi,
    First the Udoo use the /dev/ttymxc3 Serial port.
    Second if you use C++ for the communication you have to set the communication commands in C++ code.
    If you want i can send you a code in C++ with read all arduino outputs and write it into file.
     
  3. srd78

    srd78 New Member

    Joined:
    Jul 14, 2014
    Messages:
    1
    Likes Received:
    0
    Hi gfasino,

    Can you post the sample code to the forum?

    Thank you
     
  4. gfasino

    gfasino New Member

    Joined:
    Jun 27, 2014
    Messages:
    13
    Likes Received:
    0
    Re: Execute c++ on Udoo Linux and receive data on Udoo Ardui

    That's the code who i' ve made:
    Code:
    #include <stdio.h>      // standard input / output functions
    #include <stdlib.h>
    #include <string.h>     // string function definitions
    #include <unistd.h>     // UNIX standard function definitions
    #include <fcntl.h>      // File control definitions
    #include <errno.h>      // Error number definitions
    #include <iostream>
    #include <fstream>
    #include <termios.h>    // POSIX terminal control definitions
    using namespace std;
    int length(char strname []);
    int main()
    {
    /* Open File Descriptor */
    
        int USB = open( "/dev/ttymxc3", O_RDWR| O_NONBLOCK | O_NDELAY );
    /* Error Handling */
    if ( USB < 0 )
    {
        cout << "Error " << errno << " opening " << "/dev/ttymxc3" << ": " << strerror (errno) << endl;
    }
    
    /* *** Configure Port *** */
    struct termios tty;
    memset (&tty, 0, sizeof tty);
    struct tm * timeinfo;
    char buffer [80];
    time_t rawtime;
    /* Error Handling */
    if ( tcgetattr ( USB, &tty ) != 0 )
    {
        cout << "Error " << errno << " from tcgetattr: " << strerror(errno) << endl;
    }
    
    /* Set Baud Rate */
    cfsetospeed (&tty, B9600);
    
    /* Setting other Port Stuff */
    tty.c_cflag     &=  ~PARENB;        // Make 8n1
    tty.c_cflag     &=  ~CSTOPB;
    tty.c_cflag     &=  ~CSIZE;
    tty.c_cflag     |=  CS8;
    tty.c_cflag     &=  ~CRTSCTS;       // no flow control
    tty.c_lflag     =   0;          // no signaling chars, no echo, no canonical processing
    tty.c_oflag     =   0;                  // no remapping, no delays
    tty.c_cc[VMIN]      =   0;                  // read doesn't block
    tty.c_cc[VTIME]     =   5;                  // 0.5 seconds read timeout
    
    tty.c_cflag     |=  CREAD | CLOCAL;     // turn on READ & ignore ctrl lines
    tty.c_iflag     &=  ~(IXON | IXOFF | IXANY);// turn off s/w flow ctrl
    tty.c_lflag     &=  ~(ICANON | ECHO | ECHOE | ISIG); // make raw
    tty.c_oflag     &=  ~OPOST;              // make raw
    
    /* Flush Port, then applies attributes */
    tcflush( USB, TCIFLUSH );
    
    if ( tcsetattr ( USB, TCSANOW, &tty ) != 0)
    {
        cout << "Error " << errno << " from tcsetattr" << endl;
    }
    
    /* *** WRITE *** */
    int length1;
    unsigned char cmd[] = {'I', 'N', 'I', 'T', ' ', '\r', '\0'};
    int n_written = write( USB, cmd, sizeof(cmd) -1 );
    
    /* Allocate memory for read buffer */
    char buf [256];
    memset (&buf, '\0', sizeof buf);
    while(true)
    {
        ofstream sensorData("sensor.log",ios::app);
    /* *** READ *** */
    int n = read( USB, &buf , sizeof buf );
    
    /* Error Handling */
    if (n < 0)
    {
        cout << "Error reading: " << strerror(errno) << endl;
    }
    
    cout << "Read: " << buf << endl;
    length1=length(buf);
    if(length1>0)
        {time(&rawtime);
          timeinfo = localtime (&rawtime);
          strftime (buffer,80,"%F-%I-%M-%S ",timeinfo);
          sensorData<<buffer<<buf;
          sensorData.close(); 
    
       }
          sleep(1);
    }
    close(USB);
    }
    
    int length(char strname [])
    {int i=0;
     while(strname[i]!='\0')
                  {i++;
                   }
     return i;
    }
    
     
  5. san7

    san7 New Member

    Joined:
    Aug 10, 2014
    Messages:
    1
    Likes Received:
    0
    Re: Execute c++ on Udoo Linux and receive data on Udoo Ardui

    Hi! gfasino,

    I'm unable to receive the data sent by arduino. Since I followed the tutorial on bidirectional communication.
    Setting the baud rate same on both side "stty -F /dev/ttymxc3 cs8 115200 . . . "
    chmod 777 ttymxc3
    Code is written in shell script, so when i'm trying to read ttymxc3
    cat /dev/ttymxc3
    nothing happens, cursor starts blinking and waiting , so i cancelled it by pressing CTRL+C .
    What's the problem unable to troubleshoot.
    I'm able to transmit the data from Udoo but unable to receive from Arduino.
     

Share This Page