// // Quick demo - Play with modem signals. // // tvb 08-March-2003 // #include #include #include #include int Port; // // Handshake pin functions // void DtrOpen (); void DtrClose (); void DtrMinusV (); void DtrPlusV (); void Wait1PPS (); void TestDsr () { int i; time_t t; printf("Test: read DSR (DE9 pin 6)\n"); printf(" - rising edge detect on DSR\n"); DtrOpen(); for (i = 0; i < 10; i += 1) { Wait1PPS(); time(&t); printf("Caught pulse on DSR %s", ctime(&t)); } DtrClose(); } void TestDtr () { int i; printf("Test: write DTR (DE9 pin 4)\n"); printf(" - assert DTR (+12 V) for 4 seconds\n"); printf(" - deassert DTR (-12 V) for 1 second\n"); DtrOpen(); for (i = 0; i < 10; i += 1) { printf("+"); DtrPlusV(); _sleep(4000); printf("-"); DtrMinusV(); _sleep(1000); } DtrClose(); } // // RS-232 DE9S (9-pin female socket) // as seen from cable going into PC. // // 5 4 3 2 1 // ----------------------- // \ O O O O O / // \ / // \ O O O O / // ----------------- // 9 8 7 6 // // RS-232 DE9P (9-pin male plug) // as seen from back of PC // // 1 2 3 4 5 // ----------------------- // \ . . . . . / // \ / // \ . . . . / // ----------------- // 6 7 8 9 // // Signals (PC is DTE) // // pin 1 - DCD (PC input) // pin 2 - Rx (PC input) // pin 3 - Tx (PC output) // pin 4 - DTR (PC output) // pin 5 - gnd // pin 6 - DCE (PC input) // pin 7 - RTS (PC output) // pin 8 - CTS (PC input) // pin 9 - RI // // Quiescent Voltages: // // 1 2 3 4 5 6 7 8 9 // // - - -12 -12 0 - +12 - - // void main (int argc, char *argv[]) { if (argc > 1) { Port = atoi(argv[1]); } else { Port = 1; } // Toggle DTR a few times TestDtr(); // Look for 1 PPS on DSR TestDsr(); } // ------------------------------------------------------------------------ // // // Win32 specific code for modem signals. // #include #define ASSERT_SUCCESS(Function) \ if (!Success) { \ fprintf(stderr, "File %s Line %d Function %s failed (%.8x)\n", \ __FILE__, __LINE__, #Function, GetLastError()); \ exit(1); \ } HANDLE hCom; void DtrOpen() { char CommPath[100]; DCB Dcb; BOOL Success; sprintf(CommPath, "\\\\.\\COM%d", Port); hCom = CreateFile(CommPath, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL); if (hCom == INVALID_HANDLE_VALUE) { fprintf(stderr, "CreateFile failed: %s (%.8x)\n", CommPath, GetLastError()); exit(1); } memset(&Dcb, 0x0, sizeof(DCB)); Dcb.DCBlength = sizeof (DCB); Success = GetCommState(hCom, &Dcb); ASSERT_SUCCESS(GetCommState); Dcb.Parity = NOPARITY; Dcb.BaudRate = 9600; Dcb.ByteSize = 8; Dcb.StopBits = ONESTOPBIT; Dcb.fDtrControl = DTR_CONTROL_ENABLE; Dcb.fRtsControl = RTS_CONTROL_ENABLE; Success = SetCommState(hCom, &Dcb); ASSERT_SUCCESS(SetCommState); printf("Opened: %s\n", CommPath); return; } void DtrClose () { CloseHandle(hCom); return; } // // Deasserting DTR makes the pin -12 V (nominal) // void DtrMinusV () { BOOL Success; Success = EscapeCommFunction(hCom, CLRDTR); ASSERT_SUCCESS(EscapeCommFunction); return; } // // Asserting DTR makes the pin +12 V (nominal) // void DtrPlusV () { BOOL Success; Success = EscapeCommFunction(hCom, SETDTR); ASSERT_SUCCESS(EscapeCommFunction); return; } // // Wait for rising edge of DSR (DB9 pin6) // void Wait1PPS () { DWORD Modem; int New, Old; BOOL Success; New = 1; do { Success = GetCommModemStatus(hCom, &Modem); ASSERT_SUCCESS(GetCommModemStatus); Old = New; New = (Modem & MS_DSR_ON) ? 1 : 0; } while ( !(Old == 0 && New == 1) ); }