Reputation: 1096
im trying to write some bytes to the serial port on mac OSX 10.9.3 with the unistd.h from standard C. I know there is a similar topic, but its 4 years old and there is a different problem, so please excuse me for starting a new thread.
My problem is the write(); function, it always returns with a value of -1. The code for opening my port is as follows:
curByte = 0;
numBytes = 0;
bool blocking = false;
//Settings structure old and new
struct termios newtio;
fd = open(port, O_RDWR | O_NOCTTY | (blocking ? 0 : O_NDELAY));
if (fd < 0)
{
return PORT_ERROR;
}
bzero(&newtio, sizeof(newtio));
if (cfsetispeed(&newtio, BDR_ESP3) != 0)
return PORT_ERROR;
if (cfsetospeed(&newtio, BDR_ESP3) != 0)
return PORT_ERROR;
newtio.c_cflag &= ~PARENB;
newtio.c_cflag &= ~CSTOPB;
newtio.c_cflag &= ~CSIZE;
newtio.c_cflag |= CS8;
newtio.c_cflag &= ~CRTSCTS;
//Hardware control of port
newtio.c_cc[VTIME] = blocking ? 1 : 0; // Read-timout 100ms when blocking
newtio.c_cc[VMIN] = 0;
tcflush(fd, TCIFLUSH);
//Acquire new port settings
if (tcsetattr(fd, TCSANOW, &newtio) != 0)
puts(strerror(errno));
return OK;
I can open the port without problems, but once i try to write a byte to the port with the following method:
{
int res;
if (fd == -1)
return PORT_ERROR;
res = write(fd, &u8TxByte, 1);
if (res == -1){
printf("Error writing to port - %s(%d).\n", strerror(errno), errno);
}
if (res == 1)
printf("Writing to port succeeded!");
return OK;
return ERROR;
}
I keep getting error outputs like this:
Error writing to port - Resource temporarily unavailable(35).
It's driving me nuts! Because the same code actually works on Linux, so where is the difference here to OSX? What does it mean the Resource is temporarily unavailable?
I hope i was describing my problem well enough, and someone can help me! Thanks!
Upvotes: 2
Views: 2012
Reputation: 3335
use the corresponding /dev/cu.xxx
device and your program will probably work.
/dev/tty.xxx
is meant for incoming connections on OS/X and will only work if DCD is asserted.
Upvotes: 7