Hi guys,

I'm quite new to this area but've managed to get some help from online
searches
and created the following program. A bit crude & messy for now. I
apologise for that.

However, I'm unable to send any bytes using WriteFile. Kept on getting
error.

Here's the code which I'm having an issue with :

------------------------------------------------------------------

LPSTR szText;
UCHAR * cl;
unsigned long BytesWritten;
bool bResult;

cl = converted_value_str;
_tochar sztext((char *)cl);
DWORD write = sizeof(cl);
if( ! PurgeComm( h, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR |
PURGE_RXCLEAR ) )
{
printf("\nPurgeComm error!!");
}

bResult = WriteFile(h, cl, sizeof(cl) + 1, &BytesWritten, NULL);
DWORD dw1 = GetLastError();
printf("\nbResult1 = %b", bResult);
printf("\nGetLastError1 = %u", dw1);
printf("\nsizeof(cl) = %i", sizeof(cl));
printf("\nBytesWritten1 = %u", &BytesWritten);

------------------------------------------------------------------

This is the error which I've got :

Enter a command (y/n) : y
Please enter a long string: 11

Buf_Len = 2
GetLastError1 = 87
sizeof(cl) = 4
bResult1 = 0
BytesWritten1 = 9305948

Can someone advise me why I'm having this error and how I can send any
data to my serial port? Fyi, I'm using COM1. Pls help!! Need this
urgently for my project.

Thanks in adv!


Yours Sincerely,
GK

Re: WriteFile Error by William

William
Mon Apr 26 10:28:31 CDT 2004

"Reis" <gkteo@yahoo.com> wrote in message
news:fec512cd.0404260656.55ba9d33@posting.google.com...
> I'm quite new to this area but've managed to get some help from online
> searches and created the following program. A bit crude & messy for now. I
> apologise for that.
>
> However, I'm unable to send any bytes using WriteFile. Kept on getting
> error.

The error that you are getting is defined in

<winerror.h>

It is signaling an invalid parameter. Did you open the device with the
FILE_FLAG_OVERLAPPED bit set? If so you must pass the address of an
OVERLAPPED structure and not a NULL pointer.

Regards,
Will



Re: WriteFile Error by Scott

Scott
Mon Apr 26 13:04:09 CDT 2004

Reis wrote:

> Hi guys,
>
> I'm quite new to this area but've managed to get some help from online
> searches
> and created the following program. A bit crude & messy for now. I
> apologise for that.
>
> However, I'm unable to send any bytes using WriteFile. Kept on getting
> error.
>
> Here's the code which I'm having an issue with :
>
> ------------------------------------------------------------------
>
> LPSTR szText;
> UCHAR * cl;
> unsigned long BytesWritten;
> bool bResult;
>
> cl = converted_value_str;
> _tochar sztext((char *)cl);
> DWORD write = sizeof(cl);
> if( ! PurgeComm( h, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR |
> PURGE_RXCLEAR ) )
> {
> printf("\nPurgeComm error!!");
> }
>
> bResult = WriteFile(h, cl, sizeof(cl) + 1, &BytesWritten, NULL);
> DWORD dw1 = GetLastError();
> printf("\nbResult1 = %b", bResult);
> printf("\nGetLastError1 = %u", dw1);
> printf("\nsizeof(cl) = %i", sizeof(cl));
> printf("\nBytesWritten1 = %u", &BytesWritten);
>
> ------------------------------------------------------------------
>
> This is the error which I've got :
>
> Enter a command (y/n) : y
> Please enter a long string: 11
>
> Buf_Len = 2
> GetLastError1 = 87
> sizeof(cl) = 4
> bResult1 = 0
> BytesWritten1 = 9305948
>
> Can someone advise me why I'm having this error and how I can send any
> data to my serial port? Fyi, I'm using COM1. Pls help!! Need this
> urgently for my project.
>
> Thanks in adv!
>
>
> Yours Sincerely,
> GK

It is not clear what you are doing to cl, but it is probably an invalid
address. Also, sizeof(cl) is always going to be 4 because it is a
pointer type. You can't use sizeof to get the size of a string.

--
Scott McPhillips [VC++ MVP]


Re: WriteFile Error by gkteo

gkteo
Mon Apr 26 20:04:19 CDT 2004

"William DePalo [MVP VC++]" <willd.no.spam@mvps.org> wrote in message news:<#2qxeM6KEHA.1000@TK2MSFTNGP11.phx.gbl>...
> "Reis" <gkteo@yahoo.com> wrote in message
> news:fec512cd.0404260656.55ba9d33@posting.google.com...
> > I'm quite new to this area but've managed to get some help from online
> > searches and created the following program. A bit crude & messy for now. I
> > apologise for that.
> >
> > However, I'm unable to send any bytes using WriteFile. Kept on getting
> > error.
>
> The error that you are getting is defined in
>
> <winerror.h>
>
> It is signaling an invalid parameter. Did you open the device with the
> FILE_FLAG_OVERLAPPED bit set? If so you must pass the address of an
> OVERLAPPED structure and not a NULL pointer.
>
> Regards,
> Will


Hi Will,

thanks for your help. I will change the WriteFile() parameters. Btw,
is there a limitation to WriteFile()? I need to send in cmd characters
to my printer but it keeps printing "12" instead. 12Hex is the command
I wish to send to the printer. Pls advise.

Thanks for your help.

Best Regards,
Reis