Need help with USB programming in Linux

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • just4priya
    New Member
    • Sep 2007
    • 19

    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:
    [code=cpp]
    sprintf(portfil e,"/dev/usbdev2.1_ep81" );
    if((fd=open(por tfile, O_RDWR | O_NOCTTY | O_NDELAY))==-1)
    perror("open_po rt: unable to open usbdev2.1_ep81" );
    [/code]
    Please provide some inputs/help

    Thanks,
    Priya
    Last edited by sicarie; Sep 6 '07, 08:14 PM. Reason: Code Tags.
  • sicarie
    Recognized Expert Specialist
    • Nov 2006
    • 4677

    #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.

    Comment

    • just4priya
      New Member
      • Sep 2007
      • 19

      #3
      Originally posted by sicarie
      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

      Comment

      • sicarie
        Recognized Expert Specialist
        • Nov 2006
        • 4677

        #4
        Originally posted by just4priya
        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?

        Comment

        • RRick
          Recognized Expert Contributor
          • Feb 2007
          • 463

          #5
          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)

          Comment

          • just4priya
            New Member
            • Sep 2007
            • 19

            #6
            Originally posted by sicarie
            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

            Comment

            • sicarie
              Recognized Expert Specialist
              • Nov 2006
              • 4677

              #7
              Originally posted by just4priya
              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.

              Comment

              • just4priya
                New Member
                • Sep 2007
                • 19

                #8
                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 :-(

                Comment

                • just4priya
                  New Member
                  • Sep 2007
                  • 19

                  #9
                  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

                  Comment

                  • sicarie
                    Recognized Expert Specialist
                    • Nov 2006
                    • 4677

                    #10
                    Originally posted by just4priya
                    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...

                    Comment

                    • RRick
                      Recognized Expert Contributor
                      • Feb 2007
                      • 463

                      #11
                      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.

                      Comment

                      • just4priya
                        New Member
                        • Sep 2007
                        • 19

                        #12
                        Hi

                        The output of ls command is

                        ls -l /media/usbdisk/
                        boot/ .created_by_pmo unt

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

                        Thanks

                        Comment

                        • sicarie
                          Recognized Expert Specialist
                          • Nov 2006
                          • 4677

                          #13
                          Originally posted by just4priya
                          Hi

                          The output of ls command is

                          ls -l /media/usbdisk/
                          boot/ .created_by_pmo unt

                          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?)

                          Comment

                          • just4priya
                            New Member
                            • Sep 2007
                            • 19

                            #14
                            Originally posted by sicarie
                            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/

                            Comment

                            • sicarie
                              Recognized Expert Specialist
                              • Nov 2006
                              • 4677

                              #15
                              Originally posted by just4priya
                              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?

                              Comment

                              Working...