Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys! I have not been writing much, but have read many articles which have been put up here and would like to thank you all for always helping out.
My question is that how should I get a sensor connected to my serial port to communicate with the computer. As in, I have to continuously read the data from the sensor. I am a decent programmer comfortable with both C/C++ but am a complete noob when it comes to Serial Port programming.
Could you please help me by telling how I should proceed ??
p.s. I tried a serial port reading program I found on this site for the same but when the program writes some data to the sensor, the sensor returns some funny values.
Posted

Suggested reading for Win32 serial port programming is here[^].

If you had any problems running code from one of the articles on this site, it might be a good idea to post a question on the article forum. The author might be able to help you better if you explain your problem clearly to them.
 
Share this answer
 
Basic serial port communication is simple. Use CreateFile() to open the port, SetCommState() to configure it (baud rate, data size, stop bits, flow control, etc), and ReadFile() and WriteFile() for IO operations. See the above functions in the MSDN and Communications Functions[^].

But serial IO is usually asynchronous and transferring larger amounts of data with slow connections would block your program. Therefore, serial IO should be performed by worker threads using overlapped IO. Similar handling is used with network socket programming. So you might have some knowledge already.

I suggest to use the simple method first to ensure that the communication with your device is working. Then implement overlapped IO and worker threads if necessary. What and how to implement depends on your device (baud rate, amount of data, when data are send and received).
 
Share this answer
 
There are even a couple of good articles here on CodeProject, for example:
Win32 SDK Serail Comm Made Easy[^]
Just search for serial port. The code in the above article may be of help.
 
Share this answer
 
Thanks a lot everyone for replying so soon!

@parths - Yes sir, I shall definitely proceed as you say and read up + learn about Serial Port comm on my own, but I have to submit the program in a few days. So as you said, I shall also post my errors and query on the forum where I found that soln. Thnks.

@Jochen Arndt - Thanks a lot for the reply Sir. Unfortunately, I do not know too much socket programming, so I guess I shall take that up after I finish this project.

@nv - I just had a look at the aforementioned link. I see my path cut out for me. Thanks Sir.
 
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