在 Ubuntu 下从串口读取和写入 USB 规模 [英] Read and Write from serial port under Ubuntu for USB scale
问题描述
我有一个通过 USB 连接到我的 Ubuntu 笔记本电脑的数字秤,我想从中读取测量值.串行协议非常简单(9600、8N1、ttyUSB0),我可以通过终端使用腻子(VT100+)正确读取测量值.
I have a digital scale connected via USB to my Ubuntu laptop and I would like to read the measurements from it. The serial protocol is very simple (9600,8N1, ttyUSB0) and I'm able to correctly read the measurements by using putty (VT100+) from terminal.
秤需要接收命令
"READ<CR><LF>"
为了发送测量.每个测量都具有以下格式:
in order to send the measurement. Each measurement has this format:
01ST,GS, 2.5,kg<CR><LF>
例如,如果我测量的是 2.5Kg 的负载.
if, for example, I'm measuring a 2.5Kg load.
现在,我正在尝试从 C 应用程序发送 READ 命令,但我无法得到任何答案.
Now, I'm trying to send the READ command from a C application, but I'm not able to get any answer.
#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);
}
你能帮我吗?谢谢!我是初学者,所以可能只是我看不到的愚蠢错误.
Can you help me, please? Thank you! I'm a beginner, so may be it's just a stupid error I cannot see.
但是,有没有其他更快的方法来获得相同的任务?
However, is there any other faster way to acquire the same task?
推荐答案
可能命令应该以回车符结束(不是你写的换行符):
Probably the command should be terminated with a carriage return (not a linefeed as you have written):
wlen = write(fd, "READ\n", 5);
改为
wlen = write(fd, "READ\r", 5);
如果它真的(它可能,但也可能不是)必须接收 cr lf:
Of if it really (it might, but maybe not) has to receive cr lf:
wlen = write(fd, "READ\r\n", 6);
这篇关于在 Ubuntu 下从串口读取和写入 USB 规模的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!