43 #include <SoftwareSerial.h>
47 #define RS485_PIN_DE 0b01000000
48 #define RS485_PIN_RE 0b00100000
49 #define RS485_DIR_REG DDRJ
50 #define RS485_PORT_REG PORTJ
51 #define RS485_CLEAR_DE RS485_PORT_REG &= ~RS485_PIN_DE
52 #define RS485_CLEAR_RE RS485_PORT_REG &= ~RS485_PIN_RE
53 #define RS485_SET_DE RS485_PORT_REG |= RS485_PIN_DE
54 #define RS485_SET_RE RS485_PORT_REG |= RS485_PIN_RE
138 MB_FC_READ_DISCRETE_INPUT,
139 MB_FC_READ_REGISTERS,
140 MB_FC_READ_INPUT_REGISTER,
142 MB_FC_WRITE_REGISTER,
143 MB_FC_WRITE_MULTIPLE_COILS,
144 MB_FC_WRITE_MULTIPLE_REGISTERS};
175 uint16_t
calcCRC(uint8_t u8length);
181 int8_t
process_FC1(uint16_t *regs, uint8_t u8size);
182 int8_t
process_FC3(uint16_t *regs, uint8_t u8size);
183 int8_t
process_FC5(uint16_t *regs, uint8_t u8size);
184 int8_t
process_FC6(uint16_t *regs, uint8_t u8size);
194 void begin(
long u32speed);
195 void begin(SoftwareSerial *sPort,
long u32speed);
196 void begin(
long u32speed, uint8_t u8config);
203 int8_t
poll(uint16_t *regs, uint8_t u8size);
327 port->begin(u32speed);
338 while (
port->read() >= 0)
423 port->begin(u32speed, u8config);
434 while (
port->read() >= 0)
595 uint8_t u8regsno, u8bytesno;
601 if ((telegram.
u8id == 0) || (telegram.
u8id > 247))
614 switch (telegram.
u8fct)
616 case MB_FC_READ_COILS:
617 case MB_FC_READ_DISCRETE_INPUT:
618 case MB_FC_READ_REGISTERS:
619 case MB_FC_READ_INPUT_REGISTER:
624 case MB_FC_WRITE_COIL:
629 case MB_FC_WRITE_REGISTER:
634 case MB_FC_WRITE_MULTIPLE_COILS:
636 u8bytesno = u8regsno * 2;
648 u8regsno = u8bytesno = 0;
656 case MB_FC_WRITE_MULTIPLE_REGISTERS:
662 for (uint16_t i = 0; i < telegram.
u16CoilsNo; i++)
697 u8current =
port->available();
736 Log.verboseln(
"Add error : %d - RX-Buffer < 7", i8state);
743 if (u8exception != 0)
745 Log.verboseln(
"ModbusRTU::poll : exception : %d", u8exception);
753 case MB_FC_READ_COILS:
754 case MB_FC_READ_DISCRETE_INPUT:
758 case MB_FC_READ_INPUT_REGISTER:
759 case MB_FC_READ_REGISTERS:
763 case MB_FC_WRITE_COIL:
764 case MB_FC_WRITE_REGISTER:
765 case MB_FC_WRITE_MULTIPLE_COILS:
766 case MB_FC_WRITE_MULTIPLE_REGISTERS:
799 u8current =
port->available();
832 Serial.println(
"exception : 1");
845 Log.verboseln(
"mb : process message : %d",
FUNC);
850 case MB_FC_READ_COILS:
851 case MB_FC_READ_DISCRETE_INPUT:
854 case MB_FC_READ_INPUT_REGISTER:
855 case MB_FC_READ_REGISTERS:
858 case MB_FC_WRITE_COIL:
861 case MB_FC_WRITE_REGISTER:
864 case MB_FC_WRITE_MULTIPLE_COILS:
867 case MB_FC_WRITE_MULTIPLE_REGISTERS:
907 boolean bBuffOverflow =
false;
918 while (
port->available())
925 bBuffOverflow =
true;
934 bBuffOverflow =
true;
985 Serial.print(
"Send Hex : \t");
986 for (
int i = 0; i < 8; i++)
991 Serial.println(
"--------------- \n");
1001 UCSR1A = UCSR1A | (1 << TXC1);
1007 UCSR2A = UCSR2A | (1 << TXC2);
1013 UCSR3A = UCSR3A | (1 << TXC3);
1018 UCSR0A = UCSR0A | (1 << TXC0);
1042 while (!(UCSR1A & (1 << TXC1)))
1049 while (!(UCSR2A & (1 << TXC2)))
1056 while (!(UCSR3A & (1 << TXC3)))
1062 while (!(UCSR0A & (1 << TXC0)))
1072 while (
port->read() >= 0)
1098 unsigned int temp, temp2, flag;
1100 for (
unsigned char i = 0; i < u8length; i++)
1104 for (
unsigned char j = 1; j <= 8; j++)
1106 flag = temp & 0x0001;
1114 temp = (temp << 8) | temp2;
1131 uint16_t u16MsgCRC =
1140 boolean isSupported =
false;
1156 uint16_t u16regs = 0;
1160 case MB_FC_READ_COILS:
1161 case MB_FC_READ_DISCRETE_INPUT:
1162 case MB_FC_WRITE_MULTIPLE_COILS:
1165 u8regs = (uint8_t)u16regs;
1169 case MB_FC_WRITE_COIL:
1171 u8regs = (uint8_t)u16regs;
1175 case MB_FC_WRITE_REGISTER:
1177 u8regs = (uint8_t)u16regs;
1181 case MB_FC_READ_REGISTERS:
1182 case MB_FC_READ_INPUT_REGISTER:
1183 case MB_FC_WRITE_MULTIPLE_REGISTERS:
1186 u8regs = (uint8_t)u16regs;
1204 uint16_t u16MsgCRC =
1220 boolean isSupported =
false;
1304 uint8_t u8currentRegister, u8currentBit, u8bytesno, u8bitsno;
1305 uint8_t u8CopyBufferSize;
1306 uint16_t u16currentCoil, u16coil;
1313 u8bytesno = (uint8_t)(u16Coilno / 8);
1314 if (u16Coilno % 8 != 0)
1322 for (u16currentCoil = 0; u16currentCoil < u16Coilno; u16currentCoil++)
1324 u16coil = u16StartCoil + u16currentCoil;
1325 u8currentRegister = (uint8_t)(u16coil / 16);
1326 u8currentBit = (uint8_t)(u16coil % 16);
1331 bitRead(regs[u8currentRegister], u8currentBit));
1342 if (u16Coilno % 8 != 0)
1346 return u8CopyBufferSize;
1362 uint8_t u8CopyBufferSize;
1368 for (i = u8StartAdd; i < u8StartAdd + u8regsno; i++)
1378 return u8CopyBufferSize;
1391 uint8_t u8currentRegister, u8currentBit;
1392 uint8_t u8CopyBufferSize;
1396 u8currentRegister = (uint8_t)(u16coil / 16);
1397 u8currentBit = (uint8_t)(u16coil % 16);
1401 regs[u8currentRegister],
1410 return u8CopyBufferSize;
1425 uint8_t u8CopyBufferSize;
1428 regs[u8add] = u16val;
1436 return u8CopyBufferSize;
1449 uint8_t u8currentRegister, u8currentBit, u8frameByte, u8bitsno;
1450 uint8_t u8CopyBufferSize;
1451 uint16_t u16currentCoil, u16coil;
1461 for (u16currentCoil = 0; u16currentCoil < u16Coilno; u16currentCoil++)
1464 u16coil = u16StartCoil + u16currentCoil;
1465 u8currentRegister = (uint8_t)(u16coil / 16);
1466 u8currentBit = (uint8_t)(u16coil % 16);
1473 regs[u8currentRegister],
1491 return u8CopyBufferSize;
1507 uint8_t u8CopyBufferSize;
1517 for (i = 0; i < u8regsno; i++)
1523 regs[u8StartAdd + i] = temp;
1528 return u8CopyBufferSize;