I'm trying to use C to read and write data to the serial port. I've been working with a little test program I found online. The idea is for it to send a byte to an arduino, which will then return the string "Hello", which is read by the program. I'm running into issues opening the serial port, using the open function. My code is stalling at this point and not getting any further.
fd = open("/dev/tty.usbserial-AH02OC4V", O_RDWR | O_NOCTTY);
printf("fd opened as %i\n", fd);
printf("Serial Open Passed!\n");
Can anyone see why? I added a printf function directly after the open function to test whether or not it is being completed. I am working on a Mac. Complete code is below.
C:
#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>
#define DEBUG 1
int main(int argc, char *argv[])
{
printf("start\n");
int fd, n, i;
char buf[64] = "temp text";
struct termios toptions;
printf("start\n");
/* open serial port */
fd = open("/dev/tty.usbserial-AH02OC4V", O_RDWR | O_NOCTTY);
printf("fd opened as %i\n", fd);
printf("Serial Open Passed!\n");
/* 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, "0", 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);
if(DEBUG)
{
printf("Printing individual characters in buf as integers...\n\n");
for(i=0; i<n; i++)
{
printf("Byte %i:%i, ",i+1, (int)buf[i]);
}
printf("\n");
}
return 0;
}
Arduino: void setup() { Serial.begin(9600); }
void loop()
{
if (Serial.available() > 0)
{
Serial.println("Hello");
Serial.read();
}
}
char buf[64]; ...n = read(fd, buf, 64); .... buf[n] = 0;
is bad whenn==64
. Useread(fd, buf, 64-1);
– chux - Reinstate Monica