cant read arduino data on my macbook

I'm having trouble reading data from my arduino uno on my macbook (macos version 10.14.1) ...
basically, my 1 byte read calls return garbage data ...
the arduino serial monitor, when set to 115200 baud reads the character based input from the arduino fine ...
but I'm having trouble setting that baud rate from my macos basedC program, which is trying to read the device ...

the code below fails on the tcsetattr call with an errno of 22 (EINVAL) ...
I think because the speed value exceeds 38400 bpi ...
(the man page that contains tcsetattr has baud rate values up to 38400 ...)

char *arduino = "/dev/cu.usbmodem14101";
struct termios cucontrol;
speed_t inspeed;

...

fd = open(arduino, O_RDONLY);
if(fd == -1){
printf("cant open %s\n",arduino);
exit(0);
}

if(tcgetattr(fd,&cucontrol) == -1){
printf("ctcgetattr failure errno = %d\n",errno);
exit(0);
}
inspeed = cfgetispeed(&cucontrol);
printf("initial input speed is %d\n",inspeed);

inspeed = 115200;
if(cfsetispeed(&cucontrol,inspeed) == -1){
printf("problem setting speed of %d, errno = %d\n", inspeed, errno);
exit(0);
}

if(tcsetattr(fd,TCSANOW,&cucontrol) == -1){
printf("tcsetattr failure, errno = %d\n",errno);
exit(0);
}

stty -g reveals a baud rate of 115200 when the arduino serial monitor is running ...
and 9600 baud when it is not running ....

I tried setting the baud rate on the arduino side to 9600 via a Serial.begin(9600),
but that seemed to have no effect ....

I don't know how the arduino serial monitor manages to set the baud rate to 115200 ...

suggestions/comments appreciated ...

I don't have a Mac and I have never written a C program on a PC but it looks to me like all those exit(0) statements close the serial port.

When the Serial port is opened the Arduino will reset and the PC program must allow time for that and should then keep the serial port open until it is completely finished with the Arduino.

This Python - Arduino demo illustrates the idea.

...R

The explain_tcgetattr function is used to obtain an explanation of an error returned by the tcgetattr(3) system call. The least the message will contain is the value of strerror(errno), but usually it will do much better, and indicate the underlying cause in more detail.

So, use it. And, learn to use google.

thanks for the explain_tcgetattr suggestion ...

I had no problem with tcgetattr ... it was tcsetattr that returned an errno of 22 (EINVAL) ...

the functions explain_tcgetattr and explain_tcsetattr aren't available in (my version) of macos ....

getting back to this after a bit of time away ...

I can now talk to the Arduino at 9600 baud only ...
the Arduino sketch (which I didn't write) was using 115200 baud ...
when I changed it to 9600 baud, I am able to get data from it to my macbook...

the IDE serial monitor seems to work at 115200 baud ok (when matched with the sketch's Serial.begin setting ...
but any effort I try to set the baud rate on my macbook side to other than 9600
(using tcgetattr and tsetattr) fails with errno = 22.

I don't know how the IDE on the macbook sets to baud rate to 115200 ...
I'll see if I can get the IDE source code to see what it does ...

thanks for all the previous help on this forum ....

There are a few functions that can print better messages.

Play a little with the below, it might reveal something about the error.
man perror
man strerror
man error

Be aware that errno can change due to other calls if you don't use it immediately. Saving it first might be advisable if needed; see man errno