PDA

View Full Version : [Question(s)] C++, C Serial or USB communications help



Eric
10-15-2008, 06:34 AM
Hey all,
I need the vast collective wisdom here to help guide me on the right path. I am looking to do some programming of sending text down a USB or serial rs-232 in Windows XP. I would normally do it in Java, but Java is really poor in such areas (and that goes for javax.comm packages). If you have another idea, let me know!
What I am thinking of doing is creating my own app with C or C++ and if I need Java as well, run that within JNI.
QUESTION 1: Does anyone know of any good books or sites that have good tutorials on C++ (win xp) that walk through the methods and pitfalls of port communication on windows? (most basic C++ books don't touch this)
QUESTION 2: Does anyone have any source code that is a simple, basic, cut-down version of serial or port communication?
(NOTE: assume previous knowldge of any of the programming languages listed)
THANK YOU!!!!!!!

wireframewolf
10-15-2008, 06:46 AM
http://todbot.com/blog/2006/12/06/arduino-serial-c-code-to-talk-to-arduino/

Try that. I have a tutorial in these threads that gets more into detail, but that's mostly all you need to know right there. Very simple and elegant C code for serial communication. The actual file is here: http://todbot.com/arduino/host/arduino-serial/arduino-serial.c

Two things that I would suggest however. One is that before testing the serialport_read_until() method, make a slight change to it. You'll see a one element array 'char b[1]'. Make sure that after you declare this array, you initialize the value of b[0] to zero... or any character that isn't your 'until' character. You'll know what I mean should you decide to look at the code :)

The second is that, if the program doesn't work, try changing the the parameters of the call to open() within the serialport_init() function. For some reason the original call worked 100% on Mac OS X, but not on my Ubuntu computer. I haven't tried it out on Windows, but it's all standard libs, so there's no -good- reason it should work with minimal tweaking.

jes1510
10-15-2008, 07:55 AM
If you are looking for something cross platform then the pySerial module for python works great. You can find it here:
http://pyserial.wiki.sourceforge.net/pySerial

To write a simple string to a serial port you just do this:


>>> import serial
>>> ser = serial.Serial(0) #open first serial port
>>> ser.write("hello") #write a string
>>> ser.close() #close port


The module handles all of the heavy lifting for you. Anything received on the port can be treated like a string.

Adrenalynn
10-15-2008, 12:02 PM
The call I make to the serial port in my tiny little SSC-32 demo in C# is the same as you'd make in C++ under VCC/DevStudio. Standard classes.

Eric
10-15-2008, 01:38 PM
Sweet!!!! Thanks guys!!! I am going to give wireframewolf's and Adrenalynn's answer a try. I see Adrenalynn has exactly the bare-bones code that I am looking for! Brilliant! :veryhappy:

Adrenalynn
10-15-2008, 02:05 PM
That's in C#, the call is the same, but the code won't just cut-and-paste over to C++.