Click here to Skip to main content
15,906,625 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i was trying to read a serial port data by using windows api but somehow i could not able to read the serial port properly.. would anyone kindly please help. thanks

this is my code for the reading of the serial port
C#
//***************Read Operation******************//
buffRead = 0;
dwBytesRead = 0;
int nread =20;

if (!ReadFile(hSerial, buffRead, nread, &dwBytesRead, NULL))
{
printf("error reading from input buffer \n");
}
printf("Data read from read buffer is \n %s \n",buffRead);


thanks

kevin
Posted
Comments
Nish Nishant 4-Nov-10 12:40pm    
What error do you get? Do you see the inner printf being called?

1 solution

One very obvious error there is that you are passing NULL as your lpBuffer argument.
 
Share this answer
 
Comments
kevinang 4-Nov-10 12:50pm    
oh okie.. so i put buffRead=0 right? it will able to read my serial port
Nish Nishant 4-Nov-10 12:52pm    
You need to initialize that to a buffer that's at least as long as nread (in bytes).
kevinang 4-Nov-10 12:58pm    
hmmm.. do you have any exmaples for me to see? i m quite new to this api programming.. thanks
Nish Nishant 4-Nov-10 12:59pm    
byte buffRead[32];

That should do it.
kevinang 4-Nov-10 13:02pm    
Oh thanks.. Nishant I have one more question is my int nread=20 apporiated to use?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900