Software modbus, display, knoppen, bewegingssensor en mbed OS

Dependents:   testSoftware8_alles_display

Committer:
mikevd1
Date:
Mon Oct 01 12:04:27 2018 +0000
Revision:
5:6bafdac3cd5a
Parent:
4:80135cfbaeb7
geen aanpassingen ;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mikevd1 0:d603d31b42dc 1 #include "mbed.h"
mikevd1 0:d603d31b42dc 2 #include "setup.h"
mikevd1 2:ca3d2395b6d3 3 #include "ssd1306.h"
mikevd1 0:d603d31b42dc 4
mikevd1 2:ca3d2395b6d3 5 //UART to RS485 bus line & enable of RS485
mikevd1 2:ca3d2395b6d3 6 Serial bus(PA_9,PA_10); //rx , tx
mikevd1 0:d603d31b42dc 7 DigitalOut activateSN65(PA_8);
mikevd1 0:d603d31b42dc 8
mikevd1 2:ca3d2395b6d3 9 // Timer max message time
mikevd1 2:ca3d2395b6d3 10 Timer maxMessageTime ;
mikevd1 0:d603d31b42dc 11
mikevd1 4:80135cfbaeb7 12 // buffer message package
mikevd1 0:d603d31b42dc 13 unsigned static int buffer = 0;
mikevd1 2:ca3d2395b6d3 14
mikevd1 0:d603d31b42dc 15 unsigned int dataReceived[10] ;
mikevd1 0:d603d31b42dc 16 unsigned int dataSend[10] ;
mikevd1 2:ca3d2395b6d3 17 unsigned int count = 0, count1 = 0, count2 = 0 ;
mikevd1 4:80135cfbaeb7 18 static unsigned int maxSizeBuffer = 0 ;
mikevd1 2:ca3d2395b6d3 19
mikevd1 2:ca3d2395b6d3 20 int modbus_read(dataModbus *allData)
mikevd1 0:d603d31b42dc 21 {
mikevd1 2:ca3d2395b6d3 22 activateSN65 = 0 ;
mikevd1 4:80135cfbaeb7 23
mikevd1 2:ca3d2395b6d3 24 if(bus.readable())
mikevd1 2:ca3d2395b6d3 25 {
mikevd1 2:ca3d2395b6d3 26 // if( maxMessageTime.read_ms() > (int)180 )
mikevd1 2:ca3d2395b6d3 27 // {
mikevd1 2:ca3d2395b6d3 28 // buffer = 0 ;
mikevd1 2:ca3d2395b6d3 29 // maxMessageTime.reset();
mikevd1 2:ca3d2395b6d3 30 // }
mikevd1 2:ca3d2395b6d3 31 int data = bus.getc();
mikevd1 3:4fff62544119 32 if ( data == allData->slaveID && buffer == 0 )
mikevd1 2:ca3d2395b6d3 33 {
mikevd1 2:ca3d2395b6d3 34 // maxMessageTime.start();
mikevd1 3:4fff62544119 35 dataReceived[buffer++] = allData->slaveID ; // slaveID checks out proceed to checking function ID
mikevd1 2:ca3d2395b6d3 36 }
mikevd1 2:ca3d2395b6d3 37 else if ( buffer == 1 )
mikevd1 2:ca3d2395b6d3 38 {
mikevd1 2:ca3d2395b6d3 39 dataReceived[buffer++] = data ; // functie code
mikevd1 2:ca3d2395b6d3 40 maxSizeBuffer = functionCodeCheckMaxData(data) ;
mikevd1 2:ca3d2395b6d3 41 }
mikevd1 2:ca3d2395b6d3 42 else if ( ( buffer >= 2 ) && ( buffer < maxSizeBuffer ) )
mikevd1 2:ca3d2395b6d3 43 {
mikevd1 2:ca3d2395b6d3 44 dataReceived[buffer++] = data ; // data
mikevd1 2:ca3d2395b6d3 45 }
mikevd1 2:ca3d2395b6d3 46 else if ( buffer == maxSizeBuffer || buffer == maxSizeBuffer + 1 )
mikevd1 2:ca3d2395b6d3 47 {
mikevd1 2:ca3d2395b6d3 48 if (buffer == maxSizeBuffer )
mikevd1 0:d603d31b42dc 49 {
mikevd1 2:ca3d2395b6d3 50 dataReceived[buffer++] = data ;
mikevd1 0:d603d31b42dc 51 }
mikevd1 0:d603d31b42dc 52 else
mikevd1 0:d603d31b42dc 53 {
mikevd1 2:ca3d2395b6d3 54 dataReceived[buffer++] = data ;
mikevd1 2:ca3d2395b6d3 55 //buffer++;
mikevd1 2:ca3d2395b6d3 56 CRCChecking();
mikevd1 5:6bafdac3cd5a 57 }
mikevd1 0:d603d31b42dc 58 }
mikevd1 2:ca3d2395b6d3 59 else
mikevd1 0:d603d31b42dc 60 {
mikevd1 2:ca3d2395b6d3 61 buffer = 0 ;
mikevd1 2:ca3d2395b6d3 62 // maxMessageTime.stop();
mikevd1 2:ca3d2395b6d3 63 // maxMessageTime.reset();
mikevd1 2:ca3d2395b6d3 64 }
mikevd1 2:ca3d2395b6d3 65 }
mikevd1 2:ca3d2395b6d3 66 if ( buffer == maxSizeBuffer + 3 )
mikevd1 2:ca3d2395b6d3 67 {
mikevd1 2:ca3d2395b6d3 68 // maxMessageTime.stop();
mikevd1 2:ca3d2395b6d3 69 // maxMessageTime.reset();
mikevd1 2:ca3d2395b6d3 70 return modbus_sendData(allData);
mikevd1 2:ca3d2395b6d3 71 }
mikevd1 0:d603d31b42dc 72
mikevd1 0:d603d31b42dc 73 return 0 ;
mikevd1 0:d603d31b42dc 74 }
mikevd1 0:d603d31b42dc 75
mikevd1 2:ca3d2395b6d3 76 int modbus_sendData(dataModbus *allData)
mikevd1 0:d603d31b42dc 77 {
mikevd1 2:ca3d2395b6d3 78 int i = 0 ;
mikevd1 1:92f9d6cec962 79 int maxData = 0 ;
mikevd1 4:80135cfbaeb7 80 static int temperatuur = 0, beweging = 0 ;
mikevd1 2:ca3d2395b6d3 81 temperatuur = allData->temperatuur ;
mikevd1 2:ca3d2395b6d3 82 beweging = allData->beweging ;
mikevd1 3:4fff62544119 83 dataSend[maxData++] = allData->slaveID ;
mikevd1 1:92f9d6cec962 84 dataSend[maxData++] = dataReceived[1];
mikevd1 2:ca3d2395b6d3 85 if ( dataReceived[1] == 0x02 )
mikevd1 2:ca3d2395b6d3 86 {
mikevd1 2:ca3d2395b6d3 87 dataSend[maxData++] = 0x01 ; // byte count
mikevd1 2:ca3d2395b6d3 88 dataSend[maxData++] = 0x01 ;
mikevd1 2:ca3d2395b6d3 89 }
mikevd1 2:ca3d2395b6d3 90 else if (dataReceived[1] == 0x04 )
mikevd1 2:ca3d2395b6d3 91 {
mikevd1 2:ca3d2395b6d3 92 if ( dataReceived[3] == 0 )
mikevd1 2:ca3d2395b6d3 93 {
mikevd1 2:ca3d2395b6d3 94 dataSend[maxData++] = 0x02 ; // byte count
mikevd1 2:ca3d2395b6d3 95 dataSend[maxData++] = 0x00 ;
mikevd1 2:ca3d2395b6d3 96 dataSend[maxData++] = temperatuur ;
mikevd1 2:ca3d2395b6d3 97 }
mikevd1 2:ca3d2395b6d3 98 else
mikevd1 2:ca3d2395b6d3 99 {
mikevd1 2:ca3d2395b6d3 100 dataSend[maxData++] = 0x02 ; // byte count
mikevd1 2:ca3d2395b6d3 101 dataSend[maxData++] = 0x00 ;
mikevd1 2:ca3d2395b6d3 102 dataSend[maxData++] = beweging ;
mikevd1 2:ca3d2395b6d3 103 }
mikevd1 2:ca3d2395b6d3 104 }
mikevd1 2:ca3d2395b6d3 105 else if ( dataReceived[1] == 0x06 )
mikevd1 0:d603d31b42dc 106 {
mikevd1 2:ca3d2395b6d3 107 dataSend[maxData++] = dataReceived[2] ; //echo
mikevd1 2:ca3d2395b6d3 108 dataSend[maxData++] = dataReceived[3] ;
mikevd1 2:ca3d2395b6d3 109 dataSend[maxData++] = dataReceived[4] ;
mikevd1 2:ca3d2395b6d3 110 dataSend[maxData++] = dataReceived[5] ;
mikevd1 2:ca3d2395b6d3 111 if ( dataReceived[3] == 0x01 )
mikevd1 2:ca3d2395b6d3 112 {
mikevd1 2:ca3d2395b6d3 113 count2 = dataReceived[5] ;
mikevd1 2:ca3d2395b6d3 114 if ( count2 == 1 )
mikevd1 2:ca3d2395b6d3 115 {
mikevd1 2:ca3d2395b6d3 116 allData->RelaisK1 = 1 ;
mikevd1 2:ca3d2395b6d3 117 }
mikevd1 2:ca3d2395b6d3 118 else
mikevd1 2:ca3d2395b6d3 119 {
mikevd1 2:ca3d2395b6d3 120 allData->RelaisK1 = 0 ;
mikevd1 2:ca3d2395b6d3 121 }
mikevd1 2:ca3d2395b6d3 122 }
mikevd1 2:ca3d2395b6d3 123 else
mikevd1 2:ca3d2395b6d3 124 {
mikevd1 2:ca3d2395b6d3 125 count1 = dataReceived[5] ;
mikevd1 2:ca3d2395b6d3 126 if ( count1 == 1 )
mikevd1 2:ca3d2395b6d3 127 {
mikevd1 2:ca3d2395b6d3 128 allData->RelaisK2 = 1 ;
mikevd1 2:ca3d2395b6d3 129 }
mikevd1 2:ca3d2395b6d3 130 else
mikevd1 2:ca3d2395b6d3 131 {
mikevd1 2:ca3d2395b6d3 132 allData->RelaisK2 = 0 ;
mikevd1 2:ca3d2395b6d3 133 }
mikevd1 2:ca3d2395b6d3 134 }
mikevd1 2:ca3d2395b6d3 135 temperatuur = allData->temperatuur ; //count2 * count1 ;
mikevd1 2:ca3d2395b6d3 136 beweging = allData->beweging ;
mikevd1 2:ca3d2395b6d3 137 }
mikevd1 1:92f9d6cec962 138 else
mikevd1 2:ca3d2395b6d3 139 {
mikevd1 2:ca3d2395b6d3 140 return 0 ;
mikevd1 1:92f9d6cec962 141 }
mikevd1 1:92f9d6cec962 142 uint16_t temp = calculate_crc16_Modbus(dataSend, maxData ) ;
mikevd1 1:92f9d6cec962 143 dataSend[maxData++] = (uint8_t)temp ;
mikevd1 1:92f9d6cec962 144 dataSend[maxData++] = ((uint16_t)temp>>8);
mikevd1 0:d603d31b42dc 145 activateSN65 = 1 ;
mikevd1 0:d603d31b42dc 146 wait_ms(3);
mikevd1 1:92f9d6cec962 147 for(i = 0 ; i < maxData ; i++ )
mikevd1 0:d603d31b42dc 148 {
mikevd1 0:d603d31b42dc 149 bus.putc(dataSend[i]);
mikevd1 0:d603d31b42dc 150 wait_us(750);
mikevd1 0:d603d31b42dc 151 }
mikevd1 2:ca3d2395b6d3 152
mikevd1 0:d603d31b42dc 153 buffer = 0 ;
mikevd1 0:d603d31b42dc 154 wait_ms(2) ;
mikevd1 0:d603d31b42dc 155 activateSN65 = 0 ;
mikevd1 0:d603d31b42dc 156
mikevd1 2:ca3d2395b6d3 157 if ( allData->RelaisK1 == 1 && allData->RelaisK2 == 1 )
mikevd1 2:ca3d2395b6d3 158 {
mikevd1 2:ca3d2395b6d3 159 return 1 ;
mikevd1 2:ca3d2395b6d3 160 }
mikevd1 2:ca3d2395b6d3 161 else if ( allData->RelaisK1 == 0 && allData->RelaisK2 == 1 )
mikevd1 2:ca3d2395b6d3 162 {
mikevd1 2:ca3d2395b6d3 163 return 2 ;
mikevd1 2:ca3d2395b6d3 164 }
mikevd1 2:ca3d2395b6d3 165 else if ( allData->RelaisK1 == 1 && allData->RelaisK2 == 0 )
mikevd1 2:ca3d2395b6d3 166 {
mikevd1 2:ca3d2395b6d3 167 return 3 ;
mikevd1 2:ca3d2395b6d3 168 }
mikevd1 2:ca3d2395b6d3 169 else if ( allData->RelaisK1 == 0 && allData->RelaisK2 == 0 )
mikevd1 2:ca3d2395b6d3 170 {
mikevd1 2:ca3d2395b6d3 171 return 4 ;
mikevd1 2:ca3d2395b6d3 172 }
mikevd1 2:ca3d2395b6d3 173 else
mikevd1 2:ca3d2395b6d3 174 {
mikevd1 2:ca3d2395b6d3 175 return 0 ;
mikevd1 2:ca3d2395b6d3 176 }
mikevd1 2:ca3d2395b6d3 177
mikevd1 0:d603d31b42dc 178 }
mikevd1 0:d603d31b42dc 179
mikevd1 0:d603d31b42dc 180 uint16_t update_crc16_reflected(const uint16_t *table, uint16_t crc, unsigned int c )
mikevd1 0:d603d31b42dc 181 {
mikevd1 0:d603d31b42dc 182 long short_c;
mikevd1 0:d603d31b42dc 183
mikevd1 0:d603d31b42dc 184 short_c = 0x00ff & (long) c;
mikevd1 0:d603d31b42dc 185
mikevd1 0:d603d31b42dc 186 /* Reflected form */
mikevd1 0:d603d31b42dc 187 return (crc >> 8) ^ table[(crc ^ short_c) & 0xff];
mikevd1 0:d603d31b42dc 188 }
mikevd1 0:d603d31b42dc 189
mikevd1 0:d603d31b42dc 190
mikevd1 0:d603d31b42dc 191 uint16_t update_crc16_A001( long crc, unsigned int c )
mikevd1 0:d603d31b42dc 192 {
mikevd1 0:d603d31b42dc 193 return update_crc16_reflected(crc_tab_8005_reflected,crc,c);
mikevd1 0:d603d31b42dc 194 }
mikevd1 0:d603d31b42dc 195
mikevd1 0:d603d31b42dc 196
mikevd1 0:d603d31b42dc 197 long calculate_crc16_Modbus(unsigned int *p, unsigned int length)
mikevd1 0:d603d31b42dc 198 {
mikevd1 0:d603d31b42dc 199 long crc;
mikevd1 0:d603d31b42dc 200 unsigned int i;
mikevd1 0:d603d31b42dc 201
mikevd1 0:d603d31b42dc 202 crc = 0xFFFF;
mikevd1 0:d603d31b42dc 203
mikevd1 0:d603d31b42dc 204 for (i=0; i < length; i++)
mikevd1 0:d603d31b42dc 205 {
mikevd1 0:d603d31b42dc 206 crc = update_crc16_A001(crc,*p++);
mikevd1 0:d603d31b42dc 207 }
mikevd1 0:d603d31b42dc 208 return crc;
mikevd1 0:d603d31b42dc 209 }
mikevd1 0:d603d31b42dc 210
mikevd1 2:ca3d2395b6d3 211 int functionCodeCheckMaxData(int functionCode)
mikevd1 2:ca3d2395b6d3 212 {
mikevd1 2:ca3d2395b6d3 213 // exclude function code in byte count
mikevd1 2:ca3d2395b6d3 214 switch (functionCode)
mikevd1 2:ca3d2395b6d3 215 {
mikevd1 2:ca3d2395b6d3 216 // function code 0x01
mikevd1 2:ca3d2395b6d3 217 case 0x02 :
mikevd1 2:ca3d2395b6d3 218 return 0x04 + 2 ;
mikevd1 2:ca3d2395b6d3 219 //break ;
mikevd1 2:ca3d2395b6d3 220 case 0x04 :
mikevd1 2:ca3d2395b6d3 221 return 0x04 + 2 ;
mikevd1 2:ca3d2395b6d3 222 //break ;
mikevd1 2:ca3d2395b6d3 223 case 0x06 :
mikevd1 2:ca3d2395b6d3 224 return 0x04 + 2 ;
mikevd1 2:ca3d2395b6d3 225 default :
mikevd1 2:ca3d2395b6d3 226 buffer = 0 ;
mikevd1 2:ca3d2395b6d3 227 return 0 ;
mikevd1 2:ca3d2395b6d3 228 }
mikevd1 2:ca3d2395b6d3 229 }
mikevd1 2:ca3d2395b6d3 230 void CRCChecking(void)
mikevd1 2:ca3d2395b6d3 231 {
mikevd1 2:ca3d2395b6d3 232 unsigned long crc = ((dataReceived[buffer - 1] << 8) | dataReceived[buffer - 2]); // combine the crc Low & High bytes
mikevd1 2:ca3d2395b6d3 233 if ( calculate_crc16_Modbus(dataReceived, buffer - 2 ) == crc )
mikevd1 2:ca3d2395b6d3 234 {
mikevd1 2:ca3d2395b6d3 235 buffer++ ;
mikevd1 2:ca3d2395b6d3 236 }
mikevd1 2:ca3d2395b6d3 237 else
mikevd1 2:ca3d2395b6d3 238 {
mikevd1 2:ca3d2395b6d3 239 buffer = 0 ;
mikevd1 2:ca3d2395b6d3 240 }
mikevd1 2:ca3d2395b6d3 241 }