sanket123
Posts: 9
Joined: Wed Aug 09, 2017 1:06 pm

Regarding rasberry pi usb library

Thu Aug 17, 2017 9:45 am

hello,

I am new to rasberry pi 3. I have install rasbian OS on my board.
I need to use usb library for communication of rasberry pi usb port with GSM usb port.
how should i start?

User avatar
topguy
Posts: 7313
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: Regarding rasberry pi usb library

Sat Aug 19, 2017 3:57 pm

Are you sure you need an "USB library" ?
GSM devices if they are recognized by the kernel will become devices which you can talk to usually without any special libraries.

Connect your device after the Pi has booted and then run the command "dmesg" in a terminal and look at the last messages because they are most likely related to your device. A typical response from inserting a GSM/3G dongle is to get one or more /dev/ttyUSB0 devices. These are (virtual) serial ports you can read and write to.

sanket123
Posts: 9
Joined: Wed Aug 09, 2017 1:06 pm

Re: Regarding rasberry pi usb library

Mon Aug 21, 2017 6:31 am

topguy wrote:
Sat Aug 19, 2017 3:57 pm
Are you sure you need an "USB library" ?
GSM devices if they are recognized by the kernel will become devices which you can talk to usually without any special libraries.

Connect your device after the Pi has booted and then run the command "dmesg" in a terminal and look at the last messages because they are most likely related to your device. A typical response from inserting a GSM/3G dongle is to get one or more /dev/ttyUSB0 devices. These are (virtual) serial ports you can read and write to.
Yes, my GSM module gets detected on raspberry pi usb port.I need send some commands to gsm device through c code. So to write application code for usb of raspberry pi I need to use usb library libraries in c. So how to install usb libraries in raspberry pi and is there any ide to generate execution fro raspberry pi so that i can run my application on boot without using raspbian OS.

User avatar
rpdom
Posts: 22762
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Regarding rasberry pi usb library

Mon Aug 21, 2017 6:47 am

Often the USB device will show up as a serial port and you can access that without needing to touch any USB code.

However, if you want to write code to use USB you will probably need to install libusb-dev and libusb-1.0-0-dev for C and additionally libusb++-dev if you are going to use C++.

sanket123
Posts: 9
Joined: Wed Aug 09, 2017 1:06 pm

Re: Regarding rasberry pi usb library

Mon Aug 21, 2017 7:51 am

rpdom wrote:
Mon Aug 21, 2017 6:47 am
Often the USB device will show up as a serial port and you can access that without needing to touch any USB code.

However, if you want to write code to use USB you will probably need to install libusb-dev and libusb-1.0-0-dev for C and additionally libusb++-dev if you are going to use C++.
I have installed usb library using following commands:

sudo apt-get install libusb-1.0-0
sudo apt-get install libusb-1.0-0-dev

I have written simple code

#include <stdio.h>
#include <libusb-1.0/libusb.h>

int main(int argc, char **argv)
{

libusb_init(NULL);

printf("hello");
return 0;
}

I have compile as gcc usb1.c -o usb1

But getting error

/tmp/ccC7cEyG.o: In function `main':
usb1.c:(.text+0x18): undefined reference to `libusb_init'
collect2: error: ld returned 1 exit status

I have also tried as
gcc usb1.c -o usb1 -l/usr/include/libusb-1.0

getting same error as:

/usr/bin/ld: cannot find -l/usr/include/libusb-1.0
collect2: error: ld returned 1 exit status

please help....

piras77
Posts: 147
Joined: Mon Jun 13, 2016 11:39 am

Re: Regarding rasberry pi usb library

Mon Aug 21, 2017 9:21 am

Code: Select all

$ find /usr | grep libusb | grep \.so\$
/usr/lib/arm-linux-gnueabihf/libusb-1.0.so
There is your lib.

So all you need:

Code: Select all

$ gcc usb1.c -o usb1 -lusb-1.0
$ ./usb1
hello

sanket123
Posts: 9
Joined: Wed Aug 09, 2017 1:06 pm

Re: Regarding rasberry pi usb library

Mon Aug 21, 2017 9:34 am

piras77 wrote:
Mon Aug 21, 2017 9:21 am

Code: Select all

$ find /usr | grep libusb | grep \.so\$
/usr/lib/arm-linux-gnueabihf/libusb-1.0.so
There is your lib.

So all you need:

Code: Select all

$ gcc usb1.c -o usb1 -lusb-1.0
$ ./usb1
hello
Thank you
It worked with above commands

User avatar
rpdom
Posts: 22762
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Regarding rasberry pi usb library

Mon Aug 21, 2017 8:32 pm

Yes, the command I used to build USB software was cc -g -o outputfile sourcefile.c -lusb-1.0

sanket123
Posts: 9
Joined: Wed Aug 09, 2017 1:06 pm

Re: Regarding rasberry pi usb library

Tue Aug 22, 2017 6:38 am

rpdom wrote:
Mon Aug 21, 2017 8:32 pm
Yes, the command I used to build USB software was cc -g -o outputfile sourcefile.c -lusb-1.0
I am going to communicate with usb device from raspberry pi 3 using c program.

Do you know any function through which i can write and read some data from usb?

Any link which can help me

scotty101
Posts: 4504
Joined: Fri Jun 08, 2012 6:03 pm

Re: Regarding rasberry pi usb library

Tue Aug 22, 2017 11:15 am

Can you share a link to the GSM device you are communicating with?

It is not possible to tell you what commands to send to the device unless we know precisely which module you are using.

Also it would be helpful if you would copy the output of the command "lsusb" here so we can see what type of device the raspberry pi recognises the GSM module as.
As other posters have tried to get across to you, most GSM modules can be communicated to to as if they were a serial port rather than having to deal with USB.
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

sanket123
Posts: 9
Joined: Wed Aug 09, 2017 1:06 pm

Re: Regarding rasberry pi usb library

Tue Aug 22, 2017 12:13 pm

scotty101 wrote:
Tue Aug 22, 2017 11:15 am
Can you share a link to the GSM device you are communicating with?

It is not possible to tell you what commands to send to the device unless we know precisely which module you are using.

Also it would be helpful if you would copy the output of the command "lsusb" here so we can see what type of device the raspberry pi recognises the GSM module as.
As other posters have tried to get across to you, most GSM modules can be communicated to to as if they were a serial port rather than having to deal with USB.
GSM module is detected as

001 Device 008: ID 1e0e:9001 Qualcomm / Option

Gsm module is SIM7600.
It has usb port. I need to fire simple AT command and read response in C.

scotty101
Posts: 4504
Joined: Fri Jun 08, 2012 6:03 pm

Re: Regarding rasberry pi usb library

Tue Aug 22, 2017 12:49 pm

Right so you do not need libusb. At all.

You need to send commands via the serial port that is created when the GSM module is plugged in. This might be called /dev/ttyUSB0 or something similar.

Since you are using C, I'd suggest you give Wiring Pi a try and use its serial port functions
http://wiringpi.com/reference/serial-library/

I can't find the datasheet for the SIM7600 so you'll need to find the AT commands and use the wiring pi serial library to send the AT commands.
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

sanket123
Posts: 9
Joined: Wed Aug 09, 2017 1:06 pm

Re: Regarding rasberry pi usb library

Tue Aug 22, 2017 1:56 pm

scotty101 wrote:
Tue Aug 22, 2017 12:49 pm
Right so you do not need libusb. At all.

You need to send commands via the serial port that is created when the GSM module is plugged in. This might be called /dev/ttyUSB0 or something similar.

Since you are using C, I'd suggest you give Wiring Pi a try and use its serial port functions
http://wiringpi.com/reference/serial-library/

I can't find the datasheet for the SIM7600 so you'll need to find the AT commands and use the wiring pi serial library to send the AT commands.
I am able send command and read response now.
I have use following functions which work
open(portname, O_RDWR | O_NOCTTY | O_SYNC);
write(fd,cmd,strlen(cmd));
read(fd, buf, sizeof(buf) - 1);

Is it possible to run application on boot.?
Without using raspbian OS or with raspbian os.

how can I generate .hex or exe for my application as we do for microcontrollers like (PIC,etc)?
so that I can burn that file in sd card to run my application.

jamesh
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 32794
Joined: Sat Jul 30, 2011 7:41 pm

Re: Regarding rasberry pi usb library

Tue Aug 22, 2017 2:07 pm

You are using a set of libraries that require the full Linux OS to be running.

If you want this to run 'bare metal' you have a completely different set of problems and things will get a lot more complicated.

Remember, the Pi is NOT a micro controller - it is a full blown Linux PC.

Search for "Linux Running program on startup" to get details on this.
Principal Software Engineer at Raspberry Pi Ltd.
Working in the Applications Team.

W. H. Heydt
Posts: 16342
Joined: Fri Mar 09, 2012 7:36 pm
Location: Vallejo, CA (US)

Re: Regarding rasberry pi usb library

Tue Aug 22, 2017 4:44 pm

sanket123 wrote:
Tue Aug 22, 2017 1:56 pm
Is it possible to run application on boot.?
Without using raspbian OS or with raspbian os.
There are a bunch of ways to run a program (or shell script) at boot. The choice will probably depend on what else you need running and how you define "at boot". Two of them are... using crontab with the "@reboot" for timing. If you need a GUI desktop up, add the command the default account "autostart" file. Obviously in this case, you can't (successfully) run your program until after the USB devices have been enumerated.

Return to “Beginners”