1

I am getting a SIGPIPE error when I try to write to a file descriptor that was returned when I opened a USB device. I can read all day from it, but I cannot write to it. Below is the code:

#include <fcntl.h>
#include <unistd.h>
#include <iostream>
#include <sys/socket.h>
#include <string>
#include <stdio.h>
#include <sstream>
#include <pthread.h>
#include <string.h>
#include <signal.h>
#include <errno.h>

void* Thread(void*);
int fd = -1;
std::string masterName;
std::string slavename;

int main() {

    char buf[1024];
    fd = open("/dev/usbdevice", O_RDWR | O_NOCTTY);

    pthread_t thread;
    pthread_create(&thread, NULL, Thread, (void*)NULL);

    while(1) {
        int returnVal = read(fd, buf, sizeof(buf));
        if(returnVal <= 0) {
                 std::cout << "Read() returned 0 bytes." << std::endl;
                 break;
        }
        std::cout << "Buffer Data: " << byteChars << std::endl;
    }
    close(fd);
    return 0;
}

void* Thread(void*) {
    char buffer[2];
    while(1) {
        memset(buffer, 0, 2);
        std::cout << "Write to device: " << std::endl;
        std::cin >> buffer;
        ssize_t ret = write(fd, buffer, 2);
        if(ret == -1) {
            std::cout << "errno: " << errno << std::endl;
        }
        else {
                std::cout << "Successful write" << std::endl;
        }
    }
    return NULL;
}

Basically I set up a thread to be able to write to the file descriptor "fd" of which was returned from the "open()" function in the beginning. I get an error every time I attempt to write to it. But as I said before, I can read from it all day with no problem.

Side Note I have checked the fd to make sure I am writing to the correct one and I definitely am.

When I researched the SIGPIPE error, I see that every one says it happens when the fd gets closed on the remote end. So #1 how is it possible for that file descriptor to get closed? And #2 how am I still able to read from the device on the same file descriptor if it is closed?

Zonxwedop
  • 387
  • 1
  • 3
  • 12
  • 1
    It completely depends on what this USB device is, and what its device driver does. Without knowing anything else about it, it looks like it is simply not prepared to accept any data that is written to it. – Celada Mar 13 '13 at 01:11
  • I have provided documentation of which give specific commands that I need to write to it. The device is a magnetic stripe card reader. It is supposed to accept 2 byte strings as commands. – Zonxwedop Mar 13 '13 at 12:36
  • I found something new. I restarted the machine (xubuntu VM), and the first time I ran the program, I did not get a SIGPIPE error on my first write. Once I read from the reader, I could not write to it, and since that first write, I havnt been able to write to it since. – Zonxwedop Mar 13 '13 at 12:39
  • it sounds like an idiosyncracy or bug of this particular USB mag stripe reader or its driver. I think you will need to ask whoever wrote the driver (unless it is something generic like a HID device) – Celada Mar 13 '13 at 12:59
  • Well it mounts as "/dev/hidraw" I have asked, but I am waiting on the reply. I feel like this should be pretty generic, there are no specific drivers to install and it mounts automatically. – Zonxwedop Mar 13 '13 at 14:23
  • I doubt that this device is mounted. It's more likely a normal (character) device node. Anyway, if it's a HID device, might I suggest adding some tags like "usb" and "hid" so that the appropriate experts will see it? Might as well remove "sockets" while you're at it as it doesn't have anything to do with sockets if ny understanding us correct. – Celada Mar 13 '13 at 15:59
  • I added those tags, the reason why I added sockets is because most people tend to immediately say it's a problem with a the socket fd closing on the device end or something similar to that. Regardless, thank you for your input. – Zonxwedop Mar 15 '13 at 16:43

0 Answers0