0

I have a multi-threaded application where I use wiringpi for button and pwm control. And I want to add a barcode reader(A800D) using a serial connection to it. Everything works fine if the thread with the reader is running and the rest of the threads are turned off. However, when I enable all threads, the reader returns incorrect data. What can cause such action?

Only barcode thread data:

Only barcode thread data

barcode return correct data

All threads runing data:

All threads runing data

barcode returns DDDD

barcode reader code: (wiringPiSetup(); is called at program start)

int barcode_read(char* buffer) {
    int fd;
    unsigned int nextTime;

if ((fd = serialOpen("/dev/ttyS0", 9600)) < 0)
{
    fprintf(stderr, "Unable to open serial device: %s\n", strerror(errno));
    throw 1;
}

char dat;
int i = 0;
int counter = 0;
while (true)
{
    if (counter > 100) i = 0;
    if(i>0) counter++;
    delay(5);
    while (serialDataAvail(fd))
    {
        if (i == 0) counter = 0;
        dat = serialGetchar(fd);        /* receive character serially*/
        printf("%c", dat);        
        //serialPutchar(fd, dat);
        buffer[i] = dat;
        if (i == 16) {
            serialClose(fd);
            return 1;
        }// poprawnie zaczytany kod
        i++;
        fflush(stdout);
    }
}
}

i have been closing and opening different threads, with some threads the reader works with some it doesn't.

avariant
  • 2,234
  • 5
  • 25
  • 33
scared
  • 1
  • 1

0 Answers0