Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define DEBUG_SEND 0
- void rs485_send(){};
- void rs485_send(){};
- void rs485_send(byte _id, byte cmd, long top, long btm) {
- digitalWrite(DE, HIGH); // Driver Enable
- delayMicroseconds(tZH); // Driver Enable to Output High (us)
- byte b[12];
- b[0] = 0x7E; // start byte
- b[1] = _id; // id / id() とかぶるので _id とした
- b[2] = cmd; // cmd
- b[3] = (byte)(top >> 24); // top HH
- b[4] = (byte)(top >> 16); // top HL
- b[5] = (byte)(top >> 8); // top LH
- b[6] = (byte)top; // top LL
- b[7] = (byte)(btm >> 24); // btm HH
- b[8] = (byte)(btm >> 16); // btm HL
- b[9] = (byte)(btm >> 8); // btm LH
- b[10] = (byte)btm; // btm LL
- b[11] = 0; // チェックサムが入る箇所を初期化
- for (int i = 0; i < 11; i++) // チェックサム算出 b[1-10] を足し合わせる
- b[11] += b[i];
- rs485.write(b[0]); // start byte
- if (DEBUG_SEND) {
- Serial.println();
- Serial.print(b[0], HEX);
- Serial.print(" ");
- }
- for (int i = 1; i < 12; i++) // b[1-11]
- rs485_write_esc(b[i]); // スタートバイト以外はエスケープ処理を通す
- //rs485.flush(); // rs485送信終了を待ってからDEをおろさないと送信が途中で切れる
- delayMicroseconds(tPLH); // Driver Input to Output (us)
- digitalWrite(DE, LOW);
- }
- void rs485_write_esc(byte b) {
- if (b == 0x7E) { // スタートバイト 0x7E が来た場合は
- rs485.write(0x7D); // 0x7D を送ってから
- rs485.write(b ^ 0x20); // 送信したいバイトと 0x20 の AND を送る
- if (DEBUG_SEND) {
- Serial.print(0x7D, HEX);
- Serial.print(" ");
- Serial.print(b ^ 0x20, HEX);
- Serial.print(" ");
- }
- }
- else {
- rs485.write(b); // 0x7E 以外の場合はそのまま送信
- if (DEBUG_SEND) {
- Serial.print(b, HEX);
- Serial.print(" ");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement