/* Simple send and receive C example for communicating with the * Arduino echo program using UART4. * * Written by Derek Molloy for the book "Exploring BeagleBone: Tools and * Techniques for Building with Embedded Linux" by John Wiley & Sons, 2014 * ISBN 9781118935125. Please see the file README.md in the repository root * directory for copyright and GNU GPLv3 license information. */ #include #include #include #include #include int main(int argc, char *argv[]){ int file, count; if(argc!=2){ printf("Invalid number of arguments, exiting!\n"); return -2; } if ((file = open("/dev/ttyO4", O_RDWR | O_NOCTTY | O_NDELAY))<0){ perror("UART: Failed to open the file.\n"); return -1; } struct termios options; tcgetattr(file, &options); options.c_cflag = B57600 | CS8 | CREAD | CLOCAL; options.c_iflag = IGNPAR | ICRNL; tcflush(file, TCIFLUSH); tcsetattr(file, TCSANOW, &options); // send the string plus the null character if ((count = write(file, argv[1], strlen(argv[1])+1))<0){ perror("Failed to write to the output\n"); return -1; } usleep(100000); unsigned char receive[100]; if ((count = read(file, (void*)receive, 100))<0){ perror("Failed to read from the input\n"); return -1; } if (count==0) printf("There was no data available to read!\n"); else { receive[count]=0; //There is no null character sent by the Arduino printf("The following was read in [%d]: %s\n",count,receive); } close(file); return 0; }