469,283 Members | 2,303 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,283 developers. It's quick & easy.

Need help with USB programming in Linux

Hi
I am trying to write a file to a USB device using C program..
When i try to open the USB port, i find the foll error

"open_port: unable to open usbdev2.1_ep81: No such file or directory"

The command to open the USB port:
Expand|Select|Wrap|Line Numbers
  1. sprintf(portfile,"/dev/usbdev2.1_ep81");
  2. if((fd=open(portfile, O_RDWR | O_NOCTTY | O_NDELAY))==-1)
  3. perror("open_port: unable to open usbdev2.1_ep81");
  4.  
Please provide some inputs/help

Thanks,
Priya
Sep 6 '07 #1
18 4001
sicarie
4,677 Expert Mod 4TB
Please read your PM's, accessible via the link in the top right corner of the page.

You are trying to access the device from /dev/, which means you have not mounted it. In Linux the device needs to be mounted before it can be accessed. What flavor (distribution) are you running? I know Debian will automount and put it in /media/usbdisk/ so you might try looking around your / or /mnt directory for a 'media' or 'usb' folder.
Sep 6 '07 #2
Please read your PM's, accessible via the link in the top right corner of the page.

You are trying to access the device from /dev/, which means you have not mounted it. In Linux the device needs to be mounted before it can be accessed. What flavor (distribution) are you running? I know Debian will automount and put it in /media/usbdisk/ so you might try looking around your / or /mnt directory for a 'media' or 'usb' folder.
Hi
I am using Debian flavor.
What i m trying to do is use a C program to access USB device in the same way as the COM port are accessed in Serial Communication.
Thanks
Sep 6 '07 #3
sicarie
4,677 Expert Mod 4TB
Hi
I am using Debian flavor.
What i m trying to do is use a C program to access USB device in the same way as the COM port are accessed in Serial Communication.
Thanks
Just because it is a different device will mean that you will not be able to access it the same way you would access a serial communication. I mean, it's possible to write wrappers and everything to create that functionality, but you will need to access it differently, just because it is a USB device and not a serial device.

What version of Debian? Did you check the /media folder?
Sep 6 '07 #4
RRick
463 Expert 256MB
Devices need to be mounted before you can access them as directories and files. Take a look at the mount command (i.e. man mount) and google for "usb mount" for more details.

Many times linux will automatically mount devices for you at bootup and when they get plugged in. Take a look in your desktop for an icon and take a look in the root directory for /mnt and/or /media. The devices are mounted under these directories. Once you find the mounting directory use it like any other path. (i.e. /mnt/usb1/blah/blah/blah)
Sep 6 '07 #5
Just because it is a different device will mean that you will not be able to access it the same way you would access a serial communication. I mean, it's possible to write wrappers and everything to create that functionality, but you will need to access it differently, just because it is a USB device and not a serial device.

What version of Debian? Did you check the /media folder?
Following is the system details
Linux debianTSP 2.6.18-4-686 #1

Also i checked the media folder so it shows me the usbdisk dir created over there.

I want to access any of the following using C program
debianTSP:/media/usbdisk# ls -lart /dev/u*
crw-rw-rw- 1 root root 442, 0 2007-08-31 12:24 /dev/usbdev1.1_ep00
crw-rw---- 1 root root 442, 0 2007-08-31 12:24 /dev/usbdev1.1_ep81
crw-rw---- 1 root root 442, 1 2007-08-31 12:24 /dev/usbdev1.2_ep00
crw-rw---- 1 root root 442, 1 2007-08-31 12:24 /dev/usbdev1.2_ep81
Sep 6 '07 #6
sicarie
4,677 Expert Mod 4TB
Following is the system details
Linux debianTSP 2.6.18-4-686 #1

Also i checked the media folder so it shows me the usbdisk dir created over there.

I want to access any of the following using C program
debianTSP:/media/usbdisk# ls -lart /dev/u*
crw-rw-rw- 1 root root 442, 0 2007-08-31 12:24 /dev/usbdev1.1_ep00
crw-rw---- 1 root root 442, 0 2007-08-31 12:24 /dev/usbdev1.1_ep81
crw-rw---- 1 root root 442, 1 2007-08-31 12:24 /dev/usbdev1.2_ep00
crw-rw---- 1 root root 442, 1 2007-08-31 12:24 /dev/usbdev1.2_ep81
Ok, do me a favor and do

man ls

by specifying /dev/u* you are listing the contents of the /dev/ directory that start with u, doesn't matter if you're in /media/usbdisk/ or not because you are using the absolute path (starting with / ). "man" will allow you to look at how a command should be implemented - it's short for "manual". You can do that for almost any command.

Then try this:

cd /media/usbdisk/
ls -alh

That will show you the modules in /media/usbdisk. And you can then use those as RRick has said - use the path to them as you would any other path.
Sep 6 '07 #7
Thanks for the same.
Well i looked in /media/usbdisk/, it shows me the boot directory..
Also i m using a putty to access the machine :-(
Sep 6 '07 #8
Tried with the mount command..gave an error

mount /dev/usbdev1.1_ep00
mount: can't find /dev/usbdev1.1_ep00 in /etc/fstab or /etc/mtab
Sep 6 '07 #9
sicarie
4,677 Expert Mod 4TB
Tried with the mount command..gave an error

mount /dev/usbdev1.1_ep00
mount: can't find /dev/usbdev1.1_ep00 in /etc/fstab or /etc/mtab
What was the output of the 'ls' command in /media/usbdisk/ ?

Let's make sure it is not already mounted first...
Sep 6 '07 #10
RRick
463 Expert 256MB
Just4priya,

You're actually pretty close to getting something to work.

First, stay away from the /dev directory (unless you really know what you're doing). Its not going to work the way you expect it to work. Usually, devices (things in /dev) are mounted (by the mount command) to a file system (directories and files).

If you can access /media/usbdisk, then its already been mounted (maybe). Go to that directory and start looking for directories and files like Sicarie told you. Putty will work fine in this case. The normal files commands (i.e. ls, mkdir, cd) should work here. Try creating a file by copying one over or use the touch command. If these work, then your C program will be able to do the same.
Sep 7 '07 #11
Hi

The output of ls command is

ls -l /media/usbdisk/
boot/ .created_by_pmount

I don't have permissions to create a file /media/usbdisk/ :-(

Thanks
Sep 7 '07 #12
sicarie
4,677 Expert Mod 4TB
Hi

The output of ls command is

ls -l /media/usbdisk/
boot/ .created_by_pmount

I don't have permissions to create a file /media/usbdisk/ :-(

Thanks
You shouldn't have to create anything - it should be there already. I'm not sure your usb drive is mounted there. What's supposed to be on it? (ie - is there supposed to be a 'boot' directory as the root of the USB filesystem?)
Sep 7 '07 #13
You shouldn't have to create anything - it should be there already. I'm not sure your usb drive is mounted there. What's supposed to be on it? (ie - is there supposed to be a 'boot' directory as the root of the USB filesystem?)
I could see a boot directory available in /media/usbdisk/
Sep 7 '07 #14
sicarie
4,677 Expert Mod 4TB
I could see a boot directory available in /media/usbdisk/
Right, but is that a directory that is supposed to be on your USB disk?
Sep 7 '07 #15
RRick
463 Expert 256MB
Around and around and around we go. Its difficult to know for certain whether the usb drive is mounted or not. Is the boot directory correct or not?

There is one way to find out, but it takes a couple of system reboots.

Unplug all the USB drives, and then reboot. Good back to /media and look for the usbdrive and boot directories. If one or both disappear, then they probably belong to the removed drive(s).

Shutdown the computer, add one (1) usb drive, and bootup. Go back to /media and look again. Is anything added? If so, then there's your drive.

If nothing changes during all of this, then these directories do not connect to your usbdrive.
Sep 7 '07 #16
sicarie
4,677 Expert Mod 4TB
Around and around and around we go. Its difficult to know for certain whether the usb drive is mounted or not. Is the boot directory correct or not?

There is one way to find out, but it takes a couple of system reboots.

Unplug all the USB drives, and then reboot. Good back to /media and look for the usbdrive and boot directories. If one or both disappear, then they probably belong to the removed drive(s).

Shutdown the computer, add one (1) usb drive, and bootup. Go back to /media and look again. Is anything added? If so, then there's your drive.

If nothing changes during all of this, then these directories do not connect to your usbdrive.
Yeah, the OP is connecting via Putty, so I was going on "they don't have physical access".

You can also use the command 'df' to show mounted drives. Something should point to something with "usb" in the name...
Sep 7 '07 #17
Hi,

I was successfully able to mount the USB disk and write a file into it.
Thanks for all the help provided

Priya
Sep 12 '07 #18
RRick
463 Expert 256MB
Good for you. Mounting is not a trivial task for unix boxes.

Some unixes try to protect the user from "mount" and sometimes they succeed and sometimes they don't.
Sep 13 '07 #19

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by Mathias Goldau | last post: by
4 posts views Thread by João Pires | last post: by
8 posts views Thread by john | last post: by
17 posts views Thread by Nirjhar Oberoi | last post: by
8 posts views Thread by Amar | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.