| | |
Interfacing C++ with COM ports
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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!
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!
It is practically impossible to teach good programming style to students that have had prior exposure to Basic; as potential programmers they are mentally mutilated beyond hope of regeneration.
-Edsger Dijkstra
-Edsger Dijkstra
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.
>>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.
Last edited by Ancient Dragon; Aug 20th, 2008 at 6:57 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
•
•
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.
Again, thanks a bunch for your help!! Do you have much experience with interfacing with devices other than the cmd line?
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.
It is practically impossible to teach good programming style to students that have had prior exposure to Basic; as potential programmers they are mentally mutilated beyond hope of regeneration.
-Edsger Dijkstra
-Edsger Dijkstra
>>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.
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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
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...
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...
It is practically impossible to teach good programming style to students that have had prior exposure to Basic; as potential programmers they are mentally mutilated beyond hope of regeneration.
-Edsger Dijkstra
-Edsger Dijkstra
•
•
Join Date: Aug 2008
Posts: 10
Reputation:
Solved Threads: 0
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
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.
Similar for win32. CreateFile() to open the comm port, then ReadFile() and WriteFile() to read/write to it.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Also, check out this c++ class
Last edited by Ancient Dragon; Aug 21st, 2008 at 2:52 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
![]() |
Similar Threads
- Send data on a serial port (C++)
- how to control a buttons with parallel port interfacing (VB.NET)
- parallel port interfacing with switch input (C)
- C++ Real world interfaces? (C++)
- usb interfacing through vb6.0 (Visual Basic 4 / 5 / 6)
- Interfacing USB POrts from VB5 (Visual Basic 4 / 5 / 6)
Other Threads in the C++ Forum
- Previous Thread: Creating a window
- Next Thread: replacing windows functions with ubuntu linux
| Thread Tools | Search this Thread |
api array beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion count data database delete desktop developer directshow dll download dynamic email encryption error file forms fstream function functions game getline google graph gui homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text text-file tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






