Find the answer to your Linux question:
Results 1 to 2 of 2
Hi, on ubuntu I try to access a PIC-controler via the ttyUSB0. I send a few command bytes and get the status from the controler. My C code works without ...
  1. #1
    Just Joined!
    Join Date
    Apr 2010
    Posts
    1

    connect to ttyUSB with C/C++

    Hi,

    on ubuntu I try to access a PIC-controler via the ttyUSB0. I send a few command bytes and get the status from the controler. My C code works without problems on ttyS0 but with the usb-to-serial adapter (I tried two with defferent chipsets pl2303 + ch341-uart) it just sends but can't read from the controller.
    With minicom I can communicate with an other computer without problems but in my code there must be something wrong for the ttyUSB.

    Any ideas?

    Here my code for the termios configuration:
    Code:
    struct termios oldtio,newtio;
    
    extern "C" HANDLE SioOpen(char *name, unsigned int baudrate)
    {
    BOOL RetStat;
    HANDLE ComHandle;
    DWORD winrate;
    
    //Open COM port as a file
    printf("Opening %s\n",name);
    ComHandle = open(name, O_RDWR) | O_NOCTTY);
    
    	if (ComHandle < 0)
    		{
            printf("%s failed to open\n",name);
            perror(name); 
        	}
    
        switch (baudrate) {
    		case 9600: 	winrate = B9600; break;
    		case 19200: 	winrate = B19200; break;
    		case 38400: 	winrate = B38400; break;
    		case 57600: 	winrate = B57600; break;
    		case 115200: 	winrate = B115200; break;
        	default:		printf("Baud rate not supported - using default of 19200\n");
        					winrate = B19200;
        	}
        //printf("tcgetattr(ComHandle,&oldtio)\n");
    
        tcgetattr(ComHandle,&oldtio); // save current port settings
    
        bzero(&newtio, sizeof(newtio));
    
        newtio.c_cflag = winrate | CS8 | CLOCAL | CREAD;
        newtio.c_iflag = IGNPAR;
        newtio.c_oflag = 0;
    
        // set input mode (non-canonical, no echo,...)
        newtio.c_lflag = 0;
    
        // Wait block for 100ms, then timeout
        newtio.c_cc[VTIME]    = 1;   // inter-character timer unused 
        newtio.c_cc[VMIN]     = 0;   // blocking read until 1 chars received 
    
        //tcflush(ComHandle, TCIFLUSH);
        tcflush(ComHandle, TCIOFLUSH);
        //printf("tcsetattr(ComHandle,TCSANOW,&newtio)\n");
        RetStat = tcsetattr(ComHandle,TCSANOW,&newtio);
    
    	if (RetStat < 0)
        	{
        	printf("Failed to set COMM configuration\n");
            //break;
            }
    //printf("Setup complete, returning file handle.\n"); 
    return(ComHandle);
    }

  2. #2
    Just Joined!
    Join Date
    Jan 2011
    Location
    Fairfax, Virginia, USA
    Posts
    94

    I think I might know what is wrong

    Sorry for the late response, I had this same problem months later. My problem occurs 2/3 of the time. For reasons unknown, if you open(2) your tty non blocking then select(2) on your read(2), things will probably work. I suppose its a driver problem. I observed minicom always worked and strace(1)d it and compared the output with my output. I'm really surprised this problem has been around this long.
    Last edited by BrianMicek; 01-16-2011 at 05:03 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...