小编Jud*_*son的帖子

从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;   
                }
            }
        }
    } …
Run Code Online (Sandbox Code Playgroud)

c serial-port screen bluetooth

0
推荐指数
1
解决办法
8238
查看次数

标签 统计

bluetooth ×1

c ×1

screen ×1

serial-port ×1