我正试图通过C读取我的覆盆子pi上的arduino的串行数据 . 当我尝试运行我的代码时,第一个值得到的读取正常,但在第二个值我得到0到255之间的各种奇怪的数字 . 我尝试了我的macbook上的代码,它工作正常 .
我在覆盆子pi 2b上运行我的代码,arduino通过usb连接 .
Arduino代码:
unsigned char pots[2];
void setup() {
Serial.begin(9600);
}
void loop() {
pots[0] = analogRead(0) / 8 | 128;
pots[1] = analogRead(1) / 8;
Serial.write(pots,2);
delay(1);
}
C代码:
FILE *file;
unsigned char doorspoelChar;
const char *devicename="/dev/cu.usbmodem1421";
int potParams[2];
int main()
{
file=fopen(devicename,"r");
if(file==NULL){
printf("Unable to open device %s\n",devicename);
exit(1);
}
do{
do{
fread(&doorspoelChar,1,1,file);
} while (doorspoelChar < 128);
potParams[0] = doorspoelChar & 127;
fread(&doorspoelChar,1,1,file);
potParams[1] = doorspoelChar ;
fread(&doorspoelChar,1,1,file);
std::cout << potParams[0] << " " << potParams[1] << "\n";
}while(1);
}