@gfvalvo @Delta_G Thank you, I incorporated your recommendations. Especially the fixed write length seems to have made the difference. I tried with a string as well as my previously declared as hex chars character array, both work. I preferred using the hex notation as it felt easier for me to expand it to use control characters which are less common later in case I want to expand it to projector models which use a more exotic control set.
I declared them global in the notation that I use less sram if I declare them static global, so that they are instanciated only once in total. The global namespace was kind of a side effect, so I modified it along your recommendations. For other people, this is the final working code (cycling the projector once as soon as its available):
ino:
#include "Projector_wrapper.h"
#include <string.h>
Projector_wrapper proj(1);
void setup() {
proj.begin_serial();
pinMode(LED_BUILTIN, OUTPUT);
bool blink = false;
//Serial.begin(9600);
while (!proj.exists()) {
delay(1000);
if(blink) {digitalWrite(LED_BUILTIN, HIGH);}
else {digitalWrite(LED_BUILTIN, LOW);}
blink = !blink;
//Serial.println(proj.get_buffer());
}
proj.proj_pwr_on();
digitalWrite(LED_BUILTIN, HIGH);
while (!proj.ready_for_signal()) {
delay(50);
}
delay(5000);
proj.proj_pwr_off();
digitalWrite(LED_BUILTIN, LOW);
}
void loop() {
// put your main code here, to run repeatedly:
}
Projector_wrapper.h:
#ifndef Projector_wrapper_h
#define Projector_wrapper_h
#define buffer_size 18
#include <Arduino.h>
/*const static String cmd_pwr_on = "~0000 1\r";
const static String cmd_pwr_off = "~0000 0\r";
const static String cmd_pwr_stat = "~00124 1\r";
const static String cmd_info_stat = "~00150 1\r";*/
/*const static char cmd_pwr_on[] = {0x7E,0x30,0x30,0x30,0x30,0x20,0x31,0x0d};
const static char cmd_pwr_off[] = {0x7E,0x30,0x30,0x30,0x30,0x20,0x30,0x0d};
const static char cmd_pwr_stat[] = {0x7E,0x30,0x30,0x31,0x32,0x34,0x20,0x31,0x0d};
const static char cmd_info_stat[] = {0x7E,0x30,0x30,0x31,0x35,0x30,0x20,0x31,0x0d};*/
class Projector_wrapper{
public:
Projector_wrapper(uint8_t x) : portnr(x){};
bool proj_pwr_on();
bool proj_pwr_off();
bool ready_for_signal();
bool exists();
String get_buffer();
void begin_serial();
private:
void read_into_rxbuffer();
void clear_rxbuffer();
//void write_word(String &txword);
HardwareSerial* projector = nullptr;
String rxbuffer = "";
char txbuffer[buffer_size] = "";
unsigned int lamp_hours = 0;
uint8_t portnr = 0;
/*static constexpr char cmd_pwr_on[] = "~0000 1\r";
static constexpr char cmd_pwr_off[] = "~0000 0\r";
static constexpr char cmd_pwr_stat[] = "~00124 1\r";
static constexpr char cmd_info_stat[] = "~00150 1\r";*/
constexpr static char cmd_pwr_on[] = {0x7E,0x30,0x30,0x30,0x30,0x20,0x31,0x0d};
constexpr static char cmd_pwr_off[] = {0x7E,0x30,0x30,0x30,0x30,0x20,0x30,0x0d};
constexpr static char cmd_pwr_stat[] = {0x7E,0x30,0x30,0x31,0x32,0x34,0x20,0x31,0x0d};
constexpr static char cmd_info_stat[] = {0x7E,0x30,0x30,0x31,0x35,0x30,0x20,0x31,0x0d};
};
#endif
Projector_wrapper.cpp:
#include "Projector_wrapper.h"
constexpr char Projector_wrapper::cmd_pwr_on[];
constexpr char Projector_wrapper::cmd_pwr_off[];
constexpr char Projector_wrapper::cmd_pwr_stat[];
constexpr char Projector_wrapper::cmd_info_stat[];
void Projector_wrapper::begin_serial(){
switch (portnr){
case 1:
Serial1.begin(9600);
projector = &Serial1;
break;
case 2:
Serial2.begin(9600);
projector = &Serial2;
break;
case 3:
Serial3.begin(9600);
projector = &Serial3;
break;
}
}
/*void Projector_wrapper::write_word(String &txword){
//txbuffer = txword.c_str();
for(char x : txword.c_str()){
(*projector).write(x);
}
}*/
void Projector_wrapper::clear_rxbuffer(){
rxbuffer = "";
}
void Projector_wrapper::read_into_rxbuffer(){
clear_rxbuffer();
while((*projector).available()){
rxbuffer.concat((char)(*projector).read());
}
}
bool Projector_wrapper::proj_pwr_on(){
(*projector).write(cmd_pwr_on,8);
delay(50);
read_into_rxbuffer();
if (rxbuffer[0] == 'P'){
return true;
}
return false;
}
bool Projector_wrapper::proj_pwr_off(){
(*projector).write(cmd_pwr_off,8);
delay(50);
read_into_rxbuffer();
if(rxbuffer[0]=='P'){
return true;
}
return false;
}
bool Projector_wrapper::ready_for_signal(){
(*projector).write(cmd_info_stat,9);
delay(50);
read_into_rxbuffer();
if(rxbuffer[2]=='1'){
return true;
}
return false;
}
bool Projector_wrapper::exists() {
(*projector).write(cmd_info_stat,9);
delay(50);
read_into_rxbuffer();
if (rxbuffer[0] == 'O') {
return true;
}
return false;
}
String Projector_wrapper::get_buffer(){
while ((*projector).available()) {
rxbuffer.concat((char)(*projector).read());
}
return(rxbuffer);
}
Thank you!