【发布时间】:2012-11-28 14:48:57
【问题描述】:
我正在尝试使用 C 从蓝牙条形码扫描仪 (KDC300) 读取数据。这是我到目前为止的代码,程序成功建立了与扫描仪的蓝牙连接,但是当扫描条形码时,没有输入显示在屏幕上(最终会对数据进行更多处理,但我们必须先让它工作,对吧)。
这是程序:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <termios.h>
#include <sys/ioctl.h>
int main (int argc, const char * argv[]) {
// define vars
int STOP = 0;
//char buf[255];
if(argv[1])
{
int fd = open("/dev/tty.KDC1", O_RDONLY);
if(fd == -1)
{
printf("%s", strcat("Unable to open /dev/tty.", argv[1]));
}
int res;
while(STOP == 0)
{
while((res = read(fd,buf,255)) == 0);
{
if(res > 0)
{
buf[res]=0;
printf("%s:%d\n", buf, res);
if(buf[sizeof(buf)]=='\n') break;
}
}
}
}
return 0;
}
如果有人有任何想法,到目前为止我对此一无所知。如果有任何帮助,我可以运行screen /dev/tty.KDC1,扫描仪上扫描的任何条形码都会出现在终端中,我无法对数据做任何事情。
贾德
【问题讨论】:
-
为什么buf的声明被注释掉了?你有一个缓冲区溢出错误。将 buf 声明为 buf[256];如果读取返回 255,您将有空间用于终止 (char) 0。
标签: c serial-port bluetooth screen