我有unsigned int DataBAR
并想发送char
到串口!write()返回errno 14为什么?
我的代码是:
unsigned char Printer_buffer[PRN_BUFFER_SIZE];
unsigned int DataBAR, DataD, DataT;
for (i = 0; i < 8; i++) {
SumaN = SumaN + (Printer_buffer[i] & 0x0F);
DataBAR = (Printer_buffer[i] & 0x0F) + 0x30;
nbytes = write(fd,DataBAR ,1); //want to send to the serial port
printf("write error code is %d !!!!!!!!!\n", errno);
if (nbytes != 1) {
printf("error writing on serial port!!!\n");
}
sleep(1);
SumaP = SumaP + ((Printer_buffer[i] >> 4) & 0x0F);
DataBAR = ((Printer_buffer[i] >> 4) & 0x0F) + 0x30;
nbytes = write(fd, DataBAR, 1);
printf("write error code is %d !!!!!!!!!\n", errno);
if (nbytes != 1) {
printf("error writing on serial port!!!\n");
}
sleep(1);
}
write
回到errno=14
如何解决这个问题呢?
在C PIC18F我用这个代码,它是工作:
for (i=0;i<8;i++){
SumaN=SumaN+(Printer_buffer[i] & 0x0F);
DataBAR=(Printer_buffer[i] & 0x0F) + 0x30;
while(BusyUART1());
putcUART1(DataBAR);
SumaP=SumaP+((Printer_buffer[i]>>4) & 0x0F);
DataBAR=((Printer_buffer[i]>>4) & 0x0F) + 0x30;
while(BusyUART1());
putcUART1(DataBAR);
}
我真的现在在这,感谢您的帮助!
errno 14 =错误的地址,您如何打开'fd'? –
fd = open(“/ dev/ttyS0”,O_RDWR | O_NOCTTY | O_NDELAY); (“open_port:Unable to open/dev/ttyS0 \ n”);如果(fd == -1){ \t \t perror \t \t exit(1); \t} –
串口配置:tcgetattr(fd,&termAttr); \t // baudRate = B115200;/*不需要*/ \t cfsetispeed(&termAttr,B57600); \t cfsetospeed(&termAttr,B57600); \t termAttr.c_cflag&=〜PARENB; \t termAttr.c_cflag&=〜CSTOPB; \t termAttr.c_cflag&=〜CSIZE; \t termAttr.c_cflag | = CS8; \t termAttr.c_cflag | =(CLOCAL | CREAD); \t termAttr.c_lflag&=〜(ICANON | ECHO | ECHOE | ISIG); \t termAttr.c_iflag&=〜(IXON | IXOFF | IXANY); \t termAttr.c_oflag&=〜OPOST; \t termAttr.c_cc [VMIN] = 3; \t termAttr.c_cc [VTIME] = 5; \t tcsetattr(fd,TCSANOW,&termAttr); –