1

I have a device that is connected on port COM4, (115200 baud, 8-N-1). Based on examples I found here I'm opening the port with:

                    Keyboard_Handle=CreateFile("\\\\.\\COM4",GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,NULL); 
                    if(GetLastError() !=0 || Keyboard_Handle == INVALID_HANDLE_VALUE)
                    {
                        AfxMessageBox("Error opening connection to Keyboard");
                        exit(1);
                    }


                    char buffer[100];
     strcpy(buffer,"baud=115200 parity=N data=8 stop=1");
     BuildCommDCB((char*)&buffer,&dcb)) 

     if(GetCommState(Keyboard_Handle, &dcb))
     {
      dcb.BaudRate = CBR_115200;
      dcb.ByteSize = 8;
      dcb.Parity = 0;
      dcb.StopBits = 1;
      SetCommState(Keyboard_Handle, &dcb);
     } 


Later in my code I call WriteFile on the port with:

    LPDWORD bytes_written;
    LPDWORD bytes_read;
    LPOVERLAPPED OVERLAP;
    char write_buf[10];

    write_buf[0] = 's';
    write_buf[1] = '\0';

    if(Keyboard_Handle != NULL) {
    WriteFile(Keyboard_Handle, (LPCVOID)write_buf , strlen(write_buf), bytes_written, OVERLAP); 
    }

And every time I run the code I get the JIT Debugger complaining about an unhandled exception (though the WriteFile is inside a Try/catch block).

Is there something wrong with how I'm doing this?

2 Answers 2

8

bytes_written needs to be an address of a variable; the compiler wouldn't compile the statement you posted.

Likewise, "OVERLAP" doesn't make sense.

Did you check whether CreateFile succeeded?

What's in write_buf when you call strlen on it?

Try copy-and-pasting the actual code that you're using.

Also that doesn't seem like a very good/informative sample that you're using. Try Windows Serial Port Programming and http://msdn.microsoft.com/en-us/library/ms810467.aspx

Also start with the sample program from the Microsoft site, test it before you modify it (to check that it's working on your machine) and then modify it.

Sign up to request clarification or add additional context in comments.

4 Comments

First of all, I mention that the code runs, which means that it compiled. Secondly, I added some code that was trimmed off the initial post. Thirdly, thanks for the links.
Now that you've added the declaration of bytes_written, I see the problem: it's a pointer, and you haven't initialized it before you use it. Ditto the OVERLAP pointer (you haven't initialized it before you used it either). Also, a failure to open the COM port would mean that the handle contained a value of INVALID_HANDLE_VALUE (not 0).
It isn't normal to use a pointer for the "number of bytes written" parameter. A more usual way would be to declare it as type DWORD (and not as type LPDWORD), and to pass it by pointer using a & e.g. as &dwBytesWritten: so that you're passing a value by pointer, instead of passing a pointer by value (and in your case, an uninitialized pointer).
Thanks for that, it's little things like that that keep tripping me up working on this code. Making the changes you suggested allowed me to see I was getting an error code of 997, which I'm working on resolving now. Thanks.
0

When you call SetCommState is the return value 0? It could be erroring out which could be causing more problems.

Also, have you stepped through line by line to make sure that it is the WriteFile call that crashes it?

Lastly, you could have some antivirus or other software hooking into your app causing these problems (look for unknown dll's loaded in your module list).

3 Comments

The return value is 0. On a second look, I replaced the WriteFile line with a ReadFile call, and it also crashed, but without either the code runs fine.
I'm new to Visual Studio, and I still haven't figured out where the module list is.
When you're debugging, click on the Debug menu, Windows -> Modules (Ctrl + D, M if you're using default shortcuts)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.