PDA

View Full Version : [Question(s)] C++ Serial issues :(



Superlaxstar112
08-25-2008, 11:19 PM
I need to write strings to the serial port on my computer. This is the code I've been using. I have been able to write to the port in Basic but not C++. I am on windows xp and Dev-C++

//code!
#include <windows.h>
#include <stdio.h>
#include <iostream>
#include <tchar.h>

using namespace std;


BOOL WriteABuffer(char * lpBuf, DWORD dwToWrite);


int main(){
char* text="00000\r"; //this is the string to write, can be any string
DWORD towrite;
if(!WriteABuffer(text, towrite)){
cout<<"Not Written";
}
else{
cout<<"Written!\n";
};
Sleep(25000);
}



BOOL WriteABuffer(char * lpBuf, DWORD dwToWrite)
{
HANDLE hComm;
hComm = CreateFile( "COM1",
GENERIC_READ | GENERIC_WRITE,
0,
0,
OPEN_EXISTING,
FILE_FLAG_OVERLAPPED,
0); OVERLAPPED osWrite = {0};
DWORD dwWritten;
DWORD dwRes;
BOOL fRes;

// Create this write operation's OVERLAPPED structure's hEvent.
osWrite.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
if (osWrite.hEvent == NULL)
// error creating overlapped event handle
return FALSE;

// Issue write.
WriteFile(hComm, lpBuf, dwToWrite, &dwWritten, &osWrite);
CloseHandle(osWrite.hEvent);
return fRes;
}

ENDCODE//

It always prints Not written because the write function returns false... Help :(

Adrenalynn
08-26-2008, 03:22 AM
I didn't realize you were using Dev-C++ (glorified gcc).

Have a look here: http://www.phanderson.com/tm128/dev-cpp_tm128_1.html very readable, very solid looking.

Hephaistos
08-26-2008, 07:09 AM
Boy, it's been awhile since I've had to do anything with C++ under Windows (not that I'm complaining... ; )

It's hard to tell exactly what's going on because it seems as though there may be more to the code than you posted. What you're returning is fRes, which gets initialized (by default) to false at the beginning of your WriteABuffer routine. You can see from the API reference (http://msdn.microsoft.com/en-us/library/aa363858.aspx) that this refers to an input parameter representing a template file handle. So I'm not sure why you would be returning it (or declaring it as a boolean for that matter as it should be HANDLE).

Another issue I see with your code is that you are using overlapped I/O. This is generally used when processing a large amount of data because it's asynchronous and more efficient (with large amounts of data). Overlapped I/O causes an error of I/O pending to be returned because basically what happens when you write data to an overlapped I/O port is that it just starts to write the data before the function returns. You probably want to be using generic writes.

Check out that link Adrenalynn posted as I think it's more correct than what you're using now. The guy who has that site (Peter H. Anderson) has a lot of really nice code (http://www.phanderson.com/).

Superlaxstar112
08-26-2008, 04:52 PM
Thank you so much. That was the only solution that has worked for me. I emailed Peter Anderson about using his code and he was said it was fine. Thanks guys!