ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hi everyone,
I have a device that I would like to write a simple C program to talk to it utilizing the USB-CDC library in Fedora 10 Linux OS. I am sure my device is recognized upon connection and I see it under dev/ttyACM0
As a supporting comment: the device when connected to a Windows XP OS gets enumerated as a USB/CDC Virtual COM port.
I am not sure where to start since I am not familiar with the CDC libraries and how to initialize their structures and start querying the device with commands.
I am interested in opening the connection, sending a simple identification command (IDN) and closing the connection.
Any code, or snippets of code along with explanation is greatly appreciated.
If the kernel has already loaded a driver for the device, you should be able to communicate with it by simply using the open()-read()-write()-close() plus ioctl() paradigm. It sounds like this is a basic character device acting like a serial port. If so, then you can probably exercise the device with minicom or C-Kermit.
--- rod.
I am sure my device is recognized upon connection and I see it under dev/ttyACM0
If the kernel drivers present your device as a member of the TTY family, you can use the termios family of functions to manipulate it and you can use fopen and friends or open and friends to read and write to it.
Read "man termios" to see how to set the BAUD rate and set important flags. You will probably want to use cfmakeraw() and modify your application specific flags from there.
Since your device is completely supported as a TTY device, you will not need to talk to the USB stack.
Thanks! I was able to connect to the device by just using it open() close().
I am seeing another problem now, which is probably a minor thing. The device receives commands that are terminated by \n (newline). If it can't find the \n characters, it returns an error that the line was not terminated properly. Is there some sort of a trick I need to do to get the characters \n to go through correctly? I can't seem to get the device to accept the \n. Basically, I need to send 0x0A at the end of each string in order for the device to parse the command and reply to it.
I added my code below, the this line -> iOut = write(fd, "IDN?\n"); is where I am having problems.
Serial ports in Unix/Linux are primarily intended to be used as line oriented terminal attachment points. As such, the drivers tend to take over the handling of carriage control characters, line terminators, etc. There are standard methods that allow you to override these settings, and the termios manpage details these.
There are also a few decent online references for serial port programming and general concepts: Serial Programming HOWTO Serial HOWTO Serial Programming Guide for POSIX Operating Systems
and of course the traditional manpages, in particular
man termios
The section on c_oflag is probably germane to your problem.
Another Linux or other smart terminal capable of displaying everything that you send it is very useful in diagnosing such problems. Since you are evidently attempting to control some type of test & measurement instrument, perhaps you have access to a digital storage scope, which can be used to monitor the send & receive lines of the serial interface. With a little effort, one can capture and interpret the serial bitstream to diagnose problems.
--- rod.
serial ports in unix/linux are primarily intended to be used as line oriented terminal attachment points. As such, the drivers tend to take over the handling of carriage control characters, line terminators, etc. There are standard methods that allow you to override these settings, and the termios manpage details these.
There are also a few decent online references for serial port programming and general concepts: serial programming howto serial howto serial programming guide for posix operating systems
and of course the traditional manpages, in particular
man termios
the section on c_oflag is probably germane to your problem.
Another linux or other smart terminal capable of displaying everything that you send it is very useful in diagnosing such problems. Since you are evidently attempting to control some type of test & measurement instrument, perhaps you have access to a digital storage scope, which can be used to monitor the send & receive lines of the serial interface. With a little effort, one can capture and interpret the serial bitstream to diagnose problems.
--- rod.
This thread inspired me to do some searching for some tools that are useful in tracking down problems such as yours. I located SerLook, and although it is a KDE-based application with a slightly clunky interface, it does seem to provide a useful set of views of the data. Just thought you'd like to know. I've used it to communicate with a single board ARM computer to do basic shell commands.
--- rod.
Hmm. I built mine from a tarball. Actually, I didn't originally get it from the link I posted, but I couldn't find my original source. After I built and installed it, I found a new 'Applications' folder in my KDE start menu, containing a callup for the tool. Also works just fine calling '/usr/bin/serlook' from a bash commandline.
--- rod.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.