Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

COM Port Reading

I have a problem in reading the COM1 port.
I opening the COM port by using CreateFile() fn.
Baud rate is set by using SetCommState() fn.
SetCommMask() is used for mask.
After this when i call ReadFile() fn, the program hangs in ReadFile() fn.
Anyone can give a skeleton code or reason for this.

Thanks,
Siva
[340 byte] By [Siva] at [2007-11-10 12:51:07]
# 1 Re: COM Port Reading
hComm = CreateFile((const char*) "COM1",GENERIC_READ |
GENERIC_WRITE,0,0,OPEN_EXISTING,0,0);

if(hComm== INVALID_HANDLE_VALUE)
{
... errrorr arrrrggsss,...
}

if(!GetCommState(hComm,&dcb))
{
... errrorr arrrrggsss,...
}

dcb.BaudRate = CBR_600; //hi,hi,hi
dcb.ByteSize =(BYTE)bytes;//number of bits/byte, 4-8
dcb.StopBits =(BYTE)stopbit;// 0,1,2 = 1, 1.5, 2
dcb.Parity =(BYTE)parity;// 0-4=no,odd,even,mark,space

/* set state */
if(!SetCommState(hComm,&dcb))
{
... errrorr arrrrggsss,...
}
/*write*/
if (!WriteFile(hComm, data, strlen(data), &dwWritten, NULL))
{
... errrorr arrrrggsss,...
}

/*read*/
if (!WriteFile(hComm, datatoread, dwNumberToRead, &dwread, NULL))
{
... errrorr arrrrggsss,...
}

CloseHandle( hComm);
/*I have not try but...*/
regards,
evaleto@gkb.com

Siva <siva1010@hotmail.com> wrote in message news:38cdcd69@news.dev-archive.com...
>
> I have a problem in reading the COM1 port.
> I opening the COM port by using CreateFile() fn.
> Baud rate is set by using SetCommState() fn.
> SetCommMask() is used for mask.
> After this when i call ReadFile() fn, the program hangs in ReadFile() fn.
> Anyone can give a skeleton code or reason for this.
>
> Thanks,
> Siva
>
oli at 2007-11-11 20:41:55 >
# 2 Re: COM Port Reading
How you assign the file handle to the COM1 itself, I checked your code written,
you just
state "COM1" in the filename parameter in the CreateFile fun() ?

Could you please explain how to distinguish between other ports also? I want
to open
handles for COM1 & COM2 and LPT at the same time.

Best Regards

Tarek

"oli" <evaleto@gkb.com> wrote:
> hComm = CreateFile((const char*) "COM1",GENERIC_READ |
>GENERIC_WRITE,0,0,OPEN_EXISTING,0,0);
>
> if(hComm== INVALID_HANDLE_VALUE)
>{
>... errrorr arrrrggsss,...
>}
>
> if(!GetCommState(hComm,&dcb))
> {
>... errrorr arrrrggsss,...
> }
>
> dcb.BaudRate = CBR_600; //hi,hi,hi
> dcb.ByteSize =(BYTE)bytes;//number of bits/byte, 4-8
> dcb.StopBits =(BYTE)stopbit;// 0,1,2 = 1, 1.5, 2
> dcb.Parity =(BYTE)parity;// 0-4=no,odd,even,mark,space
>
> /* set state */
> if(!SetCommState(hComm,&dcb))
> {
>... errrorr arrrrggsss,...
> }
>/*write*/
> if (!WriteFile(hComm, data, strlen(data), &dwWritten, NULL))
>{
>... errrorr arrrrggsss,...
>}
>
>/*read*/
> if (!WriteFile(hComm, datatoread, dwNumberToRead, &dwread, NULL))
>{
>... errrorr arrrrggsss,...
>}
>
> CloseHandle( hComm);
>/*I have not try but...*/
>regards,
>evaleto@gkb.com
>
>Siva <siva1010@hotmail.com> wrote in message news:38cdcd69@news.dev-archive.com...
>>
>> I have a problem in reading the COM1 port.
>> I opening the COM port by using CreateFile() fn.
>> Baud rate is set by using SetCommState() fn.
>> SetCommMask() is used for mask.
>> After this when i call ReadFile() fn, the program hangs in ReadFile()
fn.
>> Anyone can give a skeleton code or reason for this.
>>
>> Thanks,
>> Siva
>>
>
>
Tarek Eslim at 2007-11-11 20:43:02 >
# 3 Re: COM Port Reading
Use SetCommTimeouts to instruct Windows how to recoginze a pause in the
serial communication and return the control to your application before
filling the entire buffer passed to ReadFile function. See
http://msdn.microsoft.com/library/en-us/devio/base/time_outs.asp
--
Boris Karadjov
Brainbench MVP for Visual C++
http://www.brainbench.com

"Siva" <siva1010@hotmail.com> wrote in message
news:38cdcd69@news.dev-archive.com...
>
> I have a problem in reading the COM1 port.
> I opening the COM port by using CreateFile() fn.
> Baud rate is set by using SetCommState() fn.
> SetCommMask() is used for mask.
> After this when i call ReadFile() fn, the program hangs in ReadFile() fn.
> Anyone can give a skeleton code or reason for this.
>
> Thanks,
> Siva
>
Boris Karadjov at 2007-11-11 20:43:59 >