USB - Serial Arduino C code lsusb ?? Please help (usb_set_configuration)
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.
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).
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?
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;
// 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);
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.