从 C (OSX/dev/tty) 读取串行数据 [英] Reading Serial Data From C (OSX /dev/tty)

查看:25
本文介绍了从 C (OSX/dev/tty) 读取串行数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 C 从蓝牙条形码扫描仪 (KDC300) 读取数据.这是我到目前为止的代码,程序成功建立了与扫描仪的蓝牙连接,但是当扫描条形码时,没有输入显示在屏幕上(最终会对数据做更多的工作,但我们必须先让它工作,对吧).

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).

这是程序:

#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
", buf, res);
                    if(buf[sizeof(buf)]=='
') break;   
                }
            }
        }
    }

    return 0;
}

如果有人有任何想法,到目前为止我对此一无所知.如果有任何帮助,我可以运行 screen/dev/tty.KDC1 并且扫描仪上扫描的任何条形码都会出现在终端中,我无法对数据做任何事情.

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.

贾德

推荐答案

@tommieb75,strcat 语句来自第一个go";在程序中,我从 argv[1] 中取出一个变量并将其附加到/dev/tty.* 中,以便您可以选择要监控的设备.

@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.

我不知道为什么我注释掉了 buf,可能是因为看代码太多/尝试不同的方法而忘记了我在哪里(不是一个 C 程序员,这就是我可以在 30 LOC 中迷路).

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,很好地抓住了 while 循环后多余的分号,不幸的是,即使在更正之后,程序也不能正常运行.

@caf, Good catch on the extra semi-colon after the while loop, unfortunately, even after correcting it, the program doesn't behave correctly.

我正在进一步研究这个问题.我可以验证(使用 osx packetlogger)计算机正在获取数据,但是缓冲区中从来没有放置任何数据.

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.

-贾德

经过反复试验,我解决了这个问题.添加以下代码来设置串行连接解决了一切:

I solved the problem after a little trial and error. Adding the following code to setup the serial connection solved everything:

struct termios theTermios;

memset(&theTermios, 0, sizeof(struct termios));
cfmakeraw(&theTermios);
cfsetspeed(&theTermios, 115200);

theTermios.c_cflag = CREAD | CLOCAL;     // turn on READ
theTermios.c_cflag |= CS8;
theTermios.c_cc[VMIN] = 0;
theTermios.c_cc[VTIME] = 10;     // 1 sec timeout
ioctl(fileDescriptor, TIOCSETA, &theTermios);

感谢其他答案让我明白这一点.

Thanks to the other answers for getting me to this point.

这篇关于从 C (OSX/dev/tty) 读取串行数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆