【发布时间】:2018-05-11 22:05:16
【问题描述】:
UPDATE根据TNTFreaks指令回答。
我定义了一个 char 变量数据,如下所示:
#define CMD_LEN 4
char data[CMD_LEN + 1];
float n1;
# I pass the data variable to `serial_read` function
n = serial_read(serial_fd, data, CMD_LEN, TIMEOUT);
# Here, the process goes to serial_read function more below and
# after it return here again to follow ...
std::cout << "Data brought from serial_read method " << data << std::endl;
flush = tcflush(serial_fd, TCIOFLUSH);
//n = n*0.01f;
cout << "Applying sscanf " << std::endl;
sscanf(data, "%f", &n1);
printf("%.3f" "%s", n1, " ");
n1 = atof(data) * 0.5f;
printf("%.3f", n1);
cout << "n1 value which have data turn it " << n1 << std::endl;
当编译器检查serial_read函数时,进入这个过程是:
*注意:我键入此 serial_read 函数仅用于说明与工作流程相关的工作流程,该工作流程遵循我关于 data 变量的问题 *
int SerialDriver::serial_read(int serial_fd, char *data, int size, int timeout_usec)
{
std::cout << "Enter to serial_read method " << std::endl;
fd_set fds;
struct timeval timeout;
bool band = false;
int count = 0;
int ret;
int n;
//-- Wait for the data. A block of size bytes is expected to arrive
//-- within the timeout_usec time. This block can be received as
//-- smaller blocks.
do
{
//-- Set the fds variable to wait for the serial descriptor
FD_ZERO(&fds);
FD_SET(serial_fd, &fds);
//-- Set the timeout in usec.
timeout.tv_sec = 0;
timeout.tv_usec = timeout_usec;
// std::cout << "timeouts establecidos " << std::endl;
//-- Wait for the data
ret = select(FD_SETSIZE, &fds, NULL, NULL, &timeout);
//-- If there are data waiting: read it
if (ret == 1)
{
//-- Read the data (n bytes)
n = read(serial_fd, &data[count], 1);
// read viene de sofa/src/applications/plugins/SofaPML/argumentParser.h
std::cout << "Enter to read method to read hapkit data via serial port " << std::endl;
if (band)
{
if (data[count] != ' ')
{
if (data[count] == '\n')
{
data[count] = '\0';
return count;
}
//-- The number of bytes receives is increased in n
count += n;
}
}
if (!band && data[count] == '\n')
{
band = true;
}
//-- The last byte is always a 0 (for printing the string data)
data[count] = 0;
}
std::cout << "band value: " << band << " and data: " << data << std::endl;
//-- Repeat the loop until a data block of size bytes is received or
//-- a timeout occurs
} while (count < size && ret == 1);
//-- Return the number of bytes reads. 0 If a timeout has occurred.
std::cout << "Leaving serial_read method " << std::endl;
std::cout << "count value " << count << std::endl;
return count;
}
当serial_read 函数完成时,data 被返回并具有 char 值,例如 1.86
我想使用atof() 将其转换为浮动,例如我在问题的开头代码处键入代码部分并将结果存储在 n1 变量中
但我得到的输出是:
Data brought from serial_read method 1.86
Applying sscanf
1,000 0,500n1 value which have data turn it 0.5
Serial Driver draw n1: 0.5 1
1,000
1,000 值是 data 值,我得到的值为 1.86,但 atof 删除小数部分或不包括浮点数据。
总而言之,我可能没有很好地将参数传递给atof 函数(我的n1 变量是 float )。
关于sscanf 函数,我将&n1 传递给sscanf,就像数据的引用参数一样,用atof 进行转换,但这不起作用
我得到相同的结果。 我在想这是可能的,虽然我理解TNTFreaks 的说明我没有以正确的方式应用它?
我做错了什么?
【问题讨论】:
-
char data="6.35";不会编译。不清楚你在问什么。 -
问题在于您的语言环境。见stackoverflow.com/questions/13919817/…
-
您确定将代码正确放入问题中吗?无论我对代码做什么,我都会遇到编译错误:godbolt.org/g/GQ86Nk
-
大多数实现还不支持它,但与语言环境无关的解析现在是 C++17 标准库的一部分:en.cppreference.com/w/cpp/utility/from_chars。
标签: c++ type-conversion atof