#!/usr/bin/env python import sys if len(sys.argv)<2: command = "04DI " else: command = sys.argv[1] print("Command:",command) s = 0x0 for c in command: s = s - (ord(c)) ubyte = (s & (0b11110000)) >> 4 lbyte = s & 0b00001111 ubyteChar = hex(ubyte).replace('0x','').upper() lbyteChar = hex(lbyte).replace('0x','').upper() print("Checksum:",hex(ord(ubyteChar)),hex(ord(lbyteChar))) command_full = [] command_full.append(hex(0x02)) for c in command: command_full.append(hex(ord(c))) command_full.append(hex(ord(ubyteChar))) command_full.append(hex(ord(lbyteChar))) command_full.append(hex(0x03)) print("whole command:",command_full)
実行
./calc.py
Command: 0EPH CH001P2000 Checksum: 0x38 0x35 whole command: ['0x2', '0x30', '0x45', '0x50', '0x48', '0x20', '0x20', '0x43', '0x48', '0x30', '0x30', '0x31', '0x50', '0x32', '0x30', '0x30', '0x30', '0x38', '0x35', '0x3']
CH1を200.0kPaに変更するときの通信プロシージャ(コマンド:0EPH CH001P2000)
0x05送信
- >0x06返ってくる
- >コマンド送信:['0x2', '0x30', '0x45', '0x50', '0x48', '0x20', '0x20', '0x43', '0x48', '0x30', '0x30', '0x31', '0x50', '0x32', '0x30', '0x30', '0x30', '0x38', '0x35', '0x3']
- >stx 0sA02D ext(成功メッセージA0)受信
- >0x04送信(これが必要)
Arduinoコード
#include <SoftwareSerial.h> #define STX 0x02 #define ETX 0x03 #define SPACE 0x20 #define ENQ 0x05 #define ACK 0x06 #define EOT 0x04 SoftwareSerial mySerial(6, 7); //232_TX,232_RX void setup() { Serial.begin(19200); // Serial3.begin(9600); mySerial.begin(19200); delay(1000); // String base_command = "04DI "; // sendMessageBytes(base_command); // String base_command = "0EPH CH001P2000"; // sendMessageBytes(base_command); } void loop() { //message to change pressure (for specific channel) //format: stx 0EPH__CHXXXPXXXX?? etx //Channel: 0-399 //20.0[kPa]-800.0[kPa] //Set channel 1 to 400.0[kPa] for(int i=0;i<4;i++){ int pressure = (i+1)*1000; String pressure_string = String(pressure); String base_command = "0EPH CH001P"; String p = base_command+pressure_string; sendMessageBytes(p); delay(10); } // delay(1000); } void sendMessageBytes(String command_and_data){ int string_length = command_and_data.length(); char buf[string_length+1]; //length+1 (null terminator \0=0x00 in ASCII) command_and_data.toCharArray(buf,string_length+1); byte result = 0x00; for (int i=0;i<string_length;i++){ result = result - buf[i]; } // byte result = 0x00 - 0x30 - 0x34 - 0x44 - 0x49 - 0x20 - 0x20; //-305->two's complement of 305->0b011001111->0xCF byte ubyte = ((result & 0b11110000) >> 4) & 0b11111111; byte lbyte = result & 0b00001111; char ubyteChar[2]; char lbyteChar[2]; sprintf(ubyteChar,"%x",ubyte); sprintf(lbyteChar,"%x",lbyte); mySerial.write(ENQ); delayMicroseconds(100); mySerial.write(STX); mySerial.write(buf,string_length); //do not send null terminator mySerial.print(ubyteChar[0]); mySerial.print(lbyteChar[0]); mySerial.write(ETX); delayMicroseconds(100); mySerial.write(EOT); }
シリアルポートを開いて送信するスクリプトにした
#!/usr/bin/env python import sys import time import serial STX = 0x02 ETX = 0x03 SPACE = 0x20 ENQ = 0x05 ACK = 0x06 EOT = 0x04 CAN = 0x18 if len(sys.argv)<2: command = "04DI " else: command = sys.argv[1] print("Command:",command) s = 0x0 for c in command: s = s - (ord(c)) ubyte = (s & (0b11110000)) >> 4 lbyte = s & 0b00001111 ubyteChar = hex(ubyte).replace('0x','').upper() lbyteChar = hex(lbyte).replace('0x','').upper() print("Checksum:",hex(ord(ubyteChar)),hex(ord(lbyteChar))) command_full = [] command_full_number = [] command_full.append(hex(0x02)) command_full_number.append(0x02) for c in command: command_full.append(hex(ord(c))) command_full_number.append(ord(c)) command_full.append(hex(ord(ubyteChar))) command_full.append(hex(ord(lbyteChar))) command_full.append(hex(0x03)) command_full_number.append(ord(ubyteChar)) command_full_number.append(ord(lbyteChar)) command_full_number.append(0x03) print("whole command:",command_full) ser = serial.Serial('/dev/tty.usbserial',baudrate=38400) enq_array = bytearray([ENQ]) ser.write(enq_array) time.sleep(0.001) command_array= bytearray(command_full_number) ser.write(command_array) time.sleep(0.010) eot_array = bytearray([EOT]) ser.write(eot_array)
使い方
./send.py "0EPH CH001P1010"