#include #include #include #include #include using namespace std; int main(void) { COMMTIMEOUTS timeouts = { 0 }; HANDLE h = CreateFile("COM1", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); if (h == INVALID_HANDLE_VALUE) { cout << "Erreur: Impossible d´ouvrir le port série" << endl; return 1; } DCB dcb = { 0 }; BOOL dcbOk = GetCommState(h, &dcb); dcb.BaudRate = CBR_115200; dcb.ByteSize = 8; dcb.Parity = NOPARITY; dcb.StopBits = TWOSTOPBITS; dcbOk = SetCommState(h, &dcb); timeouts.ReadIntervalTimeout = 100; timeouts.ReadTotalTimeoutMultiplier = 100; timeouts.ReadTotalTimeoutConstant = 100; timeouts.WriteTotalTimeoutMultiplier = 100; timeouts.WriteTotalTimeoutConstant = 100; if (!SetCommTimeouts(h, &timeouts)) return 1; // Test de l'écho for (int i = 0; i < 5; ++i) { char sentChar = '0' + i; char receivedChar = '#'; DWORD bytesWritten; DWORD bytesRead; if (!WriteFile(h, &sentChar, 1, &bytesWritten, 0)) return 1; if (!ReadFile(h, &receivedChar, 1, &bytesRead, 0) || bytesRead != 1) cout << sentChar << " : pas de reponse ?" << endl; else cout << sentChar << " = " << receivedChar << endl; } CloseHandle(h); return 0; }