我知道这个问题散布在互联网上,但是,到目前为止还没有任何东西让我完全在那里.我想将数据写入C++(linux)中的串行端口,用于Propeller板.程序在从控制台获取输入时工作正常,但是当我向其写入字符串时,总是ERROR - Invalid command从设备返回:我尝试char使用Hex值创建数组然后它工作.这是下面的工作代码.但是我怎样才能提供一个字符串变量的命令并将其发送到串口?也许,如果这是唯一的方法,我如何将其转换为十六进制值?感谢大家
注意:循环是使用来自控制台的用户输入.我需要的是一种将字符串变量发送到串行端口的方法.
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
int main(int argc,char** argv){
struct termios tio;
struct termios stdio;
int tty_fd;
fd_set rdset;
unsigned char c='D';
printf("Please start with %s /dev/ttyS1 (for example)\n",argv[0]);
memset(&stdio,0,sizeof(stdio));
stdio.c_iflag=0;
stdio.c_oflag=0;
stdio.c_cflag=0;
stdio.c_lflag=0;
stdio.c_cc[VMIN]=1;
stdio.c_cc[VTIME]=0;
tcsetattr(STDOUT_FILENO,TCSANOW,&stdio);
tcsetattr(STDOUT_FILENO,TCSAFLUSH,&stdio);
fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK); // make the reads non-blocking
memset(&tio,0,sizeof(tio));
tio.c_iflag=0;
tio.c_oflag=0;
tio.c_cflag=CS8|CREAD|CLOCAL; // 8n1, see termios.h for more information
tio.c_lflag=0;
tio.c_cc[VMIN]=1;
tio.c_cc[VTIME]=5;
tty_fd=open(argv[1], O_RDWR | O_NONBLOCK); …Run Code Online (Sandbox Code Playgroud) 我正在做一些大数除法(long/long加倍,而int/int浮动).但是当结果包含"E"时,我碰到了问题.我知道我们可以在显示时使用NumberFormat进行格式化,但这不是我的意思.只是希望分区的结果不涉及"E",即只需将其四舍五入到最适合空间的float/double.
有人有个主意吗?