从 C 读取串行数据 (OSX /dev/tty)
我正在尝试使用 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
并且扫描仪上扫描的任何条形码都会出现在终端中,我只是无法对数据执行任何操作。
贾德
I am trying to read data from a bluetooth barcode scanner (KDC300) using C. Here is the code I have so far, and the program successfully establishes a bluetooth connection to the scanner, but when a barcode is scanned, no input is displayed on the screen (Eventually more will be done with the data, but we have to get it working first, right).
Here is the program:
#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;
}
If anyone has any ideas, I am at a loss on this so far. If it is any help, I can run screen /dev/tty.KDC1
and any barcodes scanned on the scanner appear in the terminal, I just can't do anything with the data.
Jud
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这行:
不做你认为它做的事。这是一个带有空主体的
while
循环。This line:
Does not do what you think it does. That's a
while
loop with an empty body.@tommieb75,
strcat 语句来自程序的第一个“go”,我从 argv[1] 中获取一个变量并将其附加到 /dev/tty.*,以便您可以选择要监视的设备。
我不知道为什么我注释掉了
buf
,可能是因为看代码太多/尝试不同的方法而忘记了我在哪里(不是一个 C 程序员,这就是我如何得到在 30 LOC 内丢失)。@caf,很好地捕获了 while 循环后的额外分号,不幸的是,即使在更正它之后,程序也无法正确运行。
我正在进一步研究这个问题。我可以验证(使用 osx packetlogger)计算机正在获取数据,但是缓冲区中从未放置任何数据。
-Jud
---------------编辑--------------
经过一番尝试和错误后我解决了这个问题。添加以下代码来设置串行连接解决了一切:
感谢其他答案让我走到这一步。
@tommieb75,
the strcat statement was from the first "go" at the program, I took a variable from argv[1] and appended it to the /dev/tty.* so you could select which device you wanted to monitor.
I am not sure why I had commented out
buf
, probably stems from looking at the code too much / trying different approaches and forgetting where I was (not much of a C programmer, which is how I can get lost in 30 LOC).@caf, Good catch on the extra semi-colon after the while loop, unfortunately, even after correcting it, the program doesn't behave correctly.
I am researching the problem further. I can verify (with osx packetlogger) that the computer is getting the data, but the but the buffer never has any data placed in it.
-Jud
---------------Edit--------------
I solved the problem after a little trial and error. Adding the following code to setup the serial connection solved everything:
Thanks to the other answers for getting me to this point.
这里是我找到的最好的信息。
使用 termios 的 C 程序只需添加
并更改波特率以满足我的需求即可工作。
Here is the best info I've found.
The C program on there using termios worked just by adding
And changing the baudrate to match my needs.
在你的代码中
你为什么这么做?这样做会更容易:
为什么参数引用命令行?
为什么上面的
buf
声明被注释掉了?In your code
Why did you do that? It would be easier to do it this way:
Why the parameter referencing to the command line?
Why did you have
buf
declaration commented out above?