22 if(max_telegram_length < 256) max_telegram_length = 256;
23 tel =
new unsigned char[max_telegram_length];
24 maxtel = max_telegram_length;
34 if(
tel != NULL)
delete []
tel;
39 return (data[0]*256)+data[1];
44 data[0] = (
unsigned char) val / 256;
45 data[1] = (
unsigned char) val & 0x0ff;
54 int rlModbus::write(
int slave,
int function,
const unsigned char *data,
int datalen,
int *transactionID)
63 sprintf((
char *) &
tel[len],
"%02X", slave); len += 2;
64 sprintf((
char *) &
tel[len],
"%02X",
function); len += 2;
65 for(i=0; i<datalen; i++)
67 sprintf((
char *) &
tel[len],
"%02X",(
int) data[i]); len += 2;
78 if(transactionID == NULL)
85 tel[len++] = ((*transactionID) & 0xFF00) / 256;
86 tel[len++] = (*transactionID) & 0xFF;
91 tel[len++] = 2+datalen;
93 tel[len++] = (
unsigned char) slave;
94 tel[len++] = (
unsigned char)
function;
95 for(i=0; i<datalen; i++)
123 unsigned char data[4];
125 data[0] = (
unsigned char) ( start_adr / 256 );
126 data[1] = (
unsigned char) ( start_adr & 0x0ff );
127 data[2] = (
unsigned char) ( num_register / 256 );
128 data[3] = (
unsigned char) ( num_register & 0x0ff );
129 return write(slave,
function, data, 4);
134 unsigned char *telptr;
135 int ret,len,byte_count,idata,i,itel,val;
153 byte_count =
tel[5] - 3;
207 tel[i] = (
unsigned char) itel;
209 if(
tel[i] == 0x0a)
break;
212 telptr = (
unsigned char *) strchr((
const char *)
tel,
':');
215 sscanf((
char *) &telptr[len],
"%02X",slave); len += 2;
216 sscanf((
char *) &telptr[len],
"%02X",
function); len += 2;
228 sscanf((
char *) &telptr[len],
"%02X",&byte_count); len += 2;
229 for(idata=0; idata<byte_count; idata++)
231 sscanf((
const char *) &telptr[len],
"%02X", &val); len += 2;
243 for(idata=0; idata<(byte_count/2); idata++)
252 for(idata=0; idata<(byte_count/2); idata++)
261 for(idata=0; idata<(byte_count/2); idata++)
269 sscanf((
char *) &telptr[len],
"%04X",&byte_count); len += 4;
270 for(idata=0; idata<(byte_count/2); idata++)
288 *
function =
tel[len++];
289 byte_count =
tel[5] - 3;
306 memcpy(&
tel[len],data,byte_count+2); len += byte_count + 2;
317 memcpy(&
tel[len],data,byte_count+2); len += byte_count + 2;
324 memcpy(&
tel[len],data,byte_count+2); len += byte_count + 2;
331 memcpy(&
tel[len],data,byte_count+2); len += byte_count + 2;
340 memcpy(&
tel[len],data,byte_count+2); len += byte_count + 2;
354 unsigned char *telptr;
355 int ret,len,byte_count,i,itel,val;
365 if(transactionID != NULL) *transactionID =
tel[0] * 256 +
tel[1];
392 byte_count = data[4];
398 byte_count = data[0];
406 byte_count = data[8];
429 tel[i] = (
unsigned char) itel;
431 if(
tel[i] == 0x0a)
break;
434 telptr = (
unsigned char *) strchr((
const char *)
tel,
':');
437 sscanf((
char *) &telptr[len],
"%02X",slave); len += 2;
438 sscanf((
char *) &telptr[len],
"%02X",
function); len += 2;
447 sscanf((
char *) &telptr[len],
"%02X",&val); data[0] = (
unsigned char) val; len += 2;
448 sscanf((
char *) &telptr[len],
"%02X",&val); data[1] = (
unsigned char) val; len += 2;
449 sscanf((
char *) &telptr[len],
"%02X",&val); data[2] = (
unsigned char) val; len += 2;
450 sscanf((
char *) &telptr[len],
"%02X",&val); data[3] = (
unsigned char) val; len += 2;
461 sscanf((
char *) &telptr[len],
"%02X",&val); data[0] = (
unsigned char) val; len += 2;
462 sscanf((
char *) &telptr[len],
"%02X",&val); data[1] = (
unsigned char) val; len += 2;
463 sscanf((
char *) &telptr[len],
"%02X",&val); data[2] = (
unsigned char) val; len += 2;
464 sscanf((
char *) &telptr[len],
"%02X",&val); data[3] = (
unsigned char) val; len += 2;
465 sscanf((
char *) &telptr[len],
"%02X",&byte_count); len += 2;
466 for(i=0; i<byte_count; i++)
468 sscanf((
char *) &telptr[len],
"%02X",&val); data[4+i] = (
unsigned char) val; len += 2;
474 sscanf((
char *) &telptr[len],
"%02X",&byte_count); len += 2;
475 for(i=0; i<byte_count; i++)
477 sscanf((
char *) &telptr[len],
"%02X",&val); data[i] = (
unsigned char) val; len += 2;
482 sscanf((
char *) &telptr[len],
"%02X",&val); data[0] = (
unsigned char) val; len += 2;
483 sscanf((
char *) &telptr[len],
"%02X",&val); data[1] = (
unsigned char) val; len += 2;
484 sscanf((
char *) &telptr[len],
"%02X",&val); data[2] = (
unsigned char) val; len += 2;
485 sscanf((
char *) &telptr[len],
"%02X",&val); data[3] = (
unsigned char) val; len += 2;
486 sscanf((
char *) &telptr[len],
"%02X",&val); data[4] = (
unsigned char) val; len += 2;
487 sscanf((
char *) &telptr[len],
"%02X",&val); data[5] = (
unsigned char) val; len += 2;
491 sscanf((
char *) &telptr[len],
"%02X",&val); data[0] = (
unsigned char) val; len += 2;
492 sscanf((
char *) &telptr[len],
"%02X",&val); data[1] = (
unsigned char) val; len += 2;
493 sscanf((
char *) &telptr[len],
"%02X",&val); data[2] = (
unsigned char) val; len += 2;
494 sscanf((
char *) &telptr[len],
"%02X",&val); data[3] = (
unsigned char) val; len += 2;
495 sscanf((
char *) &telptr[len],
"%02X",&val); data[4] = (
unsigned char) val; len += 2;
496 sscanf((
char *) &telptr[len],
"%02X",&val); data[5] = (
unsigned char) val; len += 2;
497 sscanf((
char *) &telptr[len],
"%02X",&val); data[6] = (
unsigned char) val; len += 2;
498 sscanf((
char *) &telptr[len],
"%02X",&val); data[7] = (
unsigned char) val; len += 2;
499 sscanf((
char *) &telptr[len],
"%02X",&byte_count); len += 2;
500 for(i=0; i<byte_count; i++)
502 sscanf((
char *) &telptr[len],
"%02X",&val); data[8+i] = (
unsigned char) val; len += 2;
507 sscanf((
char *) &telptr[len],
"%02X",&val); data[0] = (
unsigned char) val; len += 2;
508 sscanf((
char *) &telptr[len],
"%02X",&val); data[1] = (
unsigned char) val; len += 2;
521 *
function =
tel[len++];
533 memcpy(&
tel[len],data,4+2); len += 4+2;
545 memcpy(&
tel[len],data,5); len += 5;
546 byte_count = data[4];
548 memcpy(&
tel[len],&data[4],byte_count+2); len += byte_count+2;
554 memcpy(&
tel[len],data,1); len++;
555 byte_count = data[0];
557 memcpy(&
tel[len],data,byte_count+2); len += byte_count+2;
562 memcpy(&
tel[len],data,6+2); len += 6+2;
567 memcpy(&
tel[len],data,9); len += 9;
568 byte_count = data[8];
570 memcpy(&
tel[len],&data[8],byte_count+2); len += byte_count+2;
575 memcpy(&
tel[len],data,2+2); len += 2+2;
601 return (buf[0]*256 + buf[1]);
610 buf[0] = (
unsigned char) high;
611 buf[1] = (
unsigned char) low;
618 sscanf((
char *) buf,
"%04X",&val);
624 sprintf((
char *) buf,
"%04X",i);
634 for(i=1; i<len; i+=2)
636 sscanf((
const char *) &
tel[i],
"%1X", &high);
637 sscanf((
const char *) &
tel[i+1],
"%1X", &low);
641 lrc = ((
unsigned char)(-((
char) lrc)));
642 sprintf((
char *) &
tel[len],
"%02X",(
unsigned int) lrc);
651 if(len < 0)
return 1;
653 cptr = (
unsigned char *) strchr((
char *)
tel,
':');
654 if(cptr == NULL)
return 1;
657 for(i=1; i<len+2; i+=2)
659 sscanf((
const char *) cptr++,
"%1X", &high);
660 sscanf((
const char *) cptr++,
"%1X", &low);
664 if(lrc == 0)
return 0;
671 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
672 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
673 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
674 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
675 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
676 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
677 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
678 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
679 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
680 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,
681 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
682 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
683 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
684 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,
685 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
686 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
687 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
688 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
689 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
690 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
691 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
692 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,
693 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
694 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
695 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
696 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40
702 0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06,
703 0x07, 0xC7, 0x05, 0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, 0x73, 0xB1, 0x71,
721 0x70, 0xB0, 0x50, 0x90, 0x91, 0x51, 0x93, 0x53, 0x52, 0x92,
722 0x96, 0x56, 0x57, 0x97, 0x55, 0x95, 0x94, 0x54, 0x9C, 0x5C,
723 0x5D, 0x9D, 0x5F, 0x9F, 0x9E, 0x5E, 0x5A, 0x9A, 0x9B, 0x5B,
724 0x99, 0x59, 0x58, 0x98, 0x88, 0x48, 0x49, 0x89, 0x4B, 0x8B,
725 0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D, 0x4C, 0x8C,
726 0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42,
727 0x43, 0x83, 0x41, 0x81, 0x80, 0x40
732 unsigned char crc_high, crc_low;
737 crc_high = crc_low = 0xff;
740 index = crc_low ^
tel[i];
745 tel[len+1] = crc_high;
750 unsigned char crc_high, crc_low;
754 if(len < 2)
return 1;
755 crc_high = crc_low = 0xff;
756 for(i=0; i<len-2; i++)
758 index = crc_low ^
tel[i];
762 if(crc_low !=
tel[len-2])
return 1;
763 if(crc_high !=
tel[len-1])
return 1;
770 int ret_slave, ret_function;
771 unsigned char data[256];
773 data[0] = (start_adr / 256) & 0x0ff;
774 data[1] = start_adr & 0x0ff;
775 data[2] = (number_of_coils / 256) & 0x0ff;
776 data[3] = number_of_coils & 0x0ff;
780 ret =
response(&ret_slave, &ret_function, status, timeout);
781 if(ret < 0 || ret_slave != slave || ret_function !=
ReadCoilStatus)
783 printf(
"rlMOdbus::ERROR response ret=%d slave=%d ret_slave=%d function=%d ret_function=%d timeout=%d\n",
794 int ret_slave, ret_function;
795 unsigned char data[256];
797 data[0] = (start_adr / 256) & 0x0ff;
798 data[1] = start_adr & 0x0ff;
799 data[2] = (number_of_inputs / 256) & 0x0ff;
800 data[3] = number_of_inputs & 0x0ff;
804 ret =
response(&ret_slave, &ret_function, status, timeout);
807 printf(
"rlMOdbus::ERROR response ret=%d slave=%d ret_slave=%d function=%d ret_function=%d timeout=%d\n",
818 int ret_slave, ret_function;
819 unsigned char data[256];
821 data[0] = (start_adr / 256) & 0x0ff;
822 data[1] = start_adr & 0x0ff;
823 data[2] = (number_of_registers / 256) & 0x0ff;
824 data[3] = number_of_registers & 0x0ff;
828 ret =
response(&ret_slave, &ret_function, data, timeout);
831 printf(
"rlMOdbus::ERROR response ret=%d slave=%d ret_slave=%d function=%d ret_function=%d timeout=%d\n",
836 for(
int i=0; i<ret; i+=2)
838 registers[j++] = ((data[i] * 256) & 0x0ff) + (data[i+1] & 0x0ff);
847 int ret_slave, ret_function;
848 unsigned char data[256];
850 data[0] = (start_adr / 256) & 0x0ff;
851 data[1] = start_adr & 0x0ff;
852 data[2] = (number_of_registers / 256) & 0x0ff;
853 data[3] = number_of_registers & 0x0ff;
857 ret =
response(&ret_slave, &ret_function, data, timeout);
860 printf(
"rlMOdbus::ERROR response ret=%d slave=%d ret_slave=%d function=%d ret_function=%d timeout=%d\n",
865 for(
int i=0; i<ret; i+=2)
867 registers[j++] = ((data[i] * 256) & 0x0ff) + (data[i+1] & 0x0ff);
876 int ret_slave, ret_function;
877 unsigned char data[256];
879 data[0] = (coil_adr / 256) & 0x0ff;
880 data[1] = coil_adr & 0x0ff;
883 if(value) data[2] = 0x0ff;
887 ret =
response(&ret_slave, &ret_function, data, timeout);
890 printf(
"rlMOdbus::ERROR response ret=%d slave=%d ret_slave=%d function=%d ret_function=%d timeout=%d\n",
901 int ret_slave, ret_function;
902 unsigned char data[256];
904 data[0] = (register_adr / 256) & 0x0ff;
905 data[1] = register_adr & 0x0ff;
906 data[2] = (value / 256) & 0x0ff;
907 data[3] = value & 0x0ff;
911 ret =
response(&ret_slave, &ret_function, data, timeout);
914 printf(
"rlMOdbus::ERROR response ret=%d slave=%d ret_slave=%d function=%d ret_function=%d timeout=%d\n",
925 int ret_slave, ret_function;
926 unsigned char data[256];
928 data[0] = (coil_adr / 256) & 0x0ff;
929 data[1] = coil_adr & 0x0ff;
930 data[2] = (number_of_coils / 256) & 0x0ff;
931 data[3] = number_of_coils & 0x0ff;
932 data[4] = (number_of_coils / 8) + 1;
934 for(i=0; i<data[4]; i++) data[5+i] = coils[i];
938 ret =
response(&ret_slave, &ret_function, data, timeout);
941 printf(
"rlMOdbus::ERROR response ret=%d slave=%d ret_slave=%d function=%d ret_function=%d timeout=%d\n",
952 int ret_slave, ret_function;
953 unsigned char data[256];
955 data[0] = (start_adr / 256) & 0x0ff;
956 data[1] = start_adr & 0x0ff;
957 data[2] = (number_of_registers / 256) & 0x0ff;
958 data[3] = number_of_registers & 0x0ff;
959 data[4] = (number_of_registers * 2) & 0x0ff;
961 for(
int i=0; i<number_of_registers; i++)
963 data[j++] = (registers[i] / 256) & 0x0ff;
964 data[j++] = registers[i] & 0x0ff;
969 ret =
response(&ret_slave, &ret_function, data, timeout);
972 printf(
"rlMOdbus::ERROR response ret=%d slave=%d ret_slave=%d function=%d ret_function=%d timeout=%d\n",
int readBlock(unsigned char *buf, int len, int timeout=-1)
int readInputStatus(int slave, int start_adr, int number_of_inputs, unsigned char *status, int timeout=1000)
int write(const void *buf, int len)
int int2data(int val, unsigned char *data)
int readCoilStatus(int slave, int start_adr, int number_of_coils, unsigned char *status, int timeout=1000)
void registerSerial(rlSerial *serial)
void int2buf_ascii(int i, unsigned char *buf)
int buf2int_ascii(unsigned char *buf)
void registerSocket(rlSocket *socket)
static const unsigned char array_crc_low[]
static const unsigned char array_crc_high[]
int write(int slave, int function, const unsigned char *data, int len, int *transactionID=NULL)
int select(int timeout=500)
int read(void *buf, int len, int timeout=0)
int buf2int_rtu(unsigned char *buf)
int forceSingleCoil(int slave, int coil_adr, int value, int timeout=1000)
int presetMultipleRegisters(int slave, int start_adr, int number_of_registers, int *registers, int timeout=1000)
rlModbus(long max_telegram_length=1024, int mode=MODBUS_RTU, char end_delimitor=0x0a)
int readHoldingRegisters(int slave, int start_adr, int number_of_registers, int *registers, int timeout=1000)
int forceMultipleCoils(int slave, int coil_adr, int number_of_coils, unsigned char *coils, int timeout=1000)
int presetSingleRegister(int slave, int register_adr, int value, int timeout=1000)
void int2buf_rtu(int i, unsigned char *buf)
int response(int *slave, int *function, unsigned char *data, int timeout=1000)
int data2int(const unsigned char *data)
int request(int slave, int function, int start_adr, int num_register)
int readInputRegisters(int slave, int start_adr, int number_of_registers, int *registers, int timeout=1000)
int writeBlock(const unsigned char *buf, int len)
int readRequest(int *slave, int *function, unsigned char *data, int timeout=1000, int *transactionID=NULL)