Hey everyone,

A friend and I are trying to figure out how to interface C++ 2008 with COM ports for our robotics team. Is there a simple "template" we could go by, or is it different for every device? Could someone provide an example of how to transfer data to COM ports via C++?

For example: The robot operates off of GPS, which is connected to the computer system via the "ninth" COM port at 19200 baud. How can we transfer information to and from the GPS device? If you need more information, let me know.

Thanks for any help!

MS-Windows Communications Reference here. I think it even has examples.

>>which is connected to the computer system via the "ninth" COM port
Does that mean the computer has a COM port expansion board installed, there are several 8, 16 and 32 port boards and each has a device driver so that your program can reference the ports as COM1, COM2, COM3, ... COM32. Just use the port number in the CreateFile() statement (see the examples previous linked).

>>How can we transfer information to and from the GPS device
I have no idea about how to communicate with the GPS device -- you will have to read the manufacturer's programmer's reference documentation for that. But you set up the com port's parameters via the functions/examples previously described.

Do you need to talk to the comm port using Windows or Linux?

MS-Windows Communications Reference here. I think it even has examples.

>>which is connected to the computer system via the "ninth" COM port
Does that mean the computer has a COM port expansion board installed, there are several 8, 16 and 32 port boards and each has a device driver so that your program can reference the ports as COM1, COM2, COM3, ... COM32. Just use the port number in the CreateFile() statement (see the examples previous linked).

>>How can we transfer information to and from the GPS device
I have no idea about how to communicate with the GPS device -- you will have to read the manufacturer's programmer's reference documentation for that. But you set up the com port's parameters via the functions/examples previously described.

Thank you!! I'm completely new to interfacing with anything other than the cmd line. Currently there are about 150 pages of VB6 code that control the robot; my friend and I are converting everything to C++ (2008), which should have been done a few years ago.

Again, thanks a bunch for your help!! Do you have much experience with interfacing with devices other than the cmd line?

Do you need to talk to the comm port using Windows or Linux?

It's currently Windows XP that runs the bot... we (my friend and I) suggested a transfer to Linux, which will be done as soon as we convert the VB6 code.

>>Do you have much experience with interfacing with devices other than the cmd line?
I have written programs to talk to robots on factory assembly lines, some via PLC (program logic controller) attached to RS232 serial port while others attached directly to the com port. Each one was unique and required manufacturer's programmer's book to get the set of commands and how to interface with it. But that was about 12 years ago, so my knowledge has slipped quite a bit.

MS-Windows is NOT a real-time operating system, so don't expect real-time instant communications through the com ports. When dealing with external hardware the computer needs to be dedicated solely to that application -- no one should run any other applications, such as browswer, windows word, or other non-related programs.

You will want to create a listening thread(s) to do nothing more than listen for incoming data. You install that thread function when you set up the communications port so that the Microsoft com driver can call your listening function as data arrives at the port. If you don't do this then your computer is likely to lose data; poling for the data is not sufficient because UARTs can only hold about 16 bytes.

Wow, thanks for the advice!
How do you create listening threads? Is that in the code?

I'm so glad to find someone that has this type of experience! I'm sure you and I will be talking a lot over the next couple of months :)

If that's ok with you of course...

Talking to the comm port in Windows you should use the winsock.h library.

In linux the procedure for writing to the port is exactly the same as writing to a file.

Steps:

open() //the first argument is filename, but when talking to the port you substitute the filename with the path to the port most likely found in the /dev/ directory... the full path depends on the number of ports, but ttys0 is pretty typical. you also have to have the permission of that port... so run as root or chmod it.

read()/write() //use the created filehandle from the open function and read/write to it

close() //close the opened filehandle

Talking to the comm port in Windows you should use the winsock.h library.

Nope. winsock library is for socket programming, not RS232 serial port programming.

In linux the procedure for writing to the port is exactly the same as writing to a file.

Similar for win32. CreateFile() to open the comm port, then ReadFile() and WriteFile() to read/write to it.

Nope. winsock library is for socket programming, not RS232 serial port programming.


Similar for win32. CreateFile() to open the comm port, then ReadFile() and WriteFile() to read/write to it.

oops.... my fault i meant it's in windows.h

Ah ok, that's kind of similar to the VB code. Here's what we have right now:

commLMS.PortOpen = True
commGPS.PortOpen = True

Then later within the same code it communicates through a function called

CalOutput()

That code looks like this:

Dim Vout As Integer
Dim Aout As Integer
Dim Vchr As String
Dim Achr As String

Vout = Int(((127 / 100) * V) )
Aout = Int(128 + (127 / 180) * (180 - A))
Vchr = Chr(Vout)
Achr = Chr(Aout)
commBobbySue.Output = Vchr
commBobbySue.Output = Achr

commBobbySue is the PIC controller.
V is velocity
A is angle

Hope that makes sense
Is this similar to what it would look like in C++? Not the declarations, just the port openings.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.