I have written a program that constantly writes and reads from/to a USB serial port of a custom hardware. For each write there is always a response from the hardware.
Sometimes (separated by days), EAGAIN error is being returned for every call to write, no matter how many times I try to call it.
The only alternative for it to work again without rebooting is by issuing unbind and rebind commands to the USB port like:
# echo -n "1-3.2" > /sys/bus/usb/drivers/usb/unbind
# echo -n "1-3.2" > /sys/bus/usb/drivers/usb/bind
I wonder if it's a problem in the custom hardware, but I have no idea how to proof it, so I can ask the manufacturer to fix it.
What else could I check to guarantee that the problem is not on the code?
Here's how the port is opened:
int open_port(const char * portname)
{
    struct termios termAttr;
    struct stat st0;
    struct stat st1;
    int flock_ret;
    int fd;
    fd = open(portname, O_RDWR | O_NOCTTY | O_NDELAY);
    if (fd == -1)
    {
        printf("Can't open %s. %s", portname,
                strerror(errno));
    }
    else
    {
        fcntl(fd, F_SETFL, FNDELAY);
        tcgetattr(fd, &termAttr);
        cfsetispeed(&termAttr, B115200);
        cfsetospeed(&termAttr, B115200);
        termAttr.c_cflag |= (CLOCAL | CREAD | CS8);
        termAttr.c_iflag |= (IGNPAR | IGNBRK);
        termAttr.c_cc[VMIN] = 0;
        termAttr.c_cc[VTIME] = 0;
        tcflush(fd, TCIFLUSH);
        if (tcsetattr(fd, TCSANOW, &termAttr) == -1)
        {
            printf("%s", strerror(errno));
        }
    }
    return fd;
}
And here is how the writes are made:
void serial_write(int fd, char *comando)
{
    int cont = 0;
    int bytes_written;
    char ret[20];
    int lenght;
    char cmdfinal[200];
    if (!fd)
    {
        printf("** Error at %s: %s **", __func__, strerror(errno));
        return;
    }
    sprintf(cmdfinal, "%s\r", comando);
    lenght = strlen(cmdfinal);
    bytes_written = write(fd, cmdfinal, lenght);
    //Check for errors
    if (bytes_written == -1)
    {
        if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK)
        {
            //Wait so the next call have more chances to work
            sleep(5);
        }
        //Just print the error 
        printf("%s", strerror(errno));
        //Reopen the port
        serial_somlcd_close();
        serial_somlcd_open();
    }
    else
    {
        //Do things w/ response
    }
}