Writing a simple program to use a COM port in C++

MrDudeMan

Lifer
Jan 15, 2001
15,065
90
91
I searched before I posted and only found 1 thread with no answers.

I am trying to write a C++ application to control a circuit I designed over a COM port. Actually, I would very much prefer to use USB but I don't know how to use the USB functions provided in the DLL for the IC I designed into my circuit, so I am just using it as a VCP. Anyway, I completely understand how it is supposed to work so theory isn't my problem here.

I've been searching google for hours and I've found basically nothing helpful. I know I can do this using write/read directly but I want to do it the "right" way. I found some code earlier today that I scrutinized in order to understand it and it talked about creating a process for reading and writing so neither blocks the other. That makes sense but I don't know how to write these kinds of programs in Windows. The Windows libraries confuse me.

Here are the facts about my circuit:

Right now I simply have the UART RX interrupt sending every value it gets back to the sender immediately as a way to "loopback" and test the communication. I've done this a billion times and I knew it would work ahead of time, but I don't know how to get that into something usable in C++.

The circuit is measuring acceleration in 3 dimensions 20 times per second and needs to send these values to a PC where they will be stored in a file that can be imported into Excel. I will also need to send commands to it based on the values it is sending me. I know none of this is very complicated, but like I said, my problem is what to actually write in my program to make this happen. I want a clean solution (80% to learn, 20% to solve this problem) and everything I've found on google has been either way too complicated for a beginner or simply doesn't work (i.e. deprecated headers are needed, written for linux, etc.).

If someone is willing to help me with this I would really appreciate it. Here is basically what I want the program to be able to do (I'm not asking someone to write the whole thing for me - just to show me where to start and give me some guidance):

-send a command to start measuring accelerations
-receive 60 data points per second and write them to a file until i type 'q' for quit
-while it is receiving data points, send commands typed by the user to change settings in the MCU


I would prefer being able to set the BAUD and format but if I simply have to use the pre-defined settings in Device Manager -> Ports then so be it.

Edit: To clarify the rate at which data points will be received - this has nothing to do with the computer. I'm saying the MCU will take a reading of each axis every 50mS, so the data will really be sent to the computer every 50mS. I know these will be read into a buffer by the PC.

Edit 2: I found this webpage explaining how to do it, but this is exactly what I was talking about before. I read every word of that webpage and I understand what it is talking about, but I don't get how to do it in C++. I'm not familiar enough with multithreading applications to actually write one or even how to get it to compile with the right headers and libraries. That is what I need help with.
 

QuixoticOne

Golden Member
Nov 4, 2005
1,855
0
0
Download the VisualStudio Express Edition ISO CD image "Offline Install" option:
http://www.microsoft.com/express/download/
..burn the disc.

It'll give you basic help that'll have code examples and documents for the APIs you need.

It'll give you C++ and C# options... I'd suggest just using C# instead of C++ since the API semantics are easier for COM port stuff in C# than C++, though no big deal either way.

If you use C++ you have to choose if you're going to use the managed CLR runtime or use straight Win32/MFC level stuff. If you'll ultimately be calling unmanaged DLL functions for USB stuff from some DLL just stick with C++ unmanaged coding.

Look at the Win32 level and MFC level serial communications APIs.. You probably don't want to write to Win32 API directly though. Use MFC or a third party library / sample code project for serial I/O for Win32 or MFC or whatever.

If you want to have basically no control over the port settings you can just write a simple console application that does fopen access to "COM1:" or whatever and use fputc fgetc or whatever but I'd suggest using something that uses the Win32 API so you have some direct control over the settings.

http://www.codeproject.com/KB/...em/SerialPortComm.aspx
etc.