Go Down

Topic: No data sent over serial port in linux c++ [RESOLVED]  (Read 570 times) previous topic - next topic

kostor

TL;DR - I am attempting serial communication with Arduino with code that I found here:
https://stackoverflow.com/questions/18108932/linux-c-serial-port-reading-writing

and nothing gets sent over (Arduino programmed to respond, and I checked that it does with its serial monitor)

Hi there,
I was looking for a way to send information over to an Arduino Mega (2560) unit over linux serial port by C++.

I came across the following solution: https://stackoverflow.com/questions/18108932/linux-c-serial-port-reading-writing

I'm using this guy's code for write (I'm able to read data from the arduino) and use the same parameters (they work, as I'm able to receive data from the Ardunio).
I programmed my Arduino to send "Hi" over serial whenever it sees at least 1 bit of information, and checked it worked through the Arduino IDE Serial Monitor.

Yet when running the C++ code, the arduino doesn't respond. Do anyone might have idea why?

Full disclosure - I inserted the code to a class so I can make an instance of it for my needs of the code.

opening port:
Code: [Select]
fd = open(portNameC, O_RDWR | O_NOCTTY | O_SYNC); //open port ("opens file")

parameters:
Code: [Select]

struct termios tty;
struct termios tty_old;
memset (&tty, 0, sizeof tty);

/* Error Handling */
if ( tcgetattr ( USB, &tty ) != 0 ) {
   std::cout << "Error " << errno << " from tcgetattr: " << strerror(errno) << std::endl;
}

/* Save old tty parameters */
tty_old = tty;

/* Set Baud Rate */
cfsetospeed (&tty, (speed_t)B9600);
cfsetispeed (&tty, (speed_t)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_cc[VMIN]   =  1;                  // 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

/* Make raw */
cfmakeraw(&tty);

/* Flush Port, then applies attributes */
tcflush( USB, TCIFLUSH );
if ( tcsetattr ( USB, TCSANOW, &tty ) != 0) {
   std::cout << "Error " << errno << " from tcsetattr" << std::endl;
}


Write function:
Code: [Select]

unsigned char cmd[] = "INIT \r";
int n_written = 0,
    spot = 0;

do {
    n_written = write( fd, &cmd[spot], 1 );
    spot += n_written;
} while (cmd[spot-1] != '\r' && n_written > 0);


Arduino:
Code: [Select]

bool dataRecieved = false;
int ledpin = 13;

void setup() {
pinMode(ledpin, OUTPUT);
digitalWrite(ledpin, HIGH);
Serial.begin(19200);
}

void loop() { 
  while(!dataRecieved)
  {
   digitalWrite(ledpin,HIGH);
   if (Serial.available() > 0)
   {
     dataRecieved = true;
   }
  }
  digitalWrite(ledpin,LOW);
  delay(1000);
  digitalWrite(ledpin,HIGH);
  delay(1000);
  Serial.println("hi");
}

Robin2

Have a look at this Python - Arduino demo  It should be straightforward to convert the Python code to C++.

It is important that the PC program should open the serial port and keep it open until it is finished with the Arduino. Also it must allow time for the Arduino's reset process when the port is opened.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

kostor

Have a look at this Python - Arduino demo  It should be straightforward to convert the Python code to C++.

It is important that the PC program should open the serial port and keep it open until it is finished with the Arduino. Also it must allow time for the Arduino's reset process when the port is opened.

...R
Thanks robin, I remembered not to make that mistake (of closing the port each time).
Turns out I was neglecting the bootloader time it took to.. well, bootload,  and sent data
BEFORE the arduino was even running its code.
After adding a second delay in the c++ code (which is fine by me, I'm not looking for
time efficiency in this project at all) everything worked out.

Thanks for the heads up as well!

Go Up