gpt4 book ai didi

c - Unix程序在树莓派上使用条形码输入和cURL命令

转载 作者:太空宇宙 更新时间:2023-11-04 04:47:57 25 4
gpt4 key购买 nike

我想构建一个将在 unix (raspberry pi) 中运行的程序,它将简单地等待来自条形码阅读器的输入并将输入添加到 cURL 命令中,然后执行该命令。网址类似于:

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

其中 123456789 将是输入。

通常当用扫描仪读取条形码时,它会在读取代码后添加一个回车符,这意味着如果您将其扫描到文本编辑器中,您会看到条形码并且光标将位于下一行.我为我的编程无能道歉 - 这是我 parent 的错 ;)

我打算在 Raspberry Pi 启动时运行这个程序,而不是运行桌面 gui 程序,所以对此的一些了解也会有所帮助。

最佳答案

假设您的条形码阅读器通过键盘输入发送字符,并且确实在条形码后放置了一个 CR,我认为这样的方法可行。使用 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);
}

关于c - Unix程序在树莓派上使用条形码输入和cURL命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19052584/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com