2016-08-20 92 views
0

使用帶有LoRa無線電的Adafruit Feather M0板卡,我想將GPS位置發送到接收器。當試圖創建一個ISO 8601時間戳和我使用下面的代碼來創建一個char數組,然後經/緯GPS值的數據包發送:類型爲'const char *'和'const char [2]'的無效操作數爲二元運算符+'

char radiopacket[40] = {GPS.year + "-" + GPS.month + "-" + GPS.day + "T" + GPS.hour + ":" + GPS.minute + ":" + GPS.seconds + "Z" + "," + GPS.latitude + "," + GPS.longitude}; 
rf95.send((uint8_t *)radiopacket, 40); 

我不斷收到錯誤:

invalid operands of types 'const char*' and 'const char [2]' to binary 'operator+'

我哪裏錯了?

回答

0

不能連接這樣的字符串C.試着這麼做

char radiopacket[40]; 
sprintf(radiopacket, "%04d-%02d-%02dT%02d:%02d:%02dZ,%f,%f", GPS.year, GPS.month, GPS.day, GPS.hour, GPS.minute, GPS.seconds, GPS.latitude, GPS.longitude); 
rf95.send((uint8_t *)radiopacket, 40); 

看到here爲內sprintf

+1

'sprintf'是不安全的,請使用'snprintf'代替,見[這裏](https://stackoverflow.com/a/3662918/2666212) – Mike

+0

謝謝!這一直奏效 –

0

的格式字符串("%04d-...")一些文檔我要去猜測並說你是從python來的。

我想你需要的是 std::stringstream

相關問題