приветствую, имею проблему с чтением по serial, вначале считывается все нормально потом после нескольких команд чтение прекратилось и стало Serial.available() = 0 всегда, и после сброса, и после перепрошивки, хотя светодиод rx моргает при отправке. Менял плату и проблема повторилась, сначала все успешно считывалось, но спустя несколько команд (15-20 команд) проблема повторилась, не пойму в чем может быть дело и сталкивался ли кто-нибудь с подобной ситуацией?
void InstrumentRunner::init(){
timeout = 2000;
debug.begin(9600);
initTimer(); //init timer _micros() 0.1 ms period
modbus.init(bodRate, SERIAL_8O1, timeout); //9600
updateTimestamp();
sei();
}
void InstrumentRunner::run(){
//debug.println("check");
static uint64_t timer = 0;
static uint8_t state = 0;
readSensors(); //reading sensors
//modbus rtu automat
if(state == 0 && modbus.bufferEmpty() == false && _micros() - timer > T240){ //+ timing t240 after sending prev answer ot master
modbus.poll(); //start reading
debug.println("1");
state++;
timer = _micros(); //remember time after success reading
}
if(state == 1){ //if input command is validated
uint8_t status = modbus.validate();
switch (status)
{
case 0:
modbus.defineAndExecute(); //define make operation for current command WriteCommand, ReadCommand,
state++;
debug.println("valided");
break;
case 1:
state = 0;
debug.println("command not for this device");
break;
case 2:
state = 0;
debug.println("crc err");
break;
}
}
if(state == 2 && _micros() - timer > (T35 + interval)){ //timing t35 + interval
modbus.query(); // send answer to master device
debug.println("send");
state = 0;
timer = _micros(); //remember time after success sending
}
}
void Modbus::init(uint16_t bodRate, uint8_t type, uint16_t timeout){
Serial.begin(bodRate, type);
while (!Serial);
Serial.setTimeout(timeout);
if (type == SERIAL_8E2 or type == SERIAL_8O2) {
_charTimeout = 180000/bodRate;
_frameTimeout = 420000/bodRate;
}
else if (type == SERIAL_8N2 or type == SERIAL_8E1 or type == SERIAL_8O1) {
_charTimeout = 165000/bodRate;
_frameTimeout = 385000/bodRate;
}
else {
_charTimeout = 150000/bodRate;
_frameTimeout = 350000/bodRate;
}
}
bool Modbus::bufferEmpty(){
if(Serial.available() > 0){
return false;
}
return true;
}
void Modbus::poll(){
uint32_t tm = _micros();
uint8_t i = 0;
while(_micros() - tm < _charTimeout){ //testing 03 03 00 08 00 01 04 2A
if(Serial.available()){
tm = _micros();
bufferInput[i] = Serial.read();
i++;
}
}
}
uint8_t Modbus::validate(){
// for (uint8_t i = 0; i < 8; i++) //check recived command
// {
// Serial.write(bufferInput[i]);
// }
// Serial.flush();
if(bufferInput[0] != id){ // id no equals in query with slave id address
clearBufferInput();
return 1; //query for not this slave device
}
if(!checkCrc()){
clearBufferInput();
return 2; //data damaged
}
return 0;
}
void Modbus::clearBufferInput(){ //clean buffer
for (uint8_t i = 0; i < 64; i++)
{
bufferInput[i] = 0;
}
}