// // Quick demo - More playing with modem signals. // // tvb 08-March-2003 // #include #include #include #include #define V 12 // // Handshake pin functions // int ComPort; void ComOpen (); void ComClose (); void SetDTR (int v); void SetRTS (int v); int GetDSR (); int GetCTS (); int GetRI (); void Wait1PPS (); // // 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 - - // // Asserting RTS/DTR makes the pin +12 V (nominal) // Deasserting RTS/DTR makes the pin -12 V (nominal) // void main (int argc, char *argv[]) { if (argc > 1) { ComPort = atoi(argv[1]); } else { ComPort = 1; } // // Test input pins. // if (1) { int Dsr, Cts, Ring; int i; printf("Begin DSR CTS RI test.\n"); ComOpen(); Dsr = Cts = Ring = 0; for (i = 0; i < 20; ) { if (Dsr != GetDSR()) { Dsr = GetDSR(); printf("DSR (pin 6) %d V\n", Dsr); i += 1; } if (Cts != GetCTS()) { Cts = GetCTS(); printf("CTS (pin 8) %d V\n", Cts); i += 1; } if (Ring != GetRI()) { Ring = GetRI(); printf("RI (pin 9) %d V\n", Ring); i += 1; } _sleep(100); } ComClose(); printf("End DSR CTS RI test.\n"); } // // Toggle DTR a few times // if (1) { 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"); ComOpen(); for (i = 0; i < 10; i += 1) { printf("+"); SetDTR(V); _sleep(4000); printf("-"); SetDTR(-V); _sleep(1000); } ComClose(); } // // Look for 1 PPS on DSR // if (1) { int i; time_t t; printf("Test: read DSR (DE9 pin 6)\n"); printf(" - rising edge detect on DSR\n"); ComOpen(); for (i = 0; i < 10; i += 1) { Wait1PPS(); time(&t); printf("Caught pulse on DSR %s", ctime(&t)); } ComClose(); } } // ------------------------------------------------------------------------ // // // 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 ComOpen() { char CommPath[100]; DCB Dcb; BOOL Success; sprintf(CommPath, "\\\\.\\COM%d", ComPort); 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 ComClose () { CloseHandle(hCom); return; } // // void SetDTR (int v) { BOOL Success; switch (v) { case V : Success = EscapeCommFunction(hCom, SETDTR); break; case -V : Success = EscapeCommFunction(hCom, CLRDTR); break; default : Success = 0; break; } ASSERT_SUCCESS(EscapeCommFunction); return; } void SetRTS (int v) { BOOL Success; switch (v) { case V : Success = EscapeCommFunction(hCom, SETRTS); break; case -V : Success = EscapeCommFunction(hCom, CLRRTS); break; default : Success = 0; break; } ASSERT_SUCCESS(EscapeCommFunction); return; } int GetDSR () { DWORD Modem; BOOL Success; Success = GetCommModemStatus(hCom, &Modem); ASSERT_SUCCESS(GetCommModemStatus); return (Modem & MS_DSR_ON) ? V : -V; } int GetCTS () { DWORD Modem; BOOL Success; Success = GetCommModemStatus(hCom, &Modem); ASSERT_SUCCESS(GetCommModemStatus); return (Modem & MS_CTS_ON) ? V : -V; } int GetRI () { DWORD Modem; BOOL Success; Success = GetCommModemStatus(hCom, &Modem); ASSERT_SUCCESS(GetCommModemStatus); return (Modem & MS_RING_ON) ? V : -V; } // // Wait for rising edge of DSR (DE9 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) ); }