Mam problem z transmisją przez RS232.
Jeżeli mikrokontroler i komputer są połączone to normalnie wysyłają dane.
Jednak jeżeli jeden z przewodów nie są połączone wówczas program "wisi" na procedurze ReadFile(). Pod linuxem wszystko działa i elegancko wychodzi z programu zgłaszając błąd transmisji. Pod windows-em (Visual studio c++) program się nie kończy w przypadku nie podłączenia przewodów rx i/ lub tx.
Jak należy sobie z tym poradzić?
Czytałem manuala, ale jakoś nie chce to działać i już tracę siły z tym czymś...
Liczę na jakąkolwiek pomoc
wysyłam listing i pozdrawiam
Kod: Zaznacz cały
unsigned int RS232::Read(void *buf, unsigned int size) {
#ifdef WIN32
DWORD cnt;
if ( ReadFile(handle, buf, size, &cnt, NULL) == FALSE )
return -1;
return cnt;
#else
return read(handle, buf, size);
#endif
}
Inicjalizacja portu
Kod: Zaznacz cały
bool RS232::Open(const char *name, unsigned int baudRate, bool parity, unsigned char stopBits) {
#ifdef WIN32
handle = NULL;
handle = CreateFile(name, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
if ( handle == INVALID_HANDLE_VALUE )
return false;
// com-port initialization
DCB dcb;
dcb.DCBlength = sizeof(DCB);
if( !GetCommState(handle, &dcb) ) {
Close();
return false;
}
dcb.BaudRate = baudRate; // baudrate (in bauds)
dcb.fBinary = true; // binary mode
dcb.fOutxCtsFlow = false; // disable the CTS sygnal listening
dcb.fOutxDsrFlow = false; // disable the DSR sygnal listening
dcb.fDtrControl = DTR_CONTROL_DISABLE; // disable the DTR-line
dcb.fDsrSensitivity = false; // ignore state of the DSR-line
dcb.fNull = false; // allow zerro-bytes input
dcb.fRtsControl = RTS_CONTROL_DISABLE; // not use the RTS-line
dcb.fAbortOnError = false; // disable abort on error
dcb.ByteSize = 8; // 8 bits per byte
dcb.Parity = parity ? 1 : 0; // paricy checking
dcb.StopBits = stopBits; // stop-bits
// load DCB to port
if( !SetCommState(handle, &dcb) ) {
Close();
return false;
}
// set timeouts
COMMTIMEOUTS timeouts;
timeouts.ReadIntervalTimeout = 0; // timeout beetwen to chars
timeouts.ReadTotalTimeoutMultiplier = 0; // general reading operations timeout
timeouts.ReadTotalTimeoutConstant = 0; // constant for the general reading operations timeout
timeouts.WriteTotalTimeoutMultiplier = 0; // general writing operations timeout
timeouts.WriteTotalTimeoutConstant = 0; // constant for the general writing operations timeout
if( !SetCommTimeouts(handle, &timeouts) ) {
Close();
return false;
}
// setup sizes of TX/RX quenes
if ( SetupComm(handle, 1024*2, 1024*2) == FALSE ) {
Close();
return false;
};
// clear the reading buffer
if ( PurgeComm(handle, PURGE_RXCLEAR) == FALSE ) {
Close();
return false;
};
#else
int baudr = getBaudrateValue(baudRate);
if ( baudr == 0 ) {
perror("invalid comport baudrate\n");
return false;
}
handle = open(name, O_RDWR | O_NOCTTY | O_NDELAY);
if( handle == -1 ) {
perror("unable to open comport");
return false;
}
int error = tcgetattr(handle, &old_port_settings);
if ( error == -1 ) {
close(handle);
perror("unable to read portattr");
return false;
}
memset(&new_port_settings, 0, sizeof(new_port_settings)); // clear the new struct
new_port_settings.c_cflag = baudr | CS8 | CLOCAL | CREAD;
if ( parity )
new_port_settings.c_cflag |= PARENB;
new_port_settings.c_iflag = IGNPAR; // Ignore characters with parity errors.
new_port_settings.c_oflag = 0;
new_port_settings.c_lflag = 0;
new_port_settings.c_cc[VMIN] = 0; // block untill n bytes are received
new_port_settings.c_cc[VTIME] = 0; // block untill a timer expires (n * 100 mSec.)
error = tcsetattr(handle, TCSANOW, &new_port_settings);
if ( error == -1 ) {
close(handle);
perror("unable to adjust portsettings ");
return false;
}
#endif
return true;
}