Go Down

Topic: Libusb help (Read 14208 times) previous topic - next topic

mdimiou



I want to make function.... and i want to send some data from a usb port.


Do you mean that you want to write a function for a program that runs on a PC so that the PC can communicate with a Leonardo over a USB connection?

If not, what computer will be running the function that uses Libusb?

You are not providing us with sufficient information for anyone to be able to give you good advice.

Your question is a bit like "I have to use a Honda engine, how do I get to San Francisco?"

...R


Ohh yeap, i want to do this.... So lets take it from the start


There is already made a code by my teacher which is running on a PC.
I have to make some functions for this program and in there i have to send some data to Arduino through USB.
I am using windows 7 and i have dev c++ and Visual studio 2010 express.

Robin2


There is already made a code by my teacher which is running on a PC.
I have to make some functions for this program and in there i have to send some data to Arduino through USB.
I am using windows 7 and i have dev c++ and Visual studio 2010 express.


That makes things a good bit clearer. Pity it's taken a whole page to get to to the question :)

Sorry, I have long since forgotten Windows programming so I can't give specific help. To be honest I didn't even know libusb works on Windows.

I think it would help people to help you if you give us an outline of each of the functions you want to make. What input paramters they will receive and what outputs they are expected to produce.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

mdimiou

These functions won't have any input parameters.
They will send only a string ( each function different string )through usb to Arduino.

If you know sth else,an other method not just with libusb i would be glad to hear it.


thanks for your reply

Robin2


These functions won't have any input parameters.


It will still help if you provide an outline of each of the proposed functions - perhaps using pseudo code. For example (based on Arduino style code)

void function1() {
    // send "this is a test from function1;
}

void function2() {
    // send "this is a test from function2;
}

I presume you have access to the libusb API so you can make an attempt to write the functions in proper code. The more work you do on this the easier it will be for people to give you some help. At the moment your question is not far from "Please write the code for me"

How will the existing code call the functions you are hoping to create?

Maybe there is another Forum tha can give advice on libusb. The Python code in the example attached to Reply #4 of this Thread illustrates the principle of communicating between a PC and an Arduino. It shouldn't be too difficult to convert the Pythong code to C++.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

mdimiou

basically no..... i don't want from anyone to write code for me..... i just don't know libusb at all.... i try to search for examples and learn about USB programming... this is what i need here....

how i can open a USB PORT, what i have to look more carefully......

But not... i don't want someone to do work for me...!!!

Robin2

You don't open a USB port to get a PC to talk to an Arduino. You open a Serial port.

Did you look at the examples in the link I gave you?

It sounds to me like you need to get more instruction from your teacher. Perhaps if you show him/her this Thread s/he will appreciate what you don't understand.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

bperrybap


You don't open a USB port to get a PC to talk to an Arduino.

If using libusb you do.
That is the entire point of libusb.
You open a connection to a device bulk end point and push data at it.
It is Host USB (PC) talking to Device USB (the leonardo) directly over USB.

To get a feel for libusb and the API functions, have a look at the documentation here:
Code: [Select]
http://libusb.sourceforge.net/doc/
If you google you can find lots of information out there on libusb

The challenge when using the Arduino environment is going to be that it has no debugging environment
The only way that is typically used is sending character data (printed messages)
from the Arduino board over a serial or virtual serial interface back to the host.
The default serial interface is named "Serial".
On Leonardo, that "serial" interface uses a virtual serial interface over USB.
But in this case, you can't use that because you will be using the USB for raw USB communication
with libusb.

If you had an AVR dragon, or a AVR ICE you could use Atmel Studio instead of the Arduino IDE
and have full source level debugging.
It can be done with the Arduino IDE (I've done it) but it is not easy to set up and I'm not sure
it could be done on Windows.

In the absence of source level debugging,
What you can do is use the USB for your libusb communication
and then use the real UART inside the 32u4 for your serial port.
It will be Serial1 instead of Serial.
See this:
http://arduino.cc/en/Guide/ArduinoLeonardoMicro?from=Guide.ArduinoLeonardo
RX will be pin 0 and TX will be pin 1.
You will need some sort of way to read the serial port data on the host.
Something like an FTDI cable or USB to TTL  module.

If you have to build the code on the PC vs the teacher giving you pre-compiled
executables, then you are going to need to set up a build environment.
You will need compilers, libraries, binutils, editor, make, shell,  revision control tools, etc...
Your teacher should be able to assist for that as I'm assuming the exercise is about
writing code to use USB communications using libusb vs how to locate and set up
all the needed tools to to create a build environment on Windows.
With linux, it is much simpler to do development since
all the tools are native and  pretty much built in,
since unix was designed by developers to do development.

Hope that helped.

--- bill





Robin2


To get a feel for libusb and the API functions, have a look at the documentation here:


I have no personal interest in libusb but I am delighted someone has come along that knows something about it and can help the OP. I will leave him in your capable hands.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

mdimiou

Ok, here is my code..... C code and arduino code. I am trying to test them together. So i connect Arduino with usb cable and i upload my code into it. With Serial Monitor from tools it seems that it's code is correct. When i run my code from pc i can see Rx led flashing one time but i cannot see any response from leds. Can you help me find any mistakes in both codes?

Arduino code:
const int Relay1=12;
const int Relay2=11;
int bytesRead;
char incoming[8];

void setup(){
  Serial.begin(9600);
  pinMode(Relay1,OUTPUT);
  pinMode(Relay2,OUTPUT);
  digitalWrite(Relay2,LOW);
  digitalWrite(Relay1,LOW);
}

void loop(){
  int matchFlag = 0;
 
  if(Serial.available()>0){
    bytesRead = Serial.readBytesUntil('\n', incoming, 7);
    incoming[bytesRead] = '\0';                        // Add null terminator...
    if (strcmp(incoming, "lights") == 0){
      Serial.println("Lights matched");
      matchFlag = 1;
      digitalWrite(Relay1, HIGH);
    }
    if (strcmp(incoming, "voltage") == 0){
      Serial.println("Voltage matched");
      matchFlag = 1;
      digitalWrite(Relay2, HIGH);
    }

    if (strcmp(incoming, "end") == 0){
      Serial.println("End matched");
      matchFlag = 1;
      digitalWrite(Relay2,LOW);
      delay(3000);
      digitalWrite(Relay1,LOW);
    }
    if (matchFlag == 0){
      Serial.println("error");
    }
  }
}


C code:

#include <usb.h>
#include <stdio.h>
#include <stdlib.h>


/* the device's vendor and product id */
#define  Vid 0x2341
#define  Pid 0x8036   /* or 0x8036 */

/* the device's endpoints */
#define ep_in 0x83
#define ep_out 0x02

usb_dev_handle *open_dev(void);

usb_dev_handle *open_dev(void)
{
  struct usb_bus *bus;
  struct usb_device *dev;

  for(bus = usb_get_busses(); bus; bus = bus->next)
    {
      for(dev = bus->devices; dev; dev = dev->next)
        {
          if(dev->descriptor.idVendor == Vid
             && dev->descriptor.idProduct == Pid)
            {
              printf("Found Specified Device\n"); 
              return usb_open(dev);
            }
           else
           {
               printf("Didn't Find Specified Device\n"); 
           }
        }
    }
  return NULL;
}

int lights(void){

   usb_dev_handle *dev = NULL; /* the device handle */
   int reply;  /* testing sending data*/
   char *msg; /*every function will have it's msg*/
   msg=(char *)malloc(8*sizeof(char));
   if(msg==NULL)
   {
      printf("error: Not enough memory!\n");
    }                 
   char end[3];
   usb_init(); /* initialize the library */
   usb_find_busses(); /* find all busses */
    usb_find_devices(); /* find all connected devices */
    msg="lights\n";

   /*testing for errors*/
   if(!(dev = open_dev()))
    {
      printf("error: device not found!\n");
      system("PAUSE");
      return -1;
    }

    if(usb_set_configuration(dev, 1) < 0)
    {
      printf("error: setting config 1 failed\n");
      usb_close(dev);
      system("PAUSE");
      return -1;
    }

    if(usb_claim_interface(dev, 0) < 0)
    {
      printf("error: claiming interface 0 failed\n");
      usb_close(dev);
      system("PAUSE");
      return -1;
    }

   /*sending data to Serial Port*/
   reply=usb_bulk_write(dev,ep_out,msg,sizeof(msg),5000);
   printf("ok until here \n");
   printf("Reply :","%d \n",reply);

   /*error check*/
   if(reply<0){
     printf("error: failed to send message \n");
      system("PAUSE");
      return -1;
   }

   

   /*closing com*/
   usb_release_interface(dev, 0);
    usb_close(dev);
    free(msg);
    return 0;
}


int main(void)
{
    if(lights()==0){
       printf("\n lights on \n");
       system("PAUSE");
      }
    else if(lights()==-1){
       printf("\n problem with lights \n");
       system("PAUSE");
       }


majenkotech

I think you're completely barking up the wrong tree here.

First you need to get a clue about how USB works, including the meaning of HID, CDC/ACM, etc.

You have programmed the Arduino to receive data over Serial.  On the Leonardo that uses a CDC/ACM endpoint.

Windows handles the connection to that through a COM port.  The serial monitor (**AS THE NAME SUGGESTS**) talks through a COM port.

Now you are trying to talk to it all through libusb.  Libusb communicates using HID endpoints.

Quite how you hope to get that to work is anybody's guess...

You need to choose one way to go.  Either just talk Serial (which is by far the simplest option as you don't need libusb), or talk HID, in which case you will need to investigate enabling HID support in the Arduino core (disabled by default I think - not sure it even works at all). 

Using HID to talk to a Serial device is like trying to talk to a Chinese person in Spanish.  It just isn't going to happen.
A better IDE for the Arudino: UECIDE - The Universal Embedded Computing IDE.

mdimiou

First of all, thanks for your reply.....
The truth is that i have never being asked to do sth like that and i try to learn whatever i could from the net,which is a chaos if you don't know what or where to look. You said that it is more easy is i "i just talk Serial".... can you give me sth to read, so i can make  things better?

The only problem i have is that i need to give those functions to my teacher who has already make a program ,which is gonna run probably in Linux and i use windows 7 and i have to use C.

I would be glad if you can help me little more...thanks

majenkotech

You keep going on about this mysterious program your teacher has, but never tell us what it actually is, nor what it does.

You have to make a choice.  Are you going to try and use a HID interface and libusb, which will be a nightmare to do as you are basically working from the lowest levels of the system, or are you going to scrap the whole idea of libusb and just open a serial port to send data?

Until you make that choice, and we can't make it for you, then we can't really help you.
A better IDE for the Arudino: UECIDE - The Universal Embedded Computing IDE.

mdimiou


You keep going on about this mysterious program your teacher has, but never tell us what it actually is, nor what it does.

You have to make a choice.  Are you going to try and use a HID interface and libusb, which will be a nightmare to do as you are basically working from the lowest levels of the system, or are you going to scrap the whole idea of libusb and just open a serial port to send data?

Until you make that choice, and we can't make it for you, then we can't really help you.


This program is going to used by doctors to decide if a patient has a HPV virus and what type of the virus analyzing a photo.
I have to buld a system that is going to start electrophoresis when it has to and taking pictures. I will control those processes by this Arduino Leonardo and i have to make these functions in C cause this program will decide if it wants one more picture or not.

Now, cause i need to make things a little bit faster i think i will leave libsub for an other time..... so i will try to open a serial port.....but i don't know how.....so i would like to ask you to give me some directions how to do that.

Sr for my bad English

majenkotech

Ok.  So opening a serial port in C is dependant on the operating system you are working with.  You say you are programming in Windows but the program is to run on Linux.

Well you can stop right there :P

Install VirtualBox, then install Ubuntu inside it, and do your programming directly in Linux.

Then ask google how to work with serial ports in C in Linux - you'll find plenty of code around for that.
A better IDE for the Arudino: UECIDE - The Universal Embedded Computing IDE.

mdimiou

thanks a lot!!!!


Next time i hope i will send an ok message to this topic as my final message!!!!

Have a good day!!!

Marios Dimiou

Go Up