PDA

View Full Version : C++ Serial Communication



Resilient
02-01-2009, 07:50 PM
So, I have a somewhat complete interface for communicating with my Roomba in python, but I want to write it in C++ because that's what all my CS classes are in, so I would like to work on it as I learn new C++ concepts.

I am a little puzzled on how to get started though.

Here is what I got in python. How do I do this in C++?




def connect(self, port, baud=115200):
self.ser = serial.Serial(port-1, baud, timeout=1)
self.ser.setRTS(0)
self.ser.flushOutput()



In python I just use the pySerial module and poof its connected with that code.

Do I need an additional library to do serial communication in C++? Or is there some built in way of doing serial comm?

Thanks
J

lnxfergy
02-01-2009, 08:02 PM
There is a reason people use python -- somebody did all the hard work for you in creating pySerial.

-Fergs

Resilient
02-01-2009, 08:05 PM
Yeah, I am just hoping someone did all the hard work in C++ too. I just cant find it :P

jes1510
02-01-2009, 08:53 PM
I believe you have to treat the serial port as a file object. I've never done this on a computer though.

[edit] maybe this will help:
http://www.codeproject.com/KB/system/serial.aspx

Adrenalynn
02-01-2009, 09:31 PM
Just to be clear:

C++ doesn't do serial - ANSI doesn't define a standard method of getting to the ports. Libraries do serial. So - what C++ are we talking about here? It's going to be different (sometimes wildly different) depending upon what compiler/environment/libraries you're using.

Resilient
02-01-2009, 10:48 PM
In the ideal world, it works on Windows and my Acer Aspire One net book running Ubuntu.

In reality, I mainly want it to work on Windows XP with Visual Studio 2008. If I need a lot more than that to get started, I suppose Ill just stick to python for the time being.

Adrenalynn
02-02-2009, 03:00 AM
Of course, it's possible to get it to compile under both - just a whole lot of ifdef's. ;)

Tomorrow night (if no one beats me to it), I'll knock out some MS VS C++ for you for that task.

Resilient
02-02-2009, 12:04 PM
Thanks a bunch! I would usually just wait till we learn it in class, but I am not sure we ever well. All the electrical engineering kids get to learn it, but not the CS kids. :(

Adrenalynn
02-02-2009, 12:13 PM
Before I get all into this - have you looked at this: http://www.codeguru.com/cpp/i-n/network/serialcommunications/article.php/c2503

Resilient
02-02-2009, 12:28 PM
I forgot about that. I looked at that awhile ago, and it seemed like it should fit the bill but it wouldn't compile.

The compiler had a bunch of problems with the decelerations that were in all caps like BOOL and DWORD.

I tried to look up what those meant and that lead me to the Microsoft development site where I promptly got very lost.

Adrenalynn
02-02-2009, 12:33 PM
Oh ouch. Yeah, I suggest learning the language rather than learning serial programming. Come back to it when you've got the very most basics down. If you're not familiar with a BOOLean, you're not going to get very far anyway.

Resilient
02-02-2009, 12:51 PM
No no, I am familiar with what a Boolean is.

But when I declare a function returning one it's something like


bool Found(str findme);Those files all have decelerations like:


BOOL Close( void ); and when I try to compile it doesn't recognize those as decorations.

VS doesn't like the all caps and I am not sure what the difference between 'bool' and 'BOOL' is.

Resilient
02-03-2009, 01:04 PM
After doing some more research, it looks like the upper case BOOL and all those other data types are from windows.h so I included that and all those nasties went away.

Now its just missing stdafx.h. Where does this header file come from best I can tell VS is supposed to create it?

Thanks
J

Adrenalynn
02-03-2009, 01:22 PM
When you create a new project that isn't a console application, it should be created by the IDE. windows.h should have also been included in a non-console-app. You are using the Wizard to create a project first, right?

That said, just whack the stdafx include out. It's just MS' way of precompiling headers. Remove it from the code and you should compile fine.

HakBot
02-03-2009, 03:23 PM
Was this solved? Would the OP mind using C++ .NET? If so creating a serial port connection is cake. Its a standard item listed in the tool box such as a button is. You can manipulate the variables in the settings window. Very easy with .NET (although Im sure Ill get flack for that).