Control USB port with progam in C language.

+1 Andres Parra · March 30, 2015
Hello.
I want to control the USB port in C.
The first thing I try to do is identify if the program can recognize the port has a device and tell the GUID.
But I have some many problem, one of this is the library, I would know which one can I use for C, and a littler code that can help me to identify the port and later I can run with this program.

Would you be kind enough to help me with this challenge.

Thanks for you time.

Post a Reply

Replies

Oldest  Newest  Rating
0 Andres Parra · March 31, 2015
Sorry... Could you explain me that?
0 M .A.K · March 31, 2015
Hi.

if u r using linux:
Take a look here at Host software section:
 https://elcharolin.wordpress.com/2014/04/07/pic18-linux-hid-example/

And here u can find programs for both win and linux:
http://openprog.altervista.org/USB_firm_eng.html

My own exprience:
If u wanna communicate through usb as if it's serial port: (I tried in linux to communicate with microcontrollers through usb to UART logic changers,i just tied tx and rx together and runned the code to recieve echo of what ever i am sending. change the program to fit ur needs!)

*Run the program as root or change ur usb file (exp:/dev/ttyUSB0) permissions.

#include <stdio.h>   /* Standard input/output definitions */
#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 <iostream>

using namespace std;

/*
 * 'open_port()' - Open serial port 1.
 *
 * Returns the file descriptor on success or -1 on error.
 */

int open_port(void)
{
  int fd; /* File descriptor for the port */


  fd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY | O_NDELAY);
  if (fd == -1)
  {
   /*
    * Could not open the port.
    */

    perror("open_port: Unable to open /dev/ttyUSB0 - ");
  }
  else
    fcntl(fd, F_SETFL, 0);

  return (fd);
}


int                  /* O - 0 =  ok, -1 =  bad */
sendAndGet(int fd)   /* I - Serial port file */
{
  char buffer[255];  /* Input buffer */
  char *bufptr;      /* Current char in buffer */
  int  nbytes;       /* Number of bytes read */
  int  tries;        /* Number of tries so far */

  for (tries = 0; tries < 3; tries ++)
  {
   /* send info */
    if (write(fd, "MAK\r", 4) < 3)
      continue;

   /* read characters into our string buffer until we get a CR or NL or any other ending send character */
    bufptr = buffer;
    while ((nbytes = read(fd, bufptr, buffer + sizeof(buffer) - bufptr - 1)) > 0)
    {
      bufptr += nbytes;
      if (bufptr[-1] == '\n' || bufptr[-1] == '\r')
        break;
    }

   /* nul terminate the string and see if we got proper response */
    *bufptr = '\0';

    if (strncmp(buffer,"MAK\r", 3) == 0){
        int i=0;
    while(buffer!='\0'){cout<<buffer; i++;}
      return (0);
  }

  return (-1);
}
}

int main(void){
int fd=open_port();
sendAndGet(fd);
close(fd);
return 0;
}



Source : http://mirror.datenwolf.net/serial/
  • 1

C

107,037 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator