前提・実現したいこと
ArduinoでPCからのSerial信号を受信したい。
受信したSerial信号の文字列を分割して、
CAN通信のサービスID、データ長、データとしてCAN通信したい。
ここで、知りたいのは分割した文字をキャストする方法です。
tx:130h,8,FE,3F,2F,88,23,23,44,FF
※ tx:までは固定で、130hは0x0h~0x7FFhまでの”0x”を省略したデータで、
8はデータ長で1~8となっています。
以降はデータで1byte×データ長のデータで、すべてコンマ区切りになています。
文字を分割することはできそうですが、その際に文字をキャストする部分でエラーになります。
この場合だと、(tx:130h,8,FE,3F,2F,88,23,23,44,FFはString)
以下のようになっていればいいようと思っています。
CANtxId = 0x130;
len = 8;
txBuf[8] = {0xFE,0x3F,0x2F,0x88,0x23,0x23,0x44,0xFF}
やったこと
以下のようにスクリプトを作成しました。
定義部
long unsigned int CANrxId, CANtxId; unsigned char len = 0; unsigned char rxBuf[8],txBuf[8]; String txData, charID, charData; char msgString[128]; // Array to store serial string int i = 0, i1 = 0, i2 = 0;
文字の分割処理部抜粋
i1 = txData.indexOf(","); charID = "0x"; charID = charID.concat(txData.substring(3,i1-2)); CANtxId = (long unsigned int)charID; i2 = txData.indexOf(",", i1+1); len = (unsigned char*)txData(i2-1); i1 = i2; while (i2 < 0){ i2 = txData.indexOf(",", i1+1); charData = "0x"; charData = charData.concat(txData.substring(i1+1, i2-1)); txBuf[i] = (byte)charData; i++; i1 = i2; }
発生している問題・エラーメッセージ
D:\Arduino\CAN_Box3\CAN_Box3.ino: In function 'void loop()': D:\Arduino\CAN_Box3\CAN_Box3.ino:39:39: warning: invalid conversion from 'const char*' to 'char' [-fpermissive] txData = Serial.readStringUntil("\n"); ^ In file included from C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/HardwareSerial.h:29:0, from C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Arduino.h:232, from C:\Users\�_�a\AppData\Local\Temp\arduino_build_334818\sketch\CAN_Box3.ino.cpp:1: C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Stream.h:108:10: note: initializing argument 1 of 'String Stream::readStringUntil(char)' String readStringUntil(char terminator); ^ CAN_Box3:45:32: error: invalid cast from type 'String' to type 'long unsigned int' CANtxId = (long unsigned int)charID; ^ CAN_Box3:47:36: error: no match for call to '(String) (int)' len = (unsigned char*)txData(i2-1); ^ CAN_Box3:53:22: error: invalid cast from type 'String' to type 'byte {aka unsigned char}' txBuf[i] = (byte)charData; ^ 次のフォルダのライブラリMCP_CAN_lib-masterを使用中:D:\Arduino\libraries\MCP_CAN_lib-master (legacy) 次のフォルダのライブラリSPIバージョン1.0を使用中:C:\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\SPI exit status 1 invalid cast from type 'String' to type 'long unsigned int'
補足情報(FW/ツールのバージョンなど)
Windows10、
Arduino1.8.9

回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/08/16 07:15
2019/08/16 07:23
2019/08/16 23:50
2019/08/17 02:28
2019/08/17 02:31
2019/08/17 08:07