LinuxQuestions.org
Visit Jeremy's Blog.
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 09-25-2012, 03:17 PM   #1
PeterUK
Member
 
Registered: May 2009
Posts: 281

Rep: Reputation: 16
USB - Serial Arduino C code lsusb ?? Please help (usb_set_configuration)


Hi!

I am trying to read data sent to USB link using an Arduino.

I have download the firmware and it work on the IDE development environment of Arduino.

The first problem I have now is about usb_set_configuration I get error the value is returned -16

Now I connect the device and do lsusb and I get:

Code:
Bus 006 Device 024: ID 2341:0001
I do lsusb -v -d 2341:0001

and I get:
Quote:
Bus 006 Device 024: ID 2341:0001
Device Descriptor:
bLength 18
bDescriptorType 1
bcdUSB 1.10
bDeviceClass 2 Communications
bDeviceSubClass 0
bDeviceProtocol 0
bMaxPacketSize0 8
idVendor 0x2341
idProduct 0x0001
bcdDevice 0.01
iManufacturer 1 Arduino (www.arduino.cc)
iProduct 2 Arduino Uno
iSerial 220 74136333033351E07161
bNumConfigurations 1
Configuration Descriptor:
bLength 9
bDescriptorType 2
wTotalLength 62
bNumInterfaces 2
bConfigurationValue 1
iConfiguration 0
bmAttributes 0xc0
Self Powered
MaxPower 100mA
Interface Descriptor:
bLength 9
bDescriptorType 4
bInterfaceNumber 0
bAlternateSetting 0
bNumEndpoints 1
bInterfaceClass 2 Communications
bInterfaceSubClass 2 Abstract (modem)
bInterfaceProtocol 1 AT-commands (v.25ter)
iInterface 0
CDC Header:
bcdCDC 10.01
CDC ACM:
bmCapabilities 0x06
sends break
line coding and serial state
CDC Union:
bMasterInterface 0
bSlaveInterface 1
Endpoint Descriptor:
bLength 7
bDescriptorType 5
bEndpointAddress 0x82 EP 2 IN
bmAttributes 3
Transfer Type Interrupt
Synch Type None
Usage Type Data
wMaxPacketSize 0x0008 1x 8 bytes
bInterval 255
Interface Descriptor:
bLength 9
bDescriptorType 4
bInterfaceNumber 1
bAlternateSetting 0
bNumEndpoints 2
bInterfaceClass 10 CDC Data
bInterfaceSubClass 0 Unused
bInterfaceProtocol 0
iInterface 0
Endpoint Descriptor:
bLength 7
bDescriptorType 5
bEndpointAddress 0x04 EP 4 OUT
bmAttributes 2
Transfer Type Bulk
Synch Type None
Usage Type Data
wMaxPacketSize 0x0040 1x 64 bytes
bInterval 1
Endpoint Descriptor:
bLength 7
bDescriptorType 5
bEndpointAddress 0x83 EP 3 IN
bmAttributes 2
Transfer Type Bulk
Synch Type None
Usage Type Data
wMaxPacketSize 0x0040 1x 64 bytes
bInterval 1
Device Status: 0x0000
(Bus Powered)

I also have added the Usbdev to rules.d as:

Quote:
SUBSYSTEM=="usb", ACTION=="add", ATTRS{idVendor}=="2341", ATTRS{idProduct}=="0001", DRIVER=="usbtest", RUN+="/bin/sh -c 'echo -n %k> %S%p/driver/unbind'"
On my C code the first problem is about:
Quote:
n_open = usb_set_configuration(usbhandle,2);
I get is fail the value returned is -16 what may I doing wrong?
 
Old 09-25-2012, 05:55 PM   #2
PeterUK
Member
 
Registered: May 2009
Posts: 281

Original Poster
Rep: Reputation: 16
A update here I know that 2 in the int config is wrong according to some page I went the bConfigurationValue is 1

as you can see on the lsusb.

But this still returning -16
 
Old 09-25-2012, 07:33 PM   #3
PeterUK
Member
 
Registered: May 2009
Posts: 281

Original Poster
Rep: Reputation: 16
Other thing if I do cat and the device I can see the serial data coming...
 
Old 09-25-2012, 11:47 PM   #4
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
Hi.

Look here for a description of how to communicate with this board without usb-serial driver (like cdc_acm). Because you have a device file in /dev, you probably have corresponding module (cdc_acm ?) loaded, which blocks your usb device (it is already used by the module). So try to remove (unload) that module:
Code:
sudo rmmod cdc_acm
You may need to discover which module is actually used: look at dmesg, lsmod etc.

On the other hand I'd advise you to just use standard driver, i.e. to communicate via /dev/ttyACM0 (or whatever name it is given).

Last edited by firstfire; 09-25-2012 at 11:48 PM.
 
Old 09-26-2012, 04:12 AM   #5
PeterUK
Member
 
Registered: May 2009
Posts: 281

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by firstfire View Post
Hi.

Look here for a description of how to communicate with this board without usb-serial driver (like cdc_acm). Because you have a device file in /dev, you probably have corresponding module (cdc_acm ?) loaded, which blocks your usb device (it is already used by the module). So try to remove (unload) that module:
Code:
sudo rmmod cdc_acm
You may need to discover which module is actually used: look at dmesg, lsmod etc.

On the other hand I'd advise you to just use standard driver, i.e. to communicate via /dev/ttyACM0 (or whatever name it is given).
Thanks for this, I went to bed thinking that was the problem like something is already opening that port.

I got a reply form another forum and now I have some code for serial comm as you suggested

Nevertheless today when I do cat to the dev its not hanging on it, after reading in close the cat do you know why?
 
Old 09-26-2012, 04:37 AM   #6
PeterUK
Member
 
Registered: May 2009
Posts: 281

Original Poster
Rep: Reputation: 16
That is very strange I reboot the PC and cat work fine it hang after executing and every time I sent data to USB_Serial (push button) I get it on the terminal but after running the C code to read the Serial port it stop hanging it close cat command on the terminal. Do you know why?

Here is the c code I am running.

Quote:
/*
* Arduino-serial
* --------------
*
* A simple command-line example program showing how a computer can
* communicate with an Arduino board. Works on any POSIX system (Mac/Unix/PC)
*
*
* Compile with something like:
* gcc -o arduino-serial arduino-serial.c
*
* Created 5 December 2006
* Copyleft (c) 2006, Tod E. Kurt, tod@todbot.com
* http://todbot.com/blog/
*
*
* Updated 8 December 2006:
* Justin McBride discoevered B14400 & B28800 aren't in Linux's termios.h.
* I've included his patch, but commented out for now. One really needs a
* real make system when doing cross-platform C and I wanted to avoid that
* for this little program. Those baudrates aren't used much anyway.
*
* Updated 26 December 2007:
* Added ability to specify a delay (so you can wait for Arduino Diecimila)
* Added ability to send a binary byte number
*
* Update 31 August 2008:
* Added patch to clean up odd baudrates from Andy at hexapodia.org
*
*/

#include <stdio.h> /* Standard input/output definitions */
#include <stdlib.h>
#include <stdint.h> /* Standard types */
#include <string.h> /* String function definitions */
#include <unistd.h> /* UNIX standard function definitions */
#include <fcntl.h> /* File control definitions */
#include <errno.h> /* Error number definitions */
#include <termios.h> /* POSIX terminal control definitions */
#include <sys/ioctl.h>
#include <getopt.h>

void usage(void);
int serialport_init(const char* serialport, int baud);
int serialport_writebyte(int fd, uint8_t b);
int serialport_write(int fd, const char* str);
int serialport_read_until(int fd, char* buf, char until);

void usage(void) {
printf("Usage: arduino-serial -p <serialport> [OPTIONS]\n"
"\n"
"Options:\n"
" -h, --help Print this help message\n"
" -p, --port=serialport Serial port Arduino is on\n"
" -b, --baud=baudrate Baudrate (bps) of Arduino\n"
" -s, --send=data Send data to Arduino\n"
" -r, --receive Receive data from Arduino & print it out\n"
" -n --num=num Send a number as a single byte\n"
" -d --delay=millis Delay for specified milliseconds\n"
"\n"
"Note: Order is important. Set '-b' before doing '-p'. \n"
" Used to make series of actions: '-d 2000 -s hello -d 100 -r' \n"
" means 'wait 2secs, send 'hello', wait 100msec, get reply'\n"
"\n");
}

int main(int argc, char *argv[])
{
int fd = 0;
char serialport[256];
int baudrate = B9600; // default
char buf[256];
int rc,n;

if (argc==1) {
usage();
exit(EXIT_SUCCESS);
}

/* parse options */
int option_index = 0, opt;
static struct option loptions[] = {
{"help", no_argument, 0, 'h'},
{"port", required_argument, 0, 'p'},
{"baud", required_argument, 0, 'b'},
{"send", required_argument, 0, 's'},
{"receive", no_argument, 0, 'r'},
{"num", required_argument, 0, 'n'},
{"delay", required_argument, 0, 'd'}
};

while(1) {
opt = getopt_long (argc, argv, "hp:b:s:rn:d:",
loptions, &option_index);
if (opt==-1) break;
switch (opt) {
case '0': break;
case 'd':
n = strtol(optarg,NULL,10);
usleep(n * 1000 ); // sleep milliseconds
break;
case 'h':
usage();
break;
case 'b':
baudrate = strtol(optarg,NULL,10);
break;
case 'p':
strcpy(serialport,optarg);
fd = serialport_init(optarg, baudrate);
if(fd==-1) return -1;
break;
case 'n':
n = strtol(optarg, NULL, 10); // convert string to number
rc = serialport_writebyte(fd, (uint8_t)n);
if(rc==-1) return -1;
break;
case 's':
strcpy(buf,optarg);
rc = serialport_write(fd, buf);
if(rc==-1) return -1;
break;
case 'r':
serialport_read_until(fd, buf, '\n');
printf("read: %s\n",buf);
break;
}
}

exit(EXIT_SUCCESS);
} // end main

int serialport_writebyte( int fd, uint8_t b)
{
int n = write(fd,&b,1);
if( n!=1)
return -1;
return 0;
}

int serialport_write(int fd, const char* str)
{
int len = strlen(str);
int n = write(fd, str, len);
if( n!=len )
return -1;
return 0;
}

int serialport_read_until(int fd, char* buf, char until)
{
char b[1];
int i=0;
do {
int n = read(fd, b, 1); // read a char at a time
if( n==-1) return -1; // couldn't read
if( n==0 ) {
usleep( 10 * 1000 ); // wait 10 msec try again
continue;
}
buf[i] = b[0]; i++;
} while( b[0] != until );

buf[i] = 0; // null terminate the string
return 0;
}

// takes the string name of the serial port (e.g. "/dev/tty.usbserial","COM1")
// and a baud rate (bps) and connects to that port at that speed and 8N1.
// opens the port in fully raw mode so you can send binary data.
// returns valid fd, or -1 on error
int serialport_init(const char* serialport, int baud)
{
struct termios toptions;
int fd;

//fprintf(stderr,"init_serialport: opening port %s @ %d bps\n",
// serialport,baud);

fd = open(serialport, O_RDWR | O_NOCTTY | O_NDELAY);
if (fd == -1) {
perror("init_serialport: Unable to open port ");
return -1;
}

if (tcgetattr(fd, &toptions) < 0) {
perror("init_serialport: Couldn't get term attributes");
return -1;
}
speed_t brate = baud; // let you override switch below if needed
switch(baud) {
case 4800: brate=B4800; break;
case 9600: brate=B9600; break;
#ifdef B14400
case 14400: brate=B14400; break;
#endif
case 19200: brate=B19200; break;
#ifdef B28800
case 28800: brate=B28800; break;
#endif
case 38400: brate=B38400; break;
case 57600: brate=B57600; break;
case 115200: brate=B115200; break;
}
cfsetispeed(&toptions, brate);
cfsetospeed(&toptions, brate);

// 8N1
toptions.c_cflag &= ~PARENB;
toptions.c_cflag &= ~CSTOPB;
toptions.c_cflag &= ~CSIZE;
toptions.c_cflag |= CS8;
// no flow control
toptions.c_cflag &= ~CRTSCTS;

toptions.c_cflag |= CREAD | CLOCAL; // turn on READ & ignore ctrl lines
toptions.c_iflag &= ~(IXON | IXOFF | IXANY); // turn off s/w flow ctrl

toptions.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); // make raw
toptions.c_oflag &= ~OPOST; // make raw

// see: http://unixwiz.net/techtips/termios-vmin-vtime.html
toptions.c_cc[VMIN] = 0;
toptions.c_cc[VTIME] = 20;

if( tcsetattr(fd, TCSANOW, &toptions) < 0) {
perror("init_serialport: Couldn't set term attributes");
return -1;
}

return fd;
}
 
  


Reply



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
[SOLVED] Arduino MEGA 2560 REV3, Arduino IDE working software versions jamesf Slackware 1 05-02-2012 12:56 AM
Can't access usb device with libusb, crashes at usb_set_configuration() zwiebelchen Programming 0 02-08-2011 09:23 AM
USB-Scanner not detected with lsusb PGPGPG Linux - Hardware 0 09-09-2006 11:29 AM
USB Device listing in lsusb Jayaraghavendran Linux - Hardware 7 06-30-2006 02:52 AM
serial to USB, trying to read from /dev/ttyUSB0 in C code newguy21 Programming 1 09-29-2004 01:11 PM

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

All times are GMT -5. The time now is 05:13 AM.

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