我有unsigned int DataBAR
并希望将char
发送到串口!
我的代码是:
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);
}
我现在在这里,谢谢你的帮助!!!
答案 0 :(得分:4)
write()
为int write( int fd, void *buf, int n )
,因此您不得将整数作为第二个参数传递。我猜你没有包含unistd.h
,否则你的代码甚至不应该编译。而是参考the man page并致电
write( fd, &DataBAR, 1 )
但请注意,这取决于系统的endiannes是否通过这样做来编写最大或最不重要的字节。最好将DataBAR
定义为char(或将值复制到char c
并在&c
中使用write()
)