I'm not really experienced in programming, but I'm trying to port C++ code from Linux to Windows. It's a function which writes to the serial port and reads the answer, which is then parsed. I could probably put this together, but what I'm not sure about is how to port the following from Linux to Windows:
void serialPort::mark() {
options.c_cflag |= PARENB | CMSPAR | PARODD;
tcsetattr(fd, TCSAFLUSH, &options);
}
and
void serialPort::space() {
options.c_cflag |= PARENB | CMSPAR;
options.c_cflag &= ~PARODD;
tcsetattr(fd, TCSANOW, &options);
These functions are used to switch between mark and space parity when writing to the serial port - the first byte from a command must be written with mark parity and the rest with space parity, something like this:
char sum = checksum(length, command);
char* bufptr;
int nbytes = 0;
mark();
write(fd, &length, 1); // Write first byte with mark parity
usleep(2000);
read(fd, buffer, 255);
space(); // Write remaining bytes with space parity
for(int i=0; i<length; i++) {
write(fd, &command[i], 1);
usleep(2000);
read(fd, buffer, 1);
}
write(fd, &sum, 1);
But I'm stuck on this. I don't even know if it's possible in Windows. How can I get started?
The equivalent operation on Windows is to call SetCommState with the appropriate flag in the Parity member of the passed DCB structure. It does have support for mark parity and space parity.