从C读取串行数据(OSX/dev/tty)

Jud*_*son 0 c serial-port screen bluetooth

我正在尝试使用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;
}
Run Code Online (Sandbox Code Playgroud)

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

caf*_*caf 7

这一行:

        while((res = read(fd,buf,255)) == 0);
Run Code Online (Sandbox Code Playgroud)

不做你认为它做的事.这是一个while空体循环.