目的:測試Arduino 與VFD RS485通訊
系統架構
配線(使用RJ11電話線)
RJ11腳位定義
變頻器通訊位址
程式
/*
Arduino RS232(RS485) and Delta VFD-S(變頻器)
*/
byte inByte[50] ; // 用來儲存收進來的 data byte
int data[10]; // 儲存 data
//read HZ, NO:10
// command : "0A03210200012E8D"
byte cmd[]={0x0A,0x03,0x21,0x02,0x00,0x01,0x2E,0x8D};
int i=0;
boolean once = false;
void setup() // run once, when the sketch starts
{
Serial.begin(9600);
Serial1.begin(9600,SERIAL_8E1); // Modbus RTU 9600,8,E,1
}
void loop()
{
if(!once){
Serial.println("Arduino --> VFD");
for(i=0;i<sizeof(cmd);i++){ //for PC Monitor
Serial.print(cmd[i],HEX);
Serial.print(" ");
}
Serial.println(" ");
Serial.println(" ");
Serial1.write(cmd,sizeof(cmd));
delay(100);
// response
i=0;
while (Serial1.available()) {
inByte[i++] = Serial1.read();
}
int len = i;
//Serial.println(len);
Serial.println("VFD --> Arduino");
for(i=0;i<len;i++){ //for PC Monitor
Serial.print(inByte[i],HEX);
Serial.print(" ");
}
Serial.println(" ");
//get slave no., func code, and data length
Serial.print("slave no.: ");
Serial.println(inByte[0]);
Serial.print("func code: ");
Serial.println(inByte[1]);
Serial.print("data length(byte): ");
Serial.println(inByte[2]);
// get data
for(int i=0 ; i< inByte[2] ; i+=2){
data[i/2] = (int) ((inByte[3+i] << 8) + inByte[3+i+1]);
}
for(int i=0 ; i< inByte[2] ; i+=2){
Serial.println(data[i/2]);
}
once = true;
}//end once
}
沒有留言:
張貼留言