LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-06-2009, 10:51 AM   #1
v333k
Member
 
Registered: Apr 2004
Location: 127.0.0.1
Posts: 38

Rep: Reputation: 15
talk to device on ttyACM0 using C programming


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.

thanks.
 
Old 04-06-2009, 11:56 AM   #2
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
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.
 
Old 04-06-2009, 12:50 PM   #3
David1357
Senior Member
 
Registered: Aug 2007
Location: South Carolina, U.S.A.
Distribution: Ubuntu, Fedora Core, Red Hat, SUSE, Gentoo, DSL, coLinux, uClinux
Posts: 1,302
Blog Entries: 1

Rep: Reputation: 107Reputation: 107
Quote:
Originally Posted by v333k View Post
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.
 
Old 04-06-2009, 03:21 PM   #4
v333k
Member
 
Registered: Apr 2004
Location: 127.0.0.1
Posts: 38

Original Poster
Rep: Reputation: 15
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.

#include "stdio.h"
#include "string.h"
#include <fcntl.h>
#include <unistd.h>

int main()
{
char *sComPort = "/dev/ttyACM0";
char reply[10];

int iOut, fd; // file descriptor

fd = open(sCpomPort, O_RDWR | O_NOCTTY | O_NONBLOCK);

if(fd>0)
{
printf("Open successfully\n");

iOut = write(fd, "IDN?\n", 5);
read(rd, reply, 10);
printf("%s\n", reply);

close(fd);
}
else
{
printf("failed to open device\n");
}

return 0;
}

Last edited by v333k; 04-06-2009 at 03:25 PM.
 
Old 04-06-2009, 04:21 PM   #5
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
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.

Last edited by theNbomr; 04-06-2009 at 04:31 PM.
 
Old 04-06-2009, 06:22 PM   #6
v333k
Member
 
Registered: Apr 2004
Location: 127.0.0.1
Posts: 38

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by thenbomr View Post
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.

thanks ------- exactly what i was looking for.
 
Old 04-07-2009, 09:04 AM   #7
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
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.

Last edited by theNbomr; 04-07-2009 at 09:05 AM.
 
Old 04-07-2009, 10:44 AM   #8
v333k
Member
 
Registered: Apr 2004
Location: 127.0.0.1
Posts: 38

Original Poster
Rep: Reputation: 15
thanks theNbomr - I tried installing this application by doing
rpm --rebuilddb serlook-0.3.1-0.src.rpm

It looks like it is successful, but I don't know what to do next... maybe that is because I didn't start with the right step.

I am interested in getting this app running just to become a tad bit more familiar with Fedora 10.

Thanks
 
Old 04-07-2009, 10:51 AM   #9
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
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.

EDIT: Found the link I used: http://www.ibiblio.org/pub/Linux/sys...al/!INDEX.html

Last edited by theNbomr; 04-07-2009 at 11:00 AM.
 
Old 04-07-2009, 12:05 PM   #10
v333k
Member
 
Registered: Apr 2004
Location: 127.0.0.1
Posts: 38

Original Poster
Rep: Reputation: 15
Thanks again.
I got the tarball file from the link you sent, but I need QT to move on. I will install QT first and then I will install it.

Thanks!
 
  


Reply

Tags
ttyacm0, usb



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
missing /dev/ttyACM0 , modprobe cdc-acm agentchange Linux - Software 5 01-25-2010 11:46 PM
device mapper / multipath creating extra device, won't let me talk to the one i want chakkerz Linux - Server 1 03-16-2008 05:52 PM
How do I create a /dev/ttyACM0? Andriy Slackware 5 06-29-2006 05:11 AM
/dev/ttyACM0 ankscorek Slackware 3 02-27-2005 01:14 AM
Any books that talk about Linux network programming? wirelessman Programming 2 05-01-2002 05:52 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:43 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration