Всем привет.
Реализую 9-битный протокол передачи, на подобие [C++] /dev/ttyS0 9-bit protocol от necromant
Изучил и первоисточник http://www.lothosoft.ch/thomas/libmip/markspaceparity.php
В теории всё понятно.
Вот часть кода:
int main(void)
{
int FileHandle;
unsigned char LedOn[] = {0x01,0x05,0x00,0x07,0xB4};
FileHandle = open("/dev/ttyS0", O_RDWR | O_NOCTTY);
if ( FileHandle<0 )
{
puts ("error ");
FileHandle=-1;
return EXIT_FAILURE;
}
puts(" serial device opened" );
// Set port Settings
struct termios newtio;
bzero(&newtio, sizeof(newtio));
newtio.c_cflag = B115200 | CS8 | CLOCAL | CREAD;
newtio.c_iflag = IGNPAR | IGNBRK;
newtio.c_oflag = 0;
newtio.c_lflag = 0;
newtio.c_cc[VTIME] = 0;
newtio.c_cc[VMIN] = 1;
tcflush(FileHandle, TCIOFLUSH);
tcsetattr(FileHandle, TCSANOW, &newtio);
// Set non-blocking mode
fcntl(FileHandle, F_SETFL, O_NONBLOCK);
SendPacket_9bit(FileHandle,LedOn,5);
close( FileHandle );
return EXIT_SUCCESS;
}
void SendPacket_9bit(int mFileHandle, unsigned char *packet, int len)
{
int err;
struct termios tio;
bzero(&tio, sizeof(tio));
tcgetattr(mFileHandle, &tio);
// Send first byte with mark parity
tio.c_cflag |= PARENB;
tio.c_cflag |= CMSPAR;
tio.c_cflag |= PARODD;
err = tcsetattr(mFileHandle, TCSADRAIN, &tio);
if(err) puts(" tcsetattr error ");
write(mFileHandle, packet, 1);
// Send the rest with space parity
tio.c_cflag &= ~PARODD;
if(err) puts(" tcsetattr error ");
write(mFileHandle, packet+1, len-1);
}
Но опыт показывает что, второе применение функции tcsetattr() после write() почему то безрезультатный. Т.е передаётся первый байт после первого write() в функции SendPacket_9bit, а остальные где то теряются. Всю голову уже поломал, не знаю куда копать