首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Ubuntu for USB scale下串口的读写

Ubuntu for USB scale下串口的读写
EN

Stack Overflow用户
提问于 2019-09-10 02:50:20
回答 1查看 194关注 0票数 0

我有一个电子秤,通过USB连接到我的Ubuntu笔记本电脑上,我想从上面读取测量值。串行协议非常简单(9600,8N1,ttyUSB0),我能够从终端使用putty (VT100+)正确读取测量值。

天平需要接收命令

代码语言:javascript
复制
"READ<CR><LF>"

以便发送测量结果。每个测量都有这样的格式:

代码语言:javascript
复制
01ST,GS,   2.5,kg<CR><LF>

例如,如果我测量的是2.5千克的负载。

现在,我正在尝试从一个C应用程序发送READ命令,但是我无法得到任何响应。

代码语言:javascript
复制
#include <errno.h>
#include <fcntl.h> 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>

int set_interface_attribs(int fd, int speed)
{
    struct termios tty;

    if (tcgetattr(fd, &tty) < 0) {
        printf("Error from tcgetattr: %s\n", strerror(errno));
        return -1;
    }

    cfsetospeed(&tty, (speed_t)speed);
    cfsetispeed(&tty, (speed_t)speed);

    tty.c_cflag |= (CLOCAL | CREAD);    /* ignore modem controls */
    tty.c_cflag &= ~CSIZE;
    tty.c_cflag |= CS8;         /* 8-bit characters */
    tty.c_cflag &= ~PARENB;     /* no parity bit */
    tty.c_cflag &= ~CSTOPB;     /* only need 1 stop bit */
    tty.c_cflag &= ~CRTSCTS;    /* no hardware flowcontrol */

    /* setup for non-canonical mode */
    tty.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON);
    tty.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
    tty.c_oflag &= ~OPOST;

    /* fetch bytes as they become available */
    tty.c_cc[VMIN] = 1;
    tty.c_cc[VTIME] = 1;

    if (tcsetattr(fd, TCSANOW, &tty) != 0) {
        printf("Error from tcsetattr: %s\n", strerror(errno));
        return -1;
    }
    return 0;
}

void set_mincount(int fd, int mcount)
{
    struct termios tty;

    if (tcgetattr(fd, &tty) < 0) {
        printf("Error tcgetattr: %s\n", strerror(errno));
        return;
    }

    tty.c_cc[VMIN] = mcount ? 1 : 0;
    tty.c_cc[VTIME] = 5;        /* half second timer */

    if (tcsetattr(fd, TCSANOW, &tty) < 0)
        printf("Error tcsetattr: %s\n", strerror(errno));
}


int main()
{
    char *portname = "/dev/ttyUSB0";
    int fd;
    int wlen;
    printf("Opening the connection on serial port\n");
    fd = open(portname, O_RDWR | O_NOCTTY | O_SYNC);
    if (fd < 0) {
        printf("Error opening %s: %s\n", portname, strerror(errno));
        return -1;
    }
    /*baudrate 9600, 8 bits, no parity, 1 stop bit */
    set_interface_attribs(fd, B9600);
    //set_mincount(fd, 0);                /* set to pure timed read */

    /* simple output */
    printf("Sending the command READ\n");
    wlen = write(fd, "READ\n", 5);
    if (wlen != 5) {
        printf("Error from write: %d, %d\n", wlen, errno);
    }
    tcdrain(fd);    /* delay for output */


    /* simple noncanonical input */
    do {
        unsigned char buf[80];
        int rdlen;

        rdlen = read(fd, buf, sizeof(buf) - 1);
        if (rdlen > 0) {

            buf[rdlen] = 0;
            printf("Read %d: \"%s\"\n", rdlen, buf);

        } else if (rdlen < 0) {
            printf("Error from read: %d: %s\n", rdlen, strerror(errno));
        } else {  /* rdlen == 0 */
            printf("Timeout from read\n");
        }               
        /* repeat read to get full message */
    } while (1);
}

你能帮帮我吗?谢谢!我是一个初学者,所以这可能只是一个我看不到的愚蠢的错误。

然而,有没有其他更快的方法来完成同样的任务呢?

EN

回答 1

Stack Overflow用户

发布于 2019-09-10 02:53:14

该命令可能应该以回车符结束(而不是您编写的换行符):

代码语言:javascript
复制
    wlen = write(fd, "READ\n", 5);

更改为

代码语言:javascript
复制
    wlen = write(fd, "READ\r", 5);

如果它真的(可能,但可能不是)必须接收cr lf:

代码语言:javascript
复制
    wlen = write(fd, "READ\r\n", 6);
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57859690

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档