Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You might try some code something like this after you've opened the file, but before you try to use it:</p> <pre><code>COMMTIMEOUTS timeouts; timeouts.ReadIntervalTimeout = 1; timeouts.ReadTotalTimeoutMultiplier = 1; timeouts.ReadTotalTimeoutConstant = 1; timeouts.WriteTotalTimeoutMultiplier = 1; timeouts.WriteTotalTimeoutConstant = 1; if (!SetCommTimeouts(m_hCommPort, &amp;timeouts)) // setting timeouts failed. </code></pre> <p>Edit: perhaps it's easier to start with some code that works, and make it do what you want rather than trying to get your code to work. Here's a simple terminal program. It's minimalist in the extreme, but does work (at least well enough to let me see output from my GPS, for one example). It's a long ways from what anybody (least of all me) would call sophisticated, but should give at least some idea of how to get started.</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;conio.h&gt; #include &lt;string.h&gt; #define STRICT #define WIN32_LEAN_AND_MEAN #include &lt;windows.h&gt; void system_error(char *name) { // Retrieve, format, and print out a message from the last error. The // `name' that's passed should be in the form of a present tense noun // (phrase) such as "opening file". // char *ptr = NULL; FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, 0, GetLastError(), 0, (char *)&amp;ptr, 1024, NULL); fprintf(stderr, "\nError %s: %s\n", name, ptr); LocalFree(ptr); } int main(int argc, char **argv) { int ch; char buffer[1]; HANDLE file; COMMTIMEOUTS timeouts; DWORD read, written; DCB port; HANDLE keyboard = GetStdHandle(STD_INPUT_HANDLE); HANDLE screen = GetStdHandle(STD_OUTPUT_HANDLE); DWORD mode; char port_name[128] = "\\\\.\\COM3"; char init[] = ""; // e.g., "ATZ" to completely reset a modem. if ( argc &gt; 2 ) sprintf(port_name, "\\\\.\\COM%c", argv[1][0]); // open the comm port. file = CreateFile(port_name, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL); if ( INVALID_HANDLE_VALUE == file) { system_error("opening file"); return 1; } // get the current DCB, and adjust a few bits to our liking. memset(&amp;port, 0, sizeof(port)); port.DCBlength = sizeof(port); if ( !GetCommState(file, &amp;port)) system_error("getting comm state"); if (!BuildCommDCB("baud=19200 parity=n data=8 stop=1", &amp;port)) system_error("building comm DCB"); if (!SetCommState(file, &amp;port)) system_error("adjusting port settings"); // set short timeouts on the comm port. timeouts.ReadIntervalTimeout = 1; timeouts.ReadTotalTimeoutMultiplier = 1; timeouts.ReadTotalTimeoutConstant = 1; timeouts.WriteTotalTimeoutMultiplier = 1; timeouts.WriteTotalTimeoutConstant = 1; if (!SetCommTimeouts(file, &amp;timeouts)) system_error("setting port time-outs."); // set keyboard to raw reading. if (!GetConsoleMode(keyboard, &amp;mode)) system_error("getting keyboard mode"); mode &amp;= ~ ENABLE_PROCESSED_INPUT; if (!SetConsoleMode(keyboard, mode)) system_error("setting keyboard mode"); if (!EscapeCommFunction(file, CLRDTR)) system_error("clearing DTR"); Sleep(200); if (!EscapeCommFunction(file, SETDTR)) system_error("setting DTR"); if ( !WriteFile(file, init, sizeof(init), &amp;written, NULL)) system_error("writing data to port"); if (written != sizeof(init)) system_error("not all data written to port"); // basic terminal loop: do { // check for data on port and display it on screen. ReadFile(file, buffer, sizeof(buffer), &amp;read, NULL); if ( read ) WriteFile(screen, buffer, read, &amp;written, NULL); // check for keypress, and write any out the port. if ( kbhit() ) { ch = getch(); WriteFile(file, &amp;ch, 1, &amp;written, NULL); } // until user hits ctrl-backspace. } while ( ch != 127); // close up and go home. CloseHandle(keyboard); CloseHandle(file); return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload