Search code examples
cunixcurlraspberry-pibarcode-scanner

Unix program to use barcode input and cURL command on Raspberry Pi


I want to build a program that will run in unix (raspberry pi) that will simply wait for input from a barcode reader and add the input into a cURL command, and then execute that command. The url is something like:

http://www.xyz.com/test/order/complete?barcode=123456789

where 123456789 would be the input.

Typically when a barcode is read with a scanner, it will add in a carriage return after it reads the code, meaning if you scanned it into a text editor, you would see the barcode and the cursor would be on the next line. I apologize for my ineptitude of programming - it's my parents fault ;)

I intend on running this program on startup of the Raspberry Pi instead of it running the desktop gui program, so some insight on that would be helpful as well.


Solution

  • Assuming your barcode reader sends the characters via keyboard input and does indeed put a CR after the barcode, I should think something like this would work. Compile w/gcc:

    gcc -o runcurl runcurl.c

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main(int argc,char *argv[])
    
        {
        while (1)
            {
            char buf[256],syscmd[512];
            int i;
    
            /* Get next barcode */
            printf("Waiting for bar code [q=quit]:  ");
            if (fgets(buf,255,stdin)==NULL)
                break;
    
            /* Clean CR/LF off of string */
            for (i=0;buf[i]!='\0' && buf[i]!='\r' && buf[i]!='\n';i++);
            buf[i]='\0';
    
            /* q = quit */
            if (!strcmp(buf,"q"))
                break;
    
            /* Build into curl command */
            sprintf(syscmd,"curl \"http://www.xyz.com/test/order/complete?barcode=%s\"",buf);
    
            /* Execute--this will wait for command to complete before continuing. */
            system(syscmd);
            }
        return(0);
        }