我試圖通過三個參數來我的write()函數:C++常量無效*轉換
write(fd, "C,1,1\r\n", 7);
這工作得很好。不過,我想帶一個參數,並把它傳遞給命令部分,使其動態:
write(fd, N, 4);
我不熟悉C++的類型,但它一直要求一種類型的「常量無效*」我已經能夠將我的變量N轉換爲兩種不同的格式,希望能夠更容易地轉換。這是我曾嘗試:
const fmx::FixPt& outValue = dataVect.AtAsNumber(3);
const double N = outValue.AsLong();
double N = outValue.AsLong();
所以雙和const雙(*這可能是幾乎同樣的事情......我不知道C++非常好)
我也可以把它作爲剛:
const fmx::FixPt& outValue = dataVect.AtAsNumber(3);
write(fd, outValue, 4);
但我想呼籲大家如何轉換雙會比試圖解釋或弄清楚獨一無二的,因爲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' 至 'ssize_t供寫入(INT,常量無效*,爲size_t)'
你究竟在做什麼?根據你寫的內容很難分辨出來,根據你的最終目標是什麼,答案可能會有所不同。 – 2009-11-04 20:16:37
請注意結尾的第三行:「只能刪除我的錯誤,但仍然不起作用」。你是什麼意思「不起作用」?你想達到什麼目的? – 2009-11-04 20:37:38
對不起每個人,有時我覺得如果我提供了太多的信息,那麼我只會混淆這個問題。 我正在爲FileMaker寫一個插件,它將打開一個串口並將命令發送到軌道設備,如C,7 C,1,1等......除了我需要命令C,3,X這將需要1-1000的數字。我想傳遞那個數字。之前我只是在「3000」或其他什麼,但我需要通過變量硬編碼。當我編譯它給我我的轉換錯誤,但是當我添加「&」時沒有錯誤,但是當我運行腳本時,軌道/掃描器設備什麼也不做。 – Pfeffer 2009-11-04 21:59:59