Writing to serial port through com3.

I'm not sure if this should have gone into the beginner, but nonetheless: I'm attempting to turn on an FTDI Usb Relay by writing 3 bytes 0xFF0101 as the manual says, and I'm failing at it. Specifically I'm having trouble using the writefile method. Using this code, I keep on getting the 997 error code, and I can't tell from the MSDN website what to make of it. I haven't been back to programming after several years for all of a week. You can bash me if you want for my use of the IOstream. But whatever.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
   OVERLAPPED osWrite = {0};
   static DWORD dwWritten;
   DWORD dwRes;
   BOOL fRes;
   //I don't really understand what the osWrite is, nor what this next part does.
   osWrite.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
   if (osWrite.hEvent == NULL)
      // error creating overlapped event handle
      return FALSE;
   const char toWrite = 0xFF0101;
   if(!WriteFile(hComm, &toWrite, 3, &dwWritten, &osWrite)){//<------HERE I'm having issue------
        cout<<GetLastError()<<": It's an error code. You should discover why it failed, Evin. It's weird.\n";
   }
   cout<<"Number of bytes written are:";
   cout<<dwWritten;


Thank you so much in advance: I've torn my head apart doing it.
Last edited on
const char toWrite = 0xFF0101;
Shouldn't that be an int, Or at least char[3]?
Last edited on
Okay. I believe the writefile function requires a char variable, correct me if I'm wrong. So: Following your advice, I now redefined it after much tweaking. This simple stuff should have been in the beginners code.

I tried both:
1
2
3
4
5
6
7
   char toWrite [3];
   toWrite[0] = 0xFF;
   toWrite[1] = 0x01;
   toWrite[2] = 0x01;
if(!WriteFile(hComm, &toWrite, 3, &dwWritten, &osWrite)){//<------HERE I'm having issue------
cout<<GetLastError()<<": It's an error code. You should discover why it failed, Evin. It's weird.\n";
}


And:

int toWrite = 0xFF0101;

I still get the same error: 997. But now I believe it's defined properly, and we can work from there.
Last edited on
According to msdn that code is ERROR_IO_PENDING which isn't a failure but pending completion asynchronously.

http://msdn.microsoft.com/en-us/library/aa365747%28v=vs.85%29.aspx
http://msdn.microsoft.com/en-us/library/ms681388%28v=vs.85%29.aspx
Solved!! Guys, don't be stupid like me and let windows choose your drivers all the time. Since 'twas my first try at something like this, I was confused. Code worked, with no errors.
evinism wrote:
Guys, don't be stupid like me and let windows choose your drivers all the time.


Ambiguous statement (can be taken one of two ways)
Topic archived. No new replies allowed.