1

Am trying to send a command to a serial device (arduino) and receive a response, the command goes through but I don't get a response from the target device.

I want the C program to control the serial device by sending & receiving data.

Arduino Code:

void setup() {
    // initialize serial communication:
    Serial.begin(9600);
}

void loop() {
    if (Serial.available() > 0) {
        int inByte = Serial.read();
        switch (inByte) {
          case 'H':
              Serial.print("75864d63001bebaa");
              break;
        }
    }
}

C Code:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdint.h>
#include <fcntl.h>
#include <termios.h>
#include <errno.h>
#include <sys/ioctl.h>


int main(int argc, char *argv[]) {
    int fd, n, i;
    char buf[64] = "temp text";
    struct termios toptions;

    /* open serial port */
    fd = open("/dev/ttyACM0", O_RDWR | O_NOCTTY);
    printf("fd opened as %i\n", fd);

    /* wait for the Arduino to reboot */
    usleep(3500000);

    /* get current serial port settings */
    tcgetattr(fd, &toptions);
    /* set 9600 baud both ways */
    cfsetispeed(&toptions, B9600);
    cfsetospeed(&toptions, B9600);
    /* 8 bits, no parity, no stop bits */
    toptions.c_cflag &= ~PARENB;
    toptions.c_cflag &= ~CSTOPB;
    toptions.c_cflag &= ~CSIZE;
    toptions.c_cflag |= CS8;
    /* Canonical mode */
    toptions.c_lflag |= ICANON;
    /* commit the serial port settings */
    tcsetattr(fd, TCSANOW, &toptions);

    /* Send byte to trigger Arduino to send string back */
    write(fd, "H", 1);

    /* Receive string from Arduino */
    n = read(fd, buf, 64);
    /* insert terminating zero in the string */
    buf[n] = 0;

    printf("%i bytes read, buffer contains: %s\n", n, buf);
    return 0;
}
alk
  • 69,737
  • 10
  • 105
  • 255
Djmc
  • 21
  • 1
  • 4
  • Where your comment says: 'wait for arduino to reboot", you are not actually waiting, perhaps the arduino is still starting up by the time you write and therefore nothing to be read? The instructions following the opening of the port are quite quickly accomplished on most machines – Patrick Sturm Oct 06 '18 at 01:05
  • Sorry I forgot to add it when I copy past the code but I have usleep(3500000); under reboot and still not working – Djmc Oct 06 '18 at 01:17
  • usleep might cause a system issue if arg >=1,000,000 and could possibly be a no-op, perhaps use sleep with smaller value? only about 2s – Patrick Sturm Oct 06 '18 at 01:21
  • what is the return value from open? Perhaps your current linux user is not apart of the dialout group? – Patrick Sturm Oct 06 '18 at 01:26
  • fd opened as 3, running under pi user – Djmc Oct 06 '18 at 01:31
  • I usually use this code as a starting point when playing with serial ports: https://stackoverflow.com/questions/6947413/how-to-open-read-and-write-from-serial-port-in-c – Patrick Sturm Oct 06 '18 at 01:37
  • Someone should post an answer to this problem. @Djmc, you are responsible for that when you find a solution, if nobody else posts it. – jwdonahue Oct 06 '18 at 01:54
  • If I find a solution I'll post it no problem. – Djmc Oct 06 '18 at 01:59

0 Answers0