I have a device which takes multiple serial commands. All the commands are of ~similar~ format. command, parameter 1, parameter 2, parameter3.....parameterN
I could write an individual function for each one, but that seems a little redundant. My end goal is to have 1 function, which will take any command and parameters, and send it to the device. If the transmission fails, I would like to inform the user 1.) that it did fail and 2.) what command specifically failed
I also want the function to clear the cmdtrans[] buffer so that I can send another command. As you will see, get_baud() has 1 parameter, and I later set cmdtrans to have 7 parameters, meaning I must modify the serial write buffer dynamically. In anycase, I've been working on this for a while, but for some reason it's not doing what I want. And suggestions from the wise community? Thanks for your help in advance.
uint8_t cmdtrans[]={0x55};
int incomingByte = 0;
int i = 1;
void setup() {
Serial.begin(9600);
get_baud();
delay(3000);
uint8_t cmdtrans[]={0x4C, 0x00, 0x00, 0x7F, 0x7F, 0xFF, 0xFF};
delay(100);
}
void loop() { // send data only when you receive data:
while(i=1){
exe();
i=2;
}
}// end loop
void get_baud(){
delay(5);
exe();
}
void exe(){
Serial.write(cmdtrans, sizeof(cmdtrans));
if (Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.read();
//if (incomingByte = 0x15){
//Serial.print("I received: ");
//Serial.print(sizeof(cmdtrans)); Serial.println(incomingByte, HEX);
//Serial.print(cmdtrans[0], HEX);
//Serial.println(" command failed");//}
}
//clears buffer for next command
uint8_t cmdtrans[] = {'\0'};
}