1
我使用USB to Uart转换器来传输和接收我的数据。 这里是我的传输码错误errno 11资源暂时不可用
void main()
{
int USB = open("/dev/ttyUSB0", O_RDWR | O_NONBLOCK | O_NDELAY);
struct termios tty;
struct termios tty_old;
memset (&tty, 0, sizeof tty);
/* Set Baud Rate */
cfsetospeed (&tty, (speed_t)B9600);
cfsetispeed (&tty, (speed_t)B9600);
/* Setting other Port Stuff */
tty.c_cflag &= ~PARENB; // Make 8n1
tty.c_cflag &= ~CSTOPB;
tty.c_cflag &= ~CSIZE;
tty.c_cflag |= CS8;
tty.c_cflag &= ~CRTSCTS; // no flow control
tty.c_cc[VMIN] = 1; // read doesn't block
tty.c_cc[VTIME] = 5; // 0.5 seconds read timeout
tty.c_cflag |= CREAD | CLOCAL; // turn on READ & ignore ctrl lines
/* Make raw */
cfmakeraw(&tty);
/* Flush Port, then applies attributes */
tcflush(USB, TCIFLUSH);
/* WRITE */
unsigned char cmd[] = "YES this program is writing \r";
int n_written = 0,spot = 0;
do {
n_written = write(USB, &cmd[spot], 1);
spot += n_written;
} while (cmd[spot-1] != '\r' && n_written > 0);
为expacted
YES this program is writing
现在,这是我从UART
阅读/* READ */
int n = 0,spot1 =0;
char buf = '\0';
/* Whole response*/
char response[1024];
memset(response, '\0', sizeof response);
do {
n = read(USB, &buf, 1);
sprintf(&response[spot1], "%c", buf);
spot1 += n;
} while(buf != '\r' && n > 0);
if (n < 0) {
printf("Error reading %d %s",errno, strerror(errno));
}
else if (n==0) {
printf("read nothing");
}
else {
printf("Response %s",response);
}
}
这个读数来自UART的代码我的代码的输出是相同的从errno给出错误,它是错误号11,表示资源暂时不可用
我得到这个输出
Error reading 11 Resource temporarily unavailable
我使用USB转UART转换器。希望有人能帮助。谢谢:)
AKA'我把它设置为非阻塞,并且当它没有阻止时感到惊讶':) –
它显示错误:继续语句不在循环内 –
嗯......你在做什么?循环?你删除了循环? –