Instantly share code, notes, and snippets.
Last active
April 23, 2023 19:34
-
Save LaudixGit/e79337032cf148c674c828b5d251eae4 to your computer and use it in GitHub Desktop.
Use a menu to communicate with the Drok power supply unit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Use a menu to communicate with the Drok power supplyu unit | |
*/ | |
//https://esp32s3.com/tinys3.html | |
//https://broadwellconsultinginc.github.io/SerialWombatArdLib/ | |
//https://www.amazon.com/dp/B08LPVWX74 | |
//https://github.com/bengineer19/BuckPSU | |
//https://www.droking.com/dc-power-supply/dc-buck/Power-Supply-Module-DC10V-75V-to-0-60V-12A-720W-Buck-Converter-Voltage-regulator-CNC-Control-Module-DC-12V-24V-36V-48V-Adapter | |
//https://www.droking.com/cs/support/topic/200310-dc-dc-buck-converter-uart/ | |
//https://www.droking.com/cs/wp-content/uploads/2022/09/200310-communicate.pdf | |
//https://www.instructables.com/Control-Power-Supply-Via-UART/ | |
// on the Drok control panel ensure the communication address (under F3) is set to the default address -00-. | |
#define RXD2 4 | |
#define TXD2 5 | |
#define txMaxLength 17 // ex: longest string "awu00001234DA" address "a" "w"rite output voltage is set to 12.34V terminate with 0x0d 0x0a | |
#define rxMaxLength 17 // ex: longest string "#ra00000000125DA" current set point value 1.25A | |
char txCommand[][txMaxLength] = { | |
{"awo00\n"}, // 0 disable output | |
{"awo01\r\n"}, // 1 enable output | |
{"aro\r\n"}, // 2 read output status | |
{"aru\r\n"}, // 3 read actual voltage | |
{"arv\r\n"}, // 4 read voltage set point value | |
{"awu1234\n"}, // 5 limit voltage (set point value) to 12.34V | |
{"awu0123\n"}, // 6 limit voltage (set point value) to 1.23V | |
{"awu0012\n"}, // 7 limit voltage (set point value) to .12V | |
{"ari\r\n"}, // 8 read actual current | |
{"ara\r\n"}, // 9 read current set point value | |
{"awi0211\n"}, //10 limit current (set point value) to 2.11A | |
{"awi0021\n"}, //11 limit current (set point value) to .21A | |
{"awm0\n"}, //12 load the settings stored in memory slot 0 (these are used at power-on) | |
{"awm8\n"}, //13 load the settings stored in memory slot 8 | |
{"aws8\n"}, //14 save the existing the settings into memory slot 8 | |
{"ary\r\n"}, //15 read the power-on-reboot setting | |
{"awy0\n"}, //16 disable power-on-reboot | |
{"awy1\n"}, //17 enable power-on-reboot | |
{"art\r\n"}, //18 read the run time (minutes) | |
{"arc\r\n"}, //19 read the output capacity (AH) | |
{"awd00\r\n"}, //20 disable LCD Display | |
{"awd01\n"}, //21 enable LCD Display | |
}; | |
void setup() { | |
Serial.begin(115200); | |
Serial.println("\n"); | |
Serial.print( F("Compiled: ")); //https://forum.arduino.cc/t/displaying-date-time-and-ide-compiler-version-in-serial-monitor/154290/3 | |
Serial.print( F(__DATE__)); | |
Serial.print( F(", ")); | |
Serial.print( F(__TIME__)); | |
Serial.print( F(", ")); | |
Serial.println( F(__VERSION__)); | |
Serial.print( F("From: ")); | |
Serial.println( F(__FILE__)); | |
Serial.println("Initialising PSU..."); | |
Serial2.begin(9600, SERIAL_8N1, RXD2, TXD2); | |
if (Serial2.read() != -1){ | |
Serial.println("Flush"); | |
while (Serial2.read() != -1){delay(10);} // Flush rx, remove residual buffer content | |
} | |
} | |
void displaymsg(char msg[], int len){ | |
for (int i=0; i<len; i++){ | |
switch (msg[i]) { | |
case 0: | |
//end of msg, do nothing | |
break; | |
case 32 ... 126: | |
// printable characters | |
Serial.print(msg[i]); | |
break; | |
default: | |
// show ascii code | |
Serial.print("{"); | |
Serial.print(msg[i], HEX); | |
Serial.print("}"); | |
break; | |
} | |
} | |
Serial.println(); | |
} | |
void loop() { | |
int menuLength = 3; | |
char menuChoice[menuLength] = {0}; //initialize array with zeros each time through the loop to avoid residual content | |
char rxResponse[rxMaxLength] = {0}; | |
if (Serial.available()) { | |
int byteCount = Serial.readBytes(menuChoice, menuLength); //retrieve input from the terminal | |
int menuOption = atoi(&menuChoice[0]); | |
Serial.print("sent(");Serial.print(menuOption);Serial.print("): "); | |
displaymsg(txCommand[menuOption], strlen(txCommand[menuOption])); //write to terminal | |
Serial2.write(txCommand[menuOption], strlen(txCommand[menuOption])); // write to the UART/Drok | |
} | |
if (Serial2.available()) { | |
int byteCount = Serial2.readBytes(rxResponse, rxMaxLength); //retrieve command from the terminal | |
Serial.print("rcvd: "); | |
displaymsg(rxResponse, rxMaxLength); //write to terminal | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment