Skip to content

Commit 879705a

Browse files
author
gabrielsan
authored
removing avr register dependency
trying to port existing code to ESP8266. It does not has UCSR0A register, so it won't compile. Since Arduino IDE 1.6, Serial.flush() waits data on buffer to be transfered, and that is exactly what is being replaced. these modifications are not tested yet
1 parent ebf5036 commit 879705a

File tree

1 file changed

+8
-55
lines changed

1 file changed

+8
-55
lines changed

ModbusRtu.h

Lines changed: 8 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* @author Samuel Marco i Armengol
66
* @contact [email protected]
77
* @contribution Helium6072
8+
* @contribution gabrielsan
89
*
910
* @description
1011
* Arduino library for communicating with Modbus devices
@@ -896,73 +897,25 @@ void Modbus::sendTxBuffer()
896897
u8BufferSize++;
897898
au8Buffer[ u8BufferSize ] = u16crc & 0x00ff;
898899
u8BufferSize++;
899-
900-
// set RS485 transceiver to transmit mode
900+
901901
if (u8txenpin > 1)
902902
{
903-
switch( u8serno )
904-
{
905-
#if defined(UBRR1H)
906-
case 1:
907-
UCSR1A=UCSR1A |(1 << TXC1);
908-
break;
909-
#endif
910-
911-
#if defined(UBRR2H)
912-
case 2:
913-
UCSR2A=UCSR2A |(1 << TXC2);
914-
break;
915-
#endif
916-
917-
#if defined(UBRR3H)
918-
case 3:
919-
UCSR3A=UCSR3A |(1 << TXC3);
920-
break;
921-
#endif
922-
case 0:
923-
UCSR0A=UCSR0A |(1 << TXC0);
924-
break;
925-
default:
926-
break;
927-
}
903+
// set RS485 transceiver to transmit mode
928904
digitalWrite( u8txenpin, HIGH );
929905
}
930906

931907
// transfer buffer to serial line
932-
if(u8serno<4)
908+
if (u8serno < 4)
933909
port->write( au8Buffer, u8BufferSize );
934910
else
935911
softPort->write( au8Buffer, u8BufferSize );
936912

937-
// keep RS485 transceiver in transmit mode as long as sending
938913
if (u8txenpin > 1)
939914
{
940-
switch( u8serno )
941-
{
942-
#if defined(UBRR1H)
943-
case 1:
944-
while (!(UCSR1A & (1 << TXC1)));
945-
break;
946-
#endif
947-
948-
#if defined(UBRR2H)
949-
case 2:
950-
while (!(UCSR2A & (1 << TXC2)));
951-
break;
952-
#endif
953-
954-
#if defined(UBRR3H)
955-
case 3:
956-
while (!(UCSR3A & (1 << TXC3)));
957-
break;
958-
#endif
959-
case 0:
960-
while (!(UCSR0A & (1 << TXC0)));
961-
break;
962-
default:
963-
break;
964-
}
965-
915+
// must wait transmission end before changing pin state
916+
// soft serial does not need it since it is blocking
917+
if (u8serno < 4)
918+
port->flush();
966919
// return RS485 transceiver to receive mode
967920
digitalWrite( u8txenpin, LOW );
968921
}

0 commit comments

Comments
 (0)