write() return errno 14 why?

c, linux, serial-port

Solution

`write()` is `int write( int fd, void *buf, int n )` so you must not pass an integer as second parameter. I guess you haven't included `unistd.h` otherwise your code shouldn'et even compile. Instead refer to the man page and call

write( fd, &DataBAR, 1 )

But be aware that it depends on the endiannes of your system whether you write the most or the least significant byte by doing that. Better define `DataBAR` as char (or copy the value into a char `c` and use `&c` in `write()`)

Problem

I have `unsigned int DataBAR` and want to send `char` to the serial port! My code is: ``` 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` returns `errno=14` how to solve this problem? With pic18f in C I use this code and it is working: ``` 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); } ``` I`m now in this and thanks for your help!!!

Original source