【发布时间】:2009-11-04 20:10:59
【问题描述】:
我正在尝试将三个参数传递给我的 write() 函数:
write(fd, "C,1,1\r\n", 7);
这很好用。但我想取一个参数并将其传递给命令部分以使其动态:
write(fd, N, 4);
我不熟悉 c++ 类型,但它一直要求一种“const void*”类型,我已经能够将我的变量 N 转换为几种不同的格式,希望其中一种更容易转换。这是我尝试过的:
const fmx::FixPt& outValue = dataVect.AtAsNumber(3);
const double N = outValue.AsLong();
double N = outValue.AsLong();
所以 double 和 const double(*这可能几乎是一回事...我不太了解 c++)
我也可以保持原样:
const fmx::FixPt& outValue = dataVect.AtAsNumber(3);
write(fd, outValue, 4);
但我认为询问每个人如何转换双精度值比试图解释或找出像 type const fmx::FixPt&... 这样独特的东西要好得多...
*我也试过了:
write(fd, &N, 4);
这只是摆脱了我的错误,但仍然不起作用。
那么,是否甚至可以转换为“const void*”类型?
非常感谢!
代码如下:
const fmx::FixPt& outValue = dataVect.AtAsNumber(3);
double N = outValue.AsLong();
int fd;
struct termios options;
fd=open("/dev/tty.KeySerial1", O_RDWR | O_NOCTTY | O_NDELAY);
fcntl(fd, F_SETFL, 0);
tcgetattr(fd,&options);
options.c_ispeed=57600;
options.c_ospeed=57600;
options.c_cflag |= (CLOCAL | CREAD);
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
options.c_cflag &= ~CSTOPB;
options.c_lflag &= ~ECHO;
options.c_oflag &= ~ECHO;
options.c_oflag &= ~OPOST;
options.c_cflag |= CS8;
options.c_cflag |= CRTSCTS;
options.c_cc[VMIN] = 0;
options.c_cc[VTIME] =10;
tcflush(fd, TCIFLUSH);
tcsetattr(fd,TCSANOW,&options);
if( tempText->Assign("2"), *tempText == direction ) {
write(fd, "C,1,2\r\n", 7);//Direction
}else{
write(fd, "C,1,1\r\n", 7);//Direction
}
if( tempText->Assign("1"), *tempText == speed ) {
write(fd, "C,2,1\r\n", 7);//Speed
} else if( tempText->Assign("2"), *tempText == speed ) {
write(fd, "C,2,2\r\n", 7);//Speed
} else if( tempText->Assign("3"), *tempText == speed ) {
write(fd, "C,2,3\r\n", 7);//Speed
} else if( tempText->Assign("4"), *tempText == speed ) {
write(fd, "C,2,4\r\n", 7);//Speed
} else if( tempText->Assign("5"), *tempText == speed ) {
write(fd, "C,2,5\r\n", 7);//Speed
} else if( tempText->Assign("6"), *tempText == speed ) {
write(fd, "C,2,6\r\n", 7);//Speed
} else if( tempText->Assign("7"), *tempText == speed ) {
write(fd, "C,2,7\r\n", 7);//Speed
} else if( tempText->Assign("8"), *tempText == speed ) {
write(fd, "C,2,8\r\n", 7);//Speed
} else if( tempText->Assign("9"), *tempText == speed ) {
write(fd, "C,2,9\r\n", 7);//Speed
} else if( tempText->Assign("10"), *tempText == speed ) {
write(fd, "C,2,10\r\n", 8);//Speed
} else if( tempText->Assign("11"), *tempText == speed ) {
write(fd, "C,2,11\r\n", 8);//Speed
} else if( tempText->Assign("12"), *tempText == speed ) {
write(fd, "C,2,12\r\n", 8);//Speed
} else if( tempText->Assign("13"), *tempText == speed ) {
write(fd, "C,2,13\r\n", 8);//Speed
} else if( tempText->Assign("14"), *tempText == speed ) {
write(fd, "C,2,14\r\n", 8);//Speed
} else if( tempText->Assign("15"), *tempText == speed ) {
write(fd, "C,2,15\r\n", 8);//Speed
} else if( tempText->Assign("16"), *tempText == speed ) {
write(fd, "C,2,16\r\n", 8);//Speed
} else if( tempText->Assign("17"), *tempText == speed ) {
write(fd, "C,2,17\r\n", 8);//Speed
}
if(tempText->Assign("1"), *tempText == length){
write(fd, "C,3,", 4);
write(fd, "1", 1);
write(fd, "\r\n", 2);
} else if(tempText->Assign("2"), *tempText == length){
write(fd, "C,3,", 4);
write(fd, "10", 2);
write(fd, "\r\n", 2);
} else if(tempText->Assign("3"), *tempText == length){
write(fd, "C,3,", 4);
write(fd, "100", 3);
write(fd, "\r\n", 2);
} else if(tempText->Assign("4"), *tempText == length){
write(fd, "C,3,", 4);
write(fd, N, 4);
write(fd, "\r\n", 2);
}
close(fd);
错误:无法将参数 '2' 的 'double' 转换为 'const void*' 到 'ssize_t write(int, const void*, size_t)'
【问题讨论】:
-
你到底想做什么?从你写的内容很难判断,答案可能会有所不同,具体取决于你的最终目标。
-
请注意倒数第三行:“这只会消除我的错误,但仍然无法正常工作”。你是什么意思“不起作用”?你想完成什么?
-
对不起大家,有时我觉得如果我提供太多信息就会混淆问题。我正在为 FileMaker 编写一个插件,它将打开一个串行端口并将命令发送到跟踪设备,如 C、7 C、1、1 等......除了我需要命令 C、3、X 之外,一切正常取一个 1-10000 的数字。我想把那个号码传给它。在我只是硬编码“3000”或其他但我需要传递变量之前。当我编译它给我我的转换错误但是当我添加“&”时没有错误但是当我运行脚本时跟踪/扫描设备什么都不做。