Hi all, \o.
Consider an AVR based modbus rtu chain. Master always call them by a 8 byte query each time. Therefore rx buffer size was set to 8 bytes in codewizard. But in response condition a slave might send 17, 33 or 69 bytes. Odd numbers cause rx_buffer[n] variable (made by codewizard) several times and lock to take further bytes therefore wrong-reading.
How can I dispose/clear the step of rx_buffer[n] variable and the value it contains in the RIGHT way?
My approach was to take first byte (slave address) {without comparing CRC checksums at the end of the stream} to check if the address is not mine, terminate the UART interrupt register:
...INSIDE OF CODEWIZARD UART RX INTERRUPT... //TEMPORARY DISABLING USART FOR REDUCING INTERFERENCE UCSR1B=(1<<RXCIE1) | (0<<TXCIE1) | (0<<UDRIE1) | (1<<RXEN1) | (1<<TXEN1) | (0<<UCSZ12) | (0<<RXB81) | (0<<TXB81); // Clock value: 172.800 kHz // Timer Period: 0.37926 s // Timer1 Overflow Interrupt: On TCCR1B=(0<<ICNC1) | (0<<ICES1) | (0<<WGM13) | (0<<WGM12) | (0<<CS12) | (1<<CS11) | (1<<CS10);
Then turn on a timer and after calculated milliseconds re-enable corresponding UART interrupt register:
//RE-ENABLE USART SETTINGS interrupt [TIM1_OVF] void timer1_ovf_isr(void) { //ENABLE USART UCSR1B=(0<<RXCIE1) | (0<<TXCIE1) | (0<<UDRIE1) | (1<<RXEN1) | (1<<TXEN1) | (0<<UCSZ12) | (0<<RXB81) | (0<<TXB81); //DISABLE TIMER AGAIN TCCR1A=(0<<COM1A1) | (0<<COM1A0) | (0<<COM1B1) | (0<<COM1B0) | (0<<COM1C1) | (0<<COM1C0) | (0<<WGM11) | (0<<WGM10); TCCR1B=(0<<ICNC1) | (0<<ICES1) | (0<<WGM13) | (0<<WGM12) | (0<<CS12) | (0<<CS11) | (0<<CS10); }
Long story short: Hang up the phone, then pick again when other communications finished...
I did not tested it yet, but it may not work due to checking-first-address-byte already occupied first buffer size and data collapses again. : (
Any clues?
Codevision 3.12 | Windows 11 | Atmega128AU
- Dear moderator, I didn't find a specific codevision dedicated forum so feel free to migrate the topic. -