Click here to Skip to main content
15,890,579 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am reading data from a device at comm port using ReadFile() function in C Programming and i saved it into array, Data i received is in chunks now i want to break it into lines or read it line by line, kindly help me in code to read data line by line or is there any solution like first save data into an array then read array line by line..

What I have tried:

char data[50];
ReadFile(hComm, &data, sizeof(data), &noofbytesread,NULL);
printf("%s", data);
Posted
Updated 12-Jan-21 7:51am
Comments
Richard MacCutchan 12-Jan-21 9:08am    
There is no concept of lines in data coming from a comm port, it is just a stream of bytes. You need to format it yourself as you read it.

As Richard says, a stream has no concept of lines.

So create yourself a buffer, and as you read new data add it to the buffer until you reach a newline. Process the line of data, and then clear the buffer. Continue adding data until the data you read runs out, and then read more from your stream.
All you have to do is make sure the buffer you create will hold the largest "line" the device can generate, and you're good to go.
 
Share this answer
 
My pre-posters are absolutly right and you need to understand the principles of COM communication. Take a look at this C++ COM article to understand the principles better or read another COM Q&A answer.
 
Share this answer
 

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