modbus for modular2_dio10

Committer:
yao6116601
Date:
Sat Sep 28 02:25:51 2019 +0000
Revision:
1:8fcbafe2ca98
Parent:
0:998a68b8defc
modbus for modular2_dio10

Who changed what in which revision?

UserRevisionLine numberNew contents of line
yao6116601 0:998a68b8defc 1 #include "modbus.h"
yao6116601 0:998a68b8defc 2 #define MODBUS_DEFAULT_BAUD_RATE 9600
yao6116601 0:998a68b8defc 3 const char auchCRCHi[256]={
yao6116601 0:998a68b8defc 4 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81,
yao6116601 0:998a68b8defc 5 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
yao6116601 0:998a68b8defc 6 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01,
yao6116601 0:998a68b8defc 7 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
yao6116601 0:998a68b8defc 8 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81,
yao6116601 0:998a68b8defc 9 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
yao6116601 0:998a68b8defc 10 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01,
yao6116601 0:998a68b8defc 11 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
yao6116601 0:998a68b8defc 12 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81,
yao6116601 0:998a68b8defc 13 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
yao6116601 0:998a68b8defc 14 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01,
yao6116601 0:998a68b8defc 15 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
yao6116601 0:998a68b8defc 16 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81,
yao6116601 0:998a68b8defc 17 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
yao6116601 0:998a68b8defc 18 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01,
yao6116601 0:998a68b8defc 19 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
yao6116601 0:998a68b8defc 20 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81,
yao6116601 0:998a68b8defc 21 0x40
yao6116601 0:998a68b8defc 22 };
yao6116601 0:998a68b8defc 23 const char auchCRCLo[256]={
yao6116601 0:998a68b8defc 24 0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06, 0x07, 0xC7, 0x05, 0xC5, 0xC4,
yao6116601 0:998a68b8defc 25 0x04, 0xCC, 0x0C, 0x0D, 0xCD, 0x0F, 0xCF, 0xCE, 0x0E, 0x0A, 0xCA, 0xCB, 0x0B, 0xC9, 0x09,
yao6116601 0:998a68b8defc 26 0x08, 0xC8, 0xD8, 0x18, 0x19, 0xD9, 0x1B, 0xDB, 0xDA, 0x1A, 0x1E, 0xDE, 0xDF, 0x1F, 0xDD,
yao6116601 0:998a68b8defc 27 0x1D, 0x1C, 0xDC, 0x14, 0xD4, 0xD5, 0x15, 0xD7, 0x17, 0x16, 0xD6, 0xD2, 0x12, 0x13, 0xD3,
yao6116601 0:998a68b8defc 28 0x11, 0xD1, 0xD0, 0x10, 0xF0, 0x30, 0x31, 0xF1, 0x33, 0xF3, 0xF2, 0x32, 0x36, 0xF6, 0xF7,
yao6116601 0:998a68b8defc 29 0x37, 0xF5, 0x35, 0x34, 0xF4, 0x3C, 0xFC, 0xFD, 0x3D, 0xFF, 0x3F, 0x3E, 0xFE, 0xFA, 0x3A,
yao6116601 0:998a68b8defc 30 0x3B, 0xFB, 0x39, 0xF9, 0xF8, 0x38, 0x28, 0xE8, 0xE9, 0x29, 0xEB, 0x2B, 0x2A, 0xEA, 0xEE,
yao6116601 0:998a68b8defc 31 0x2E, 0x2F, 0xEF, 0x2D, 0xED, 0xEC, 0x2C, 0xE4, 0x24, 0x25, 0xE5, 0x27, 0xE7, 0xE6, 0x26,
yao6116601 0:998a68b8defc 32 0x22, 0xE2, 0xE3, 0x23, 0xE1, 0x21, 0x20, 0xE0, 0xA0, 0x60, 0x61, 0xA1, 0x63, 0xA3, 0xA2,
yao6116601 0:998a68b8defc 33 0x62, 0x66, 0xA6, 0xA7, 0x67, 0xA5, 0x65, 0x64, 0xA4, 0x6C, 0xAC, 0xAD, 0x6D, 0xAF, 0x6F,
yao6116601 0:998a68b8defc 34 0x6E, 0xAE, 0xAA, 0x6A, 0x6B, 0xAB, 0x69, 0xA9, 0xA8, 0x68, 0x78, 0xB8, 0xB9, 0x79, 0xBB,
yao6116601 0:998a68b8defc 35 0x7B, 0x7A, 0xBA, 0xBE, 0x7E, 0x7F, 0xBF, 0x7D, 0xBD, 0xBC, 0x7C, 0xB4, 0x74, 0x75, 0xB5,
yao6116601 0:998a68b8defc 36 0x77, 0xB7, 0xB6, 0x76, 0x72, 0xB2, 0xB3, 0x73, 0xB1, 0x71, 0x70, 0xB0, 0x50, 0x90, 0x91,
yao6116601 0:998a68b8defc 37 0x51, 0x93, 0x53, 0x52, 0x92, 0x96, 0x56, 0x57, 0x97, 0x55, 0x95, 0x94, 0x54, 0x9C, 0x5C,
yao6116601 0:998a68b8defc 38 0x5D, 0x9D, 0x5F, 0x9F, 0x9E, 0x5E, 0x5A, 0x9A, 0x9B, 0x5B, 0x99, 0x59, 0x58, 0x98, 0x88,
yao6116601 0:998a68b8defc 39 0x48, 0x49, 0x89, 0x4B, 0x8B, 0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D, 0x4C, 0x8C,
yao6116601 0:998a68b8defc 40 0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42, 0x43, 0x83, 0x41, 0x81, 0x80,
yao6116601 0:998a68b8defc 41 0x40
yao6116601 0:998a68b8defc 42 };
yao6116601 0:998a68b8defc 43 Modbus::Modbus(PinName tx, PinName rx,PinName txEnable, int baud)
yao6116601 0:998a68b8defc 44 : rs485(tx, rx,baud),
yao6116601 0:998a68b8defc 45 dir(txEnable)
yao6116601 0:998a68b8defc 46 {
yao6116601 0:998a68b8defc 47 rs485.format(8, Serial::None, 1);
yao6116601 0:998a68b8defc 48 }
yao6116601 0:998a68b8defc 49 bool Modbus::sendQuery(uint8_t address,uint8_t function,uint16_t startAddress,uint16_t points)
yao6116601 0:998a68b8defc 50 { int index,i;
yao6116601 0:998a68b8defc 51 index=0;
yao6116601 0:998a68b8defc 52 _address=address;
yao6116601 0:998a68b8defc 53 _function=function;
yao6116601 0:998a68b8defc 54 _startAddress=startAddress;
yao6116601 0:998a68b8defc 55 _points=points;
yao6116601 0:998a68b8defc 56 sbuf[index++]=address;
yao6116601 0:998a68b8defc 57 sbuf[index++]=function;
yao6116601 0:998a68b8defc 58 sbuf[index++]=(startAddress>>8)&0xff;
yao6116601 0:998a68b8defc 59 sbuf[index++]=startAddress&0xff;
yao6116601 0:998a68b8defc 60 sbuf[index++]=(points>>8)&0xff;
yao6116601 0:998a68b8defc 61 sbuf[index++]=points&0xff;
yao6116601 0:998a68b8defc 62 unsigned short resultCRC = calculateCRC16(sbuf,index);
yao6116601 0:998a68b8defc 63 sbuf[index++]=(resultCRC>>8)&0xff;
yao6116601 0:998a68b8defc 64 sbuf[index++]=resultCRC&0xff;
yao6116601 0:998a68b8defc 65 dir=1;
yao6116601 0:998a68b8defc 66 wait_ms(2);
yao6116601 0:998a68b8defc 67 for (i=0;i<index;i++)
yao6116601 0:998a68b8defc 68 {
yao6116601 0:998a68b8defc 69 rs485.putc(sbuf[i]);
yao6116601 0:998a68b8defc 70
yao6116601 0:998a68b8defc 71 }
yao6116601 0:998a68b8defc 72 wait_ms(2);
yao6116601 0:998a68b8defc 73 dir=0;
yao6116601 0:998a68b8defc 74 return true;
yao6116601 0:998a68b8defc 75 }
yao6116601 0:998a68b8defc 76 int Modbus::waitResponse(uint8_t *data)
yao6116601 0:998a68b8defc 77 { unsigned short resultCRC ,recCRC;
yao6116601 0:998a68b8defc 78
yao6116601 0:998a68b8defc 79 int state,index,len,cnt,i;
yao6116601 0:998a68b8defc 80 uint8_t ch;
yao6116601 0:998a68b8defc 81 bool flag;
yao6116601 0:998a68b8defc 82 flag=true;
yao6116601 0:998a68b8defc 83 state=0;
yao6116601 0:998a68b8defc 84 index=0;
yao6116601 0:998a68b8defc 85 while(flag)
yao6116601 0:998a68b8defc 86 { ch=rs485.getc();
yao6116601 0:998a68b8defc 87
yao6116601 0:998a68b8defc 88 switch(state)
yao6116601 0:998a68b8defc 89 {
yao6116601 0:998a68b8defc 90 case 0:{
yao6116601 0:998a68b8defc 91 if (ch==_address)
yao6116601 0:998a68b8defc 92 {state=1;
yao6116601 0:998a68b8defc 93 rbuf[index++]=ch;}
yao6116601 0:998a68b8defc 94 break;
yao6116601 0:998a68b8defc 95 }
yao6116601 0:998a68b8defc 96 case 1:{
yao6116601 0:998a68b8defc 97
yao6116601 0:998a68b8defc 98 if (ch==_function)
yao6116601 0:998a68b8defc 99 {state=2;rbuf[index++]=ch;}
yao6116601 0:998a68b8defc 100 else if(ch==(_function+0x80))
yao6116601 0:998a68b8defc 101 state=10;
yao6116601 0:998a68b8defc 102 else
yao6116601 0:998a68b8defc 103 return 0;
yao6116601 0:998a68b8defc 104 break;
yao6116601 0:998a68b8defc 105 }
yao6116601 0:998a68b8defc 106 case 2:{
yao6116601 0:998a68b8defc 107 len=ch;cnt=ch;
yao6116601 0:998a68b8defc 108 rbuf[index++]=ch;
yao6116601 0:998a68b8defc 109 state=3;
yao6116601 0:998a68b8defc 110 break;
yao6116601 0:998a68b8defc 111 }
yao6116601 0:998a68b8defc 112 case 3:{
yao6116601 0:998a68b8defc 113 rbuf[index++]=ch;
yao6116601 0:998a68b8defc 114 cnt--;
yao6116601 0:998a68b8defc 115 if (cnt==0)
yao6116601 0:998a68b8defc 116 {
yao6116601 0:998a68b8defc 117 state=4;
yao6116601 0:998a68b8defc 118
yao6116601 0:998a68b8defc 119 }
yao6116601 0:998a68b8defc 120 break;
yao6116601 0:998a68b8defc 121 }
yao6116601 0:998a68b8defc 122 case 4:{
yao6116601 0:998a68b8defc 123 recCRC=ch;
yao6116601 0:998a68b8defc 124 state=5;
yao6116601 0:998a68b8defc 125 break;
yao6116601 0:998a68b8defc 126 }
yao6116601 0:998a68b8defc 127 case 5:{
yao6116601 0:998a68b8defc 128 recCRC=(recCRC<<8)|ch;
yao6116601 0:998a68b8defc 129 resultCRC = calculateCRC16(rbuf,index);
yao6116601 0:998a68b8defc 130 // printf("CRC=%02x,%02x\n",recCRC,resultCRC);
yao6116601 0:998a68b8defc 131 if (recCRC==resultCRC) {
yao6116601 0:998a68b8defc 132 for (i=0;i<len;i++)
yao6116601 0:998a68b8defc 133 data[i]=rbuf[3+i];
yao6116601 0:998a68b8defc 134 return len;
yao6116601 0:998a68b8defc 135 }
yao6116601 0:998a68b8defc 136 else return 0;
yao6116601 0:998a68b8defc 137 }
yao6116601 0:998a68b8defc 138 case 10:{
yao6116601 0:998a68b8defc 139 len=0;
yao6116601 0:998a68b8defc 140 flag=false;
yao6116601 0:998a68b8defc 141 break;
yao6116601 0:998a68b8defc 142 }
yao6116601 0:998a68b8defc 143 }
yao6116601 0:998a68b8defc 144 }
yao6116601 0:998a68b8defc 145
yao6116601 0:998a68b8defc 146 return len;
yao6116601 0:998a68b8defc 147 }
yao6116601 0:998a68b8defc 148 bool Modbus::waitQuery(uint8_t &address,uint8_t &function,uint16_t &startAddress,uint16_t &points)
yao6116601 0:998a68b8defc 149 {
yao6116601 0:998a68b8defc 150 unsigned short resultCRC ,recCRC;
yao6116601 0:998a68b8defc 151 int i;
yao6116601 0:998a68b8defc 152 for (i=0;i<8;i++)
yao6116601 0:998a68b8defc 153 rbuf[i]=rs485.getc();
yao6116601 0:998a68b8defc 154 resultCRC = calculateCRC16(rbuf,6);
yao6116601 0:998a68b8defc 155 recCRC=(rbuf[6]<<8)|rbuf[7];
yao6116601 0:998a68b8defc 156 if ( resultCRC!=recCRC) return false;
yao6116601 0:998a68b8defc 157 address=rbuf[0];
yao6116601 0:998a68b8defc 158 _address=rbuf[0];
yao6116601 0:998a68b8defc 159 function=rbuf[1];
yao6116601 0:998a68b8defc 160 _function=rbuf[1];
yao6116601 0:998a68b8defc 161 _startAddress=(rbuf[2]<<8)|rbuf[3];
yao6116601 0:998a68b8defc 162 startAddress=_startAddress;
yao6116601 0:998a68b8defc 163 _points=(rbuf[4]<<8)|rbuf[5];
yao6116601 0:998a68b8defc 164 points=_points;
yao6116601 0:998a68b8defc 165 return true;
yao6116601 0:998a68b8defc 166 }
yao6116601 0:998a68b8defc 167 bool Modbus::sendResponse(uint8_t *data,uint8_t length)
yao6116601 0:998a68b8defc 168 {
yao6116601 0:998a68b8defc 169 unsigned short resultCRC;
yao6116601 0:998a68b8defc 170 int index,i;
yao6116601 0:998a68b8defc 171 index=0;
yao6116601 0:998a68b8defc 172 sbuf[index++]=_address;
yao6116601 0:998a68b8defc 173 sbuf[index++]=_function;
yao6116601 0:998a68b8defc 174 sbuf[index++]= length;
yao6116601 0:998a68b8defc 175 for (i=0;i<length;i++)
yao6116601 0:998a68b8defc 176 sbuf[index++]=data[i];
yao6116601 0:998a68b8defc 177 resultCRC = calculateCRC16(rbuf,index);
yao6116601 0:998a68b8defc 178 sbuf[index++]=(resultCRC>>8)&0xff;
yao6116601 0:998a68b8defc 179 sbuf[index++]=resultCRC &0xff;
yao6116601 1:8fcbafe2ca98 180 dir=1;
yao6116601 1:8fcbafe2ca98 181 wait_ms(2);
yao6116601 0:998a68b8defc 182 for (i=0;i<index;i++)
yao6116601 0:998a68b8defc 183 rs485.putc(sbuf[i]) ;
yao6116601 1:8fcbafe2ca98 184 wait_ms(2);
yao6116601 1:8fcbafe2ca98 185 dir=0;
yao6116601 0:998a68b8defc 186 return true;
yao6116601 0:998a68b8defc 187 }
yao6116601 0:998a68b8defc 188 unsigned int Modbus::calculateCRC16( uint8_t *puchMsg,unsigned int usDataLen)
yao6116601 0:998a68b8defc 189 {
yao6116601 0:998a68b8defc 190 unsigned char uchCRCHi,uchCRCLo;
yao6116601 0:998a68b8defc 191 unsigned char uIndex=0 ; /* will index into CRC lookup table */
yao6116601 0:998a68b8defc 192 unsigned char tempp=0;
yao6116601 0:998a68b8defc 193 unsigned int CRCHL=0;
yao6116601 0:998a68b8defc 194 uchCRCHi=0xFF ; // high byte of CRC initialized
yao6116601 0:998a68b8defc 195 uchCRCLo=0xFF ;
yao6116601 0:998a68b8defc 196 tempp=0; // low byte of CRC initialized
yao6116601 0:998a68b8defc 197 while(usDataLen--) // pass through message buffer
yao6116601 0:998a68b8defc 198 {
yao6116601 0:998a68b8defc 199 uIndex=uchCRCHi^*puchMsg++ ; // calculate the CRC high byte
yao6116601 0:998a68b8defc 200 tempp++;
yao6116601 0:998a68b8defc 201 uchCRCHi=uchCRCLo^auchCRCHi[uIndex] ;//01-auchCRCHi[254]:0x81;02-0x01;03-0x80;04-0xC0
yao6116601 0:998a68b8defc 202 uchCRCLo=auchCRCLo[uIndex] ;
yao6116601 0:998a68b8defc 203 }
yao6116601 0:998a68b8defc 204 CRCHL=uchCRCHi;
yao6116601 0:998a68b8defc 205 CRCHL=CRCHL<<8;
yao6116601 0:998a68b8defc 206 CRCHL|=uchCRCLo;
yao6116601 0:998a68b8defc 207 return CRCHL;//(uchCRCHi<<8|uchCRCLo) ;//uchCRCHi:0xa1;uchCRCLo:2B
yao6116601 0:998a68b8defc 208 }