Interfacing C++ with COM ports

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jun 2006
Posts: 1,169
Reputation: Duki has a spectacular aura about Duki has a spectacular aura about Duki has a spectacular aura about 
Solved Threads: 9
Duki's Avatar
Duki Duki is offline Offline
Veteran Poster

Interfacing C++ with COM ports

 
0
  #1
Aug 20th, 2008
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!
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,378
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1466
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Interfacing C++ with COM ports

 
0
  #2
Aug 20th, 2008
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.
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 10
Reputation: sparty is an unknown quantity at this point 
Solved Threads: 0
sparty sparty is offline Offline
Newbie Poster

Re: Interfacing C++ with COM ports

 
0
  #3
Aug 20th, 2008
Do you need to talk to the comm port using Windows or Linux?
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 1,169
Reputation: Duki has a spectacular aura about Duki has a spectacular aura about Duki has a spectacular aura about 
Solved Threads: 9
Duki's Avatar
Duki Duki is offline Offline
Veteran Poster

Re: Interfacing C++ with COM ports

 
0
  #4
Aug 20th, 2008
Originally Posted by Ancient Dragon View Post
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?

Originally Posted by sparty View Post
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.
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,378
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1466
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Interfacing C++ with COM ports

 
0
  #5
Aug 21st, 2008
>>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.
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 1,169
Reputation: Duki has a spectacular aura about Duki has a spectacular aura about Duki has a spectacular aura about 
Solved Threads: 9
Duki's Avatar
Duki Duki is offline Offline
Veteran Poster

Re: Interfacing C++ with COM ports

 
0
  #6
Aug 21st, 2008
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...
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 10
Reputation: sparty is an unknown quantity at this point 
Solved Threads: 0
sparty sparty is offline Offline
Newbie Poster

Re: Interfacing C++ with COM ports

 
0
  #7
Aug 21st, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,378
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1466
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Interfacing C++ with COM ports

 
0
  #8
Aug 21st, 2008
Originally Posted by sparty View Post
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.

Originally Posted by sparty View Post
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.
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 10
Reputation: sparty is an unknown quantity at this point 
Solved Threads: 0
sparty sparty is offline Offline
Newbie Poster

Re: Interfacing C++ with COM ports

 
0
  #9
Aug 21st, 2008
Originally Posted by Ancient Dragon View Post
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,378
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1466
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Interfacing C++ with COM ports

 
0
  #10
Aug 21st, 2008
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC