share

Dependencies:   mbed ATParser2

Committer:
ohlaaa
Date:
Mon Mar 23 08:05:49 2020 +0000
Revision:
2:9497b91d341a
Parent:
1:17ea42de4a8a
12345;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nobelkuo 0:6fd21c22a0c9 1 #include "mbed.h"
ohlaaa 2:9497b91d341a 2 #include "ATParser.h"
ohlaaa 2:9497b91d341a 3 #include "string.h"
ohlaaa 2:9497b91d341a 4 #include <string>
ohlaaa 2:9497b91d341a 5
ohlaaa 2:9497b91d341a 6
ohlaaa 1:17ea42de4a8a 7 Serial pc(USBTX, USBRX); //((USBTX, USBRX))
ohlaaa 1:17ea42de4a8a 8 Serial modbus(PA_9, PA_10,9600);//(tx,rx,baud)
ohlaaa 2:9497b91d341a 9 //Serial evk(PC_10,PC_11,115200); //CN7
ohlaaa 2:9497b91d341a 10 BufferedSerial evk(PC_10,PC_11,1024);
ohlaaa 2:9497b91d341a 11 ATParser at=ATParser(evk,"\r\n");
ohlaaa 2:9497b91d341a 12
ohlaaa 1:17ea42de4a8a 13 DigitalOut DE_RE(PA_8);
nobelkuo 0:6fd21c22a0c9 14
nobelkuo 0:6fd21c22a0c9 15 int Max_Byte=8;
ohlaaa 2:9497b91d341a 16 uint8_t mdb_Command_buffer[11];
nobelkuo 0:6fd21c22a0c9 17 uint8_t mdb_Message_buffer[8];
nobelkuo 0:6fd21c22a0c9 18 uint16_t RTU_CRC;
nobelkuo 0:6fd21c22a0c9 19 int message_in_byte=0;
ohlaaa 2:9497b91d341a 20 char device_counter=1;
ohlaaa 2:9497b91d341a 21 int D3_X0 = 0;
ohlaaa 2:9497b91d341a 22 int D1_X0 = 0;
ohlaaa 2:9497b91d341a 23 char server_buff[27]={0};
ohlaaa 2:9497b91d341a 24
ohlaaa 2:9497b91d341a 25
ohlaaa 2:9497b91d341a 26 void AT_publish(char* s);
ohlaaa 2:9497b91d341a 27
nobelkuo 0:6fd21c22a0c9 28 struct mdb_Data_Set {
nobelkuo 0:6fd21c22a0c9 29 uint8_t Addr;
nobelkuo 0:6fd21c22a0c9 30 uint8_t Data[8];
nobelkuo 0:6fd21c22a0c9 31 };
nobelkuo 0:6fd21c22a0c9 32 mdb_Data_Set mdb_Data[8];
nobelkuo 0:6fd21c22a0c9 33
nobelkuo 0:6fd21c22a0c9 34 //MODBUS RTU CRC
nobelkuo 0:6fd21c22a0c9 35 uint16_t mdbRTU_CRC(uint8_t message[], int Length)
nobelkuo 0:6fd21c22a0c9 36 {
nobelkuo 0:6fd21c22a0c9 37 RTU_CRC=0xffff;
nobelkuo 0:6fd21c22a0c9 38 for (int pos =0 ; pos < Length ; pos++)
nobelkuo 0:6fd21c22a0c9 39 {
nobelkuo 0:6fd21c22a0c9 40 RTU_CRC ^= (uint16_t)message[pos];
nobelkuo 0:6fd21c22a0c9 41
nobelkuo 0:6fd21c22a0c9 42 for (int i = 8; i != 0; i--)
nobelkuo 0:6fd21c22a0c9 43 {
nobelkuo 0:6fd21c22a0c9 44 if ((RTU_CRC & 0x0001) != 0)
nobelkuo 0:6fd21c22a0c9 45 {
nobelkuo 0:6fd21c22a0c9 46 RTU_CRC >>= 1;
nobelkuo 0:6fd21c22a0c9 47 RTU_CRC ^= 0xA001;
nobelkuo 0:6fd21c22a0c9 48 }
nobelkuo 0:6fd21c22a0c9 49 else
nobelkuo 0:6fd21c22a0c9 50 RTU_CRC >>= 1;
nobelkuo 0:6fd21c22a0c9 51 }
nobelkuo 0:6fd21c22a0c9 52 }
nobelkuo 0:6fd21c22a0c9 53 return RTU_CRC;
nobelkuo 0:6fd21c22a0c9 54 //pc.printf("hi_mdb_CRC= %02X \r\n", hi_RTU_CRC);
nobelkuo 0:6fd21c22a0c9 55 //pc.printf("lo_mdb_CRC= %02X \r\n", lo_RTU_CRC);
nobelkuo 0:6fd21c22a0c9 56 }
nobelkuo 0:6fd21c22a0c9 57
ohlaaa 2:9497b91d341a 58 //char to int
ohlaaa 2:9497b91d341a 59
ohlaaa 2:9497b91d341a 60 int char_to_int(char high,char low){
ohlaaa 2:9497b91d341a 61 int H = high - 48;
ohlaaa 2:9497b91d341a 62 int L = low - 48;
ohlaaa 2:9497b91d341a 63 int K = H*10 + L -30;
nobelkuo 0:6fd21c22a0c9 64
ohlaaa 2:9497b91d341a 65 if(K == 31) return 10;
ohlaaa 2:9497b91d341a 66 else if(K == 32) return 11;
ohlaaa 2:9497b91d341a 67 else if(K == 33) return 12;
ohlaaa 2:9497b91d341a 68 else if(K == 34) return 13;
ohlaaa 2:9497b91d341a 69 else if(K == 35) return 14;
ohlaaa 2:9497b91d341a 70 else if(K == 36) return 15;
ohlaaa 2:9497b91d341a 71 else return K;
ohlaaa 2:9497b91d341a 72 }
ohlaaa 2:9497b91d341a 73
ohlaaa 2:9497b91d341a 74
ohlaaa 2:9497b91d341a 75 //write modbus_data
ohlaaa 2:9497b91d341a 76 void mdb_control(uint8_t action){
ohlaaa 2:9497b91d341a 77 int dev_Addr = 0;
ohlaaa 2:9497b91d341a 78 int command_length=0;
ohlaaa 2:9497b91d341a 79 uint8_t hi_RTU_CRC ;
ohlaaa 2:9497b91d341a 80 uint8_t lo_RTU_CRC ;
ohlaaa 2:9497b91d341a 81
ohlaaa 2:9497b91d341a 82
ohlaaa 2:9497b91d341a 83 switch(action){
ohlaaa 2:9497b91d341a 84
ohlaaa 2:9497b91d341a 85 case 1:
ohlaaa 2:9497b91d341a 86 dev_Addr = 1;
ohlaaa 2:9497b91d341a 87 pc.printf("Device 1 task 1 : Read X0~X7 \n");
ohlaaa 2:9497b91d341a 88 command_length = 8;
ohlaaa 2:9497b91d341a 89 //pc.printf("\nRead from device %d at %04x with length %04x\n",dev_Addr,Hold_Reg,Reg_len);
ohlaaa 2:9497b91d341a 90 mdb_Command_buffer[0]=(uint8_t) 0x01;
ohlaaa 2:9497b91d341a 91 mdb_Command_buffer[1]=(uint8_t) 0x02;
ohlaaa 2:9497b91d341a 92 mdb_Command_buffer[2]=(uint8_t) 0x04;
ohlaaa 2:9497b91d341a 93 mdb_Command_buffer[3]=(uint8_t) 0x10;
ohlaaa 2:9497b91d341a 94 mdb_Command_buffer[4]=(uint8_t) 0x00;
ohlaaa 2:9497b91d341a 95 mdb_Command_buffer[5]=(uint8_t) 0x08;
nobelkuo 0:6fd21c22a0c9 96 RTU_CRC=mdbRTU_CRC(mdb_Command_buffer,6);
ohlaaa 2:9497b91d341a 97 hi_RTU_CRC = (uint8_t)((RTU_CRC & 0xFF00) >> 8);
ohlaaa 2:9497b91d341a 98 lo_RTU_CRC = (uint8_t)(RTU_CRC & 0x00FF);
ohlaaa 2:9497b91d341a 99 mdb_Command_buffer[6]=lo_RTU_CRC;
ohlaaa 2:9497b91d341a 100 mdb_Command_buffer[7]=hi_RTU_CRC;
ohlaaa 2:9497b91d341a 101 break;
ohlaaa 2:9497b91d341a 102
ohlaaa 2:9497b91d341a 103 case 2:
ohlaaa 2:9497b91d341a 104 dev_Addr = 1;
ohlaaa 2:9497b91d341a 105 pc.printf("Device 1 task2 : Y3 change state every 5 seconds \n");
ohlaaa 2:9497b91d341a 106 command_length = 8;
ohlaaa 2:9497b91d341a 107 //pc.printf("\nRead from device %d at %04x with length %04x\n",dev_Addr,Hold_Reg,Reg_len);
ohlaaa 2:9497b91d341a 108 mdb_Command_buffer[0]=(uint8_t) 0x01;
ohlaaa 2:9497b91d341a 109 mdb_Command_buffer[1]=(uint8_t) 0x01;
ohlaaa 2:9497b91d341a 110 mdb_Command_buffer[2]=(uint8_t) 0x05;
ohlaaa 2:9497b91d341a 111 mdb_Command_buffer[3]=(uint8_t) 0x03;
ohlaaa 2:9497b91d341a 112 mdb_Command_buffer[4]=(uint8_t) 0x00;
ohlaaa 2:9497b91d341a 113 mdb_Command_buffer[5]=(uint8_t) 0x01;
ohlaaa 2:9497b91d341a 114 RTU_CRC=mdbRTU_CRC(mdb_Command_buffer,6);
ohlaaa 2:9497b91d341a 115 hi_RTU_CRC = (uint8_t)((RTU_CRC & 0xFF00) >> 8);
ohlaaa 2:9497b91d341a 116 lo_RTU_CRC = (uint8_t)(RTU_CRC & 0x00FF);
ohlaaa 2:9497b91d341a 117 mdb_Command_buffer[6]=lo_RTU_CRC;
ohlaaa 2:9497b91d341a 118 mdb_Command_buffer[7]=hi_RTU_CRC;
ohlaaa 2:9497b91d341a 119 break;
ohlaaa 2:9497b91d341a 120
ohlaaa 2:9497b91d341a 121 case 3:
ohlaaa 2:9497b91d341a 122 dev_Addr = 1;
ohlaaa 2:9497b91d341a 123 pc.printf("Device 1 task 3 : Read D0 which add 1 per second \n");
ohlaaa 2:9497b91d341a 124 command_length = 8;
ohlaaa 2:9497b91d341a 125 //pc.printf("\nRead from device %d at %04x with length %04x\n",dev_Addr,Hold_Reg,Reg_len);
ohlaaa 2:9497b91d341a 126 mdb_Command_buffer[0]=(uint8_t) 0x01;
ohlaaa 2:9497b91d341a 127 mdb_Command_buffer[1]=(uint8_t) 0x03;
ohlaaa 2:9497b91d341a 128 mdb_Command_buffer[2]=(uint8_t) 0x10;
ohlaaa 2:9497b91d341a 129 mdb_Command_buffer[3]=(uint8_t) 0x00;
ohlaaa 2:9497b91d341a 130 mdb_Command_buffer[4]=(uint8_t) 0x00;
ohlaaa 2:9497b91d341a 131 mdb_Command_buffer[5]=(uint8_t) 0x01;
ohlaaa 2:9497b91d341a 132 RTU_CRC=mdbRTU_CRC(mdb_Command_buffer,6);
ohlaaa 2:9497b91d341a 133 hi_RTU_CRC = (uint8_t)((RTU_CRC & 0xFF00) >> 8);
ohlaaa 2:9497b91d341a 134 lo_RTU_CRC = (uint8_t)(RTU_CRC & 0x00FF);
ohlaaa 2:9497b91d341a 135 mdb_Command_buffer[6]=lo_RTU_CRC;
ohlaaa 2:9497b91d341a 136 mdb_Command_buffer[7]=hi_RTU_CRC;
ohlaaa 2:9497b91d341a 137 break;
ohlaaa 2:9497b91d341a 138
ohlaaa 2:9497b91d341a 139 case 4:
ohlaaa 2:9497b91d341a 140 dev_Addr = 1;
ohlaaa 2:9497b91d341a 141 if(D1_X0==0)pc.printf("Device 1 task 4 : Set X0 ON\n");
ohlaaa 2:9497b91d341a 142 else pc.printf("Device 1 task 4 : Set X0 OFF\n");
ohlaaa 2:9497b91d341a 143 command_length = 8;
ohlaaa 2:9497b91d341a 144 //pc.printf("\nRead from device %d at %04x with length %04x\n",dev_Addr,Hold_Reg,Reg_len);
ohlaaa 2:9497b91d341a 145 mdb_Command_buffer[0]=(uint8_t) 0x01;
ohlaaa 2:9497b91d341a 146 mdb_Command_buffer[1]=(uint8_t) 0x05;
ohlaaa 2:9497b91d341a 147 mdb_Command_buffer[2]=(uint8_t) 0x05;
ohlaaa 2:9497b91d341a 148 mdb_Command_buffer[3]=(uint8_t) 0x00;
ohlaaa 2:9497b91d341a 149 if(D1_X0==0)mdb_Command_buffer[4]=(uint8_t) 0xFF;
ohlaaa 2:9497b91d341a 150 else mdb_Command_buffer[4]=(uint8_t) 0x00;
ohlaaa 2:9497b91d341a 151 mdb_Command_buffer[5]=(uint8_t) 0x00;
ohlaaa 2:9497b91d341a 152 RTU_CRC=mdbRTU_CRC(mdb_Command_buffer,6);
ohlaaa 2:9497b91d341a 153 hi_RTU_CRC = (uint8_t)((RTU_CRC & 0xFF00) >> 8);
ohlaaa 2:9497b91d341a 154 lo_RTU_CRC = (uint8_t)(RTU_CRC & 0x00FF);
nobelkuo 0:6fd21c22a0c9 155 mdb_Command_buffer[6]=lo_RTU_CRC;
nobelkuo 0:6fd21c22a0c9 156 mdb_Command_buffer[7]=hi_RTU_CRC;
ohlaaa 2:9497b91d341a 157 break;
nobelkuo 0:6fd21c22a0c9 158
ohlaaa 2:9497b91d341a 159 case 5:
ohlaaa 2:9497b91d341a 160 dev_Addr = 1;
ohlaaa 2:9497b91d341a 161 pc.printf("Device 1 task 4 : Read Y0 \n");
ohlaaa 2:9497b91d341a 162 command_length = 8;
ohlaaa 2:9497b91d341a 163 //pc.printf("\nRead from device %d at %04x with length %04x\n",dev_Addr,Hold_Reg,Reg_len);
ohlaaa 2:9497b91d341a 164 mdb_Command_buffer[0]=(uint8_t) 0x01;
ohlaaa 2:9497b91d341a 165 mdb_Command_buffer[1]=(uint8_t) 0x02;
ohlaaa 2:9497b91d341a 166 mdb_Command_buffer[2]=(uint8_t) 0x04;
ohlaaa 2:9497b91d341a 167 mdb_Command_buffer[3]=(uint8_t) 0x00;
ohlaaa 2:9497b91d341a 168 mdb_Command_buffer[4]=(uint8_t) 0x00;
ohlaaa 2:9497b91d341a 169 mdb_Command_buffer[5]=(uint8_t) 0x08;
ohlaaa 2:9497b91d341a 170 RTU_CRC=mdbRTU_CRC(mdb_Command_buffer,6);
ohlaaa 2:9497b91d341a 171 hi_RTU_CRC = (uint8_t)((RTU_CRC & 0xFF00) >> 8);
ohlaaa 2:9497b91d341a 172 lo_RTU_CRC = (uint8_t)(RTU_CRC & 0x00FF);
ohlaaa 2:9497b91d341a 173 mdb_Command_buffer[6]=lo_RTU_CRC;
ohlaaa 2:9497b91d341a 174 mdb_Command_buffer[7]=hi_RTU_CRC;
ohlaaa 2:9497b91d341a 175 break;
ohlaaa 2:9497b91d341a 176
ohlaaa 2:9497b91d341a 177 case 6:
ohlaaa 2:9497b91d341a 178 dev_Addr = 2;
ohlaaa 2:9497b91d341a 179 pc.printf("Device 2 : Voltage \n");
ohlaaa 2:9497b91d341a 180 command_length = 8;
ohlaaa 2:9497b91d341a 181 //pc.printf("\nRead from device %d at %04x with length %04x\n",dev_Addr,Hold_Reg,Reg_len);
ohlaaa 2:9497b91d341a 182 mdb_Command_buffer[0]=(uint8_t) 0x02;
ohlaaa 2:9497b91d341a 183 mdb_Command_buffer[1]=(uint8_t) 0x03;
ohlaaa 2:9497b91d341a 184 mdb_Command_buffer[2]=(uint8_t) 0x01;
ohlaaa 2:9497b91d341a 185 mdb_Command_buffer[3]=(uint8_t) 0x06;
ohlaaa 2:9497b91d341a 186 mdb_Command_buffer[4]=(uint8_t) 0x00;
ohlaaa 2:9497b91d341a 187 mdb_Command_buffer[5]=(uint8_t) 0x02;
ohlaaa 2:9497b91d341a 188 RTU_CRC=mdbRTU_CRC(mdb_Command_buffer,6);
ohlaaa 2:9497b91d341a 189 hi_RTU_CRC = (uint8_t)((RTU_CRC & 0xFF00) >> 8);
ohlaaa 2:9497b91d341a 190 lo_RTU_CRC = (uint8_t)(RTU_CRC & 0x00FF);
ohlaaa 2:9497b91d341a 191 mdb_Command_buffer[6]=lo_RTU_CRC;
ohlaaa 2:9497b91d341a 192 mdb_Command_buffer[7]=hi_RTU_CRC;
ohlaaa 2:9497b91d341a 193 break;
ohlaaa 2:9497b91d341a 194
ohlaaa 2:9497b91d341a 195 case 7:
ohlaaa 2:9497b91d341a 196 dev_Addr = 3;
ohlaaa 2:9497b91d341a 197 if(D3_X0==0)pc.printf("Device 3 : Set X0 ON\n");
ohlaaa 2:9497b91d341a 198 else pc.printf("Device 3 : Set X0 OFF\n");
ohlaaa 2:9497b91d341a 199 command_length = 8;
ohlaaa 2:9497b91d341a 200 //pc.printf("\nRead from device %d at %04x with length %04x\n",dev_Addr,Hold_Reg,Reg_len);
ohlaaa 2:9497b91d341a 201 mdb_Command_buffer[0]=(uint8_t) 0x03;
ohlaaa 2:9497b91d341a 202 mdb_Command_buffer[1]=(uint8_t) 0x05;
ohlaaa 2:9497b91d341a 203 mdb_Command_buffer[2]=(uint8_t) 0x05;
ohlaaa 2:9497b91d341a 204 mdb_Command_buffer[3]=(uint8_t) 0x00;
ohlaaa 2:9497b91d341a 205 if(D3_X0==0)mdb_Command_buffer[4]=(uint8_t) 0xFF;
ohlaaa 2:9497b91d341a 206 else mdb_Command_buffer[4]=(uint8_t) 0x00;
ohlaaa 2:9497b91d341a 207 mdb_Command_buffer[5]=(uint8_t) 0x00;
ohlaaa 2:9497b91d341a 208 RTU_CRC=mdbRTU_CRC(mdb_Command_buffer,6);
ohlaaa 2:9497b91d341a 209 hi_RTU_CRC = (uint8_t)((RTU_CRC & 0xFF00) >> 8);
ohlaaa 2:9497b91d341a 210 lo_RTU_CRC = (uint8_t)(RTU_CRC & 0x00FF);
ohlaaa 2:9497b91d341a 211 mdb_Command_buffer[6]=lo_RTU_CRC;
ohlaaa 2:9497b91d341a 212 mdb_Command_buffer[7]=hi_RTU_CRC;
ohlaaa 2:9497b91d341a 213 break;
ohlaaa 2:9497b91d341a 214
ohlaaa 2:9497b91d341a 215 case 8:
ohlaaa 2:9497b91d341a 216 dev_Addr = 3;
ohlaaa 2:9497b91d341a 217 pc.printf("Device 3 : Read Y0 \n");
ohlaaa 2:9497b91d341a 218 command_length = 8;
ohlaaa 2:9497b91d341a 219 //pc.printf("\nRead from device %d at %04x with length %04x\n",dev_Addr,Hold_Reg,Reg_len);
ohlaaa 2:9497b91d341a 220 mdb_Command_buffer[0]=(uint8_t) 0x03;
ohlaaa 2:9497b91d341a 221 mdb_Command_buffer[1]=(uint8_t) 0x02;
ohlaaa 2:9497b91d341a 222 mdb_Command_buffer[2]=(uint8_t) 0x04;
ohlaaa 2:9497b91d341a 223 mdb_Command_buffer[3]=(uint8_t) 0x00;
ohlaaa 2:9497b91d341a 224 mdb_Command_buffer[4]=(uint8_t) 0x00;
ohlaaa 2:9497b91d341a 225 mdb_Command_buffer[5]=(uint8_t) 0x08;
ohlaaa 2:9497b91d341a 226 RTU_CRC=mdbRTU_CRC(mdb_Command_buffer,6);
ohlaaa 2:9497b91d341a 227 hi_RTU_CRC = (uint8_t)((RTU_CRC & 0xFF00) >> 8);
ohlaaa 2:9497b91d341a 228 lo_RTU_CRC = (uint8_t)(RTU_CRC & 0x00FF);
ohlaaa 2:9497b91d341a 229 mdb_Command_buffer[6]=lo_RTU_CRC;
ohlaaa 2:9497b91d341a 230 mdb_Command_buffer[7]=hi_RTU_CRC;
ohlaaa 2:9497b91d341a 231 break;
ohlaaa 2:9497b91d341a 232
ohlaaa 2:9497b91d341a 233 case 9:
ohlaaa 2:9497b91d341a 234 dev_Addr = 5;
ohlaaa 2:9497b91d341a 235 pc.printf("Device 5 : Read Temperature \n");
ohlaaa 2:9497b91d341a 236 command_length = 8;
ohlaaa 2:9497b91d341a 237 //pc.printf("\nRead from device %d at %04x with length %04x\n",dev_Addr,Hold_Reg,Reg_len);
ohlaaa 2:9497b91d341a 238 mdb_Command_buffer[0]=(uint8_t) 0x05;
ohlaaa 2:9497b91d341a 239 mdb_Command_buffer[1]=(uint8_t) 0x03;
ohlaaa 2:9497b91d341a 240 mdb_Command_buffer[2]=(uint8_t) 0x10;
ohlaaa 2:9497b91d341a 241 mdb_Command_buffer[3]=(uint8_t) 0x00;
ohlaaa 2:9497b91d341a 242 mdb_Command_buffer[4]=(uint8_t) 0x00;
ohlaaa 2:9497b91d341a 243 mdb_Command_buffer[5]=(uint8_t) 0x01;
ohlaaa 2:9497b91d341a 244 RTU_CRC=mdbRTU_CRC(mdb_Command_buffer,6);
ohlaaa 2:9497b91d341a 245 hi_RTU_CRC = (uint8_t)((RTU_CRC & 0xFF00) >> 8);
ohlaaa 2:9497b91d341a 246 lo_RTU_CRC = (uint8_t)(RTU_CRC & 0x00FF);
ohlaaa 2:9497b91d341a 247 mdb_Command_buffer[6]=lo_RTU_CRC;
ohlaaa 2:9497b91d341a 248 mdb_Command_buffer[7]=hi_RTU_CRC;
ohlaaa 2:9497b91d341a 249 break;
ohlaaa 2:9497b91d341a 250
ohlaaa 2:9497b91d341a 251 default:
ohlaaa 2:9497b91d341a 252 command_length = 0;
ohlaaa 2:9497b91d341a 253 pc.printf("there is no action in mdb_ctrol");
ohlaaa 2:9497b91d341a 254 break;
ohlaaa 2:9497b91d341a 255 }
ohlaaa 2:9497b91d341a 256
ohlaaa 2:9497b91d341a 257 // put command_buffer to modbus
nobelkuo 0:6fd21c22a0c9 258 DE_RE=1;
ohlaaa 2:9497b91d341a 259
nobelkuo 0:6fd21c22a0c9 260 int byte_of_buffer;
ohlaaa 2:9497b91d341a 261 for (byte_of_buffer=0 ; byte_of_buffer<command_length ; byte_of_buffer++)
nobelkuo 0:6fd21c22a0c9 262 {
nobelkuo 0:6fd21c22a0c9 263 modbus.putc(mdb_Command_buffer[byte_of_buffer]);
nobelkuo 0:6fd21c22a0c9 264 }
nobelkuo 0:6fd21c22a0c9 265
nobelkuo 0:6fd21c22a0c9 266 wait_ms(2);
nobelkuo 0:6fd21c22a0c9 267 message_in_byte=0;
nobelkuo 0:6fd21c22a0c9 268 DE_RE =0 ;
ohlaaa 2:9497b91d341a 269 wait_ms(80);
nobelkuo 0:6fd21c22a0c9 270 int data_len=(int)(mdb_Message_buffer[2]);
nobelkuo 0:6fd21c22a0c9 271 RTU_CRC=mdbRTU_CRC(mdb_Message_buffer,data_len+3);
nobelkuo 0:6fd21c22a0c9 272 hi_RTU_CRC = (uint8_t)((RTU_CRC & 0xFF00) >> 8);
nobelkuo 0:6fd21c22a0c9 273 lo_RTU_CRC = (uint8_t)(RTU_CRC & 0x00FF);
nobelkuo 0:6fd21c22a0c9 274 if ((mdb_Message_buffer[data_len+3]==lo_RTU_CRC)&&(mdb_Message_buffer[data_len+4]==hi_RTU_CRC))
nobelkuo 0:6fd21c22a0c9 275 {
nobelkuo 0:6fd21c22a0c9 276 for (int i=0 ; i<data_len ; i++)
nobelkuo 0:6fd21c22a0c9 277 {
nobelkuo 0:6fd21c22a0c9 278 mdb_Data[(dev_Addr)].Data[i]=mdb_Message_buffer[i+3];
nobelkuo 0:6fd21c22a0c9 279 pc.printf("mdb_Data= %02x\r\n",mdb_Data[(dev_Addr)].Data[i]);
ohlaaa 2:9497b91d341a 280 char _buff[13];
ohlaaa 2:9497b91d341a 281 sprintf((char*)_buff,"mdb_Data= %02x",mdb_Data[(dev_Addr)].Data[i]);
ohlaaa 2:9497b91d341a 282 char buff[4];
ohlaaa 2:9497b91d341a 283 sprintf((char*)buff,"%d,%02x",dev_Addr,mdb_Data[(dev_Addr)].Data[i]);
ohlaaa 2:9497b91d341a 284
nobelkuo 0:6fd21c22a0c9 285 }
nobelkuo 0:6fd21c22a0c9 286 }
nobelkuo 0:6fd21c22a0c9 287
ohlaaa 2:9497b91d341a 288 }
ohlaaa 2:9497b91d341a 289
ohlaaa 2:9497b91d341a 290
ohlaaa 2:9497b91d341a 291 //put PC input into modbus
ohlaaa 2:9497b91d341a 292 void PC_callback() {
ohlaaa 2:9497b91d341a 293 //modbus.putc(pc.getc());
ohlaaa 2:9497b91d341a 294 // char incomming_CMD;
ohlaaa 2:9497b91d341a 295 /* while (pc.readable()){
ohlaaa 2:9497b91d341a 296 incomming_CMD+=pc.getc()-1;
nobelkuo 0:6fd21c22a0c9 297 }
ohlaaa 2:9497b91d341a 298 pc.printf("Received Command :%c\r\n", incomming_CMD);*/
ohlaaa 2:9497b91d341a 299
ohlaaa 2:9497b91d341a 300 // evk.putc(pc.getc());
ohlaaa 2:9497b91d341a 301
ohlaaa 2:9497b91d341a 302 /* if(pc.readable()){
ohlaaa 2:9497b91d341a 303 ch=pc.getc();
ohlaaa 2:9497b91d341a 304 at_cmd[index++]=ch;
ohlaaa 2:9497b91d341a 305 }
ohlaaa 2:9497b91d341a 306 at_cmd[index]='\x0';
ohlaaa 2:9497b91d341a 307 pc.printf(at_cmd);*/
ohlaaa 2:9497b91d341a 308 if(pc.readable()){
ohlaaa 2:9497b91d341a 309 char at_cmd[100];
ohlaaa 2:9497b91d341a 310 int index=0;
ohlaaa 2:9497b91d341a 311 char ch;
ohlaaa 2:9497b91d341a 312 do{
ohlaaa 2:9497b91d341a 313 ch=pc.getc();
ohlaaa 2:9497b91d341a 314 at_cmd[index++]=ch;
ohlaaa 2:9497b91d341a 315 pc.printf(at_cmd);
ohlaaa 2:9497b91d341a 316 }while(ch!='\n');
ohlaaa 2:9497b91d341a 317
ohlaaa 2:9497b91d341a 318 at_cmd[index]='\x0';
ohlaaa 2:9497b91d341a 319 pc.printf(at_cmd);
ohlaaa 2:9497b91d341a 320 AT_publish(at_cmd);
ohlaaa 2:9497b91d341a 321
ohlaaa 2:9497b91d341a 322 //pc.putc(pc.getc());
ohlaaa 2:9497b91d341a 323 }
ohlaaa 2:9497b91d341a 324
ohlaaa 2:9497b91d341a 325 }
ohlaaa 2:9497b91d341a 326
ohlaaa 2:9497b91d341a 327 void evk_callback() {
ohlaaa 2:9497b91d341a 328 /* int counter=0;
ohlaaa 2:9497b91d341a 329 char ch;
ohlaaa 2:9497b91d341a 330 char evk_buff[300];
ohlaaa 2:9497b91d341a 331 while(evk.readable()){
ohlaaa 2:9497b91d341a 332 ch=at.getc();
ohlaaa 2:9497b91d341a 333 evk_buff[counter]=ch;
ohlaaa 2:9497b91d341a 334 counter++;
ohlaaa 2:9497b91d341a 335 }
ohlaaa 2:9497b91d341a 336 evk_buff[counter]='\x0';
ohlaaa 2:9497b91d341a 337 if(strncmp(evk_buff,"AT+UUSORD",9)==1){
ohlaaa 2:9497b91d341a 338
ohlaaa 2:9497b91d341a 339 }
ohlaaa 2:9497b91d341a 340 else pc.printf(evk_buff); */
ohlaaa 2:9497b91d341a 341 //pc.putc(evk.getc());
ohlaaa 2:9497b91d341a 342 //int callback_len=0;
ohlaaa 2:9497b91d341a 343 //if(at.recv("AT+USORD=0,%d",&callback_len))pc.printf(&callback_len);
ohlaaa 2:9497b91d341a 344 }
ohlaaa 2:9497b91d341a 345
ohlaaa 2:9497b91d341a 346
ohlaaa 2:9497b91d341a 347 //put modbus reply into PC
ohlaaa 2:9497b91d341a 348 void MODBUS_callback() {
ohlaaa 2:9497b91d341a 349 //pc.putc(modbus.getc());
ohlaaa 2:9497b91d341a 350 mdb_Message_buffer[message_in_byte]=modbus.getc();
ohlaaa 2:9497b91d341a 351 message_in_byte++;
ohlaaa 2:9497b91d341a 352
ohlaaa 2:9497b91d341a 353 }
ohlaaa 2:9497b91d341a 354
ohlaaa 2:9497b91d341a 355 void AT_publish(char* s){
ohlaaa 2:9497b91d341a 356 if(at.send("AT+UMQTTC=2,0,0,\"nbiot/gw\",\"%s\"",s)
ohlaaa 2:9497b91d341a 357 )pc.printf("Send \"%s\" to MQTT server\r\n",s);
ohlaaa 2:9497b91d341a 358 else pc.printf("Send \"%s\" to Server Failed !\r\n",s);
ohlaaa 2:9497b91d341a 359 at.flush();
ohlaaa 2:9497b91d341a 360 wait_ms(10);
nobelkuo 0:6fd21c22a0c9 361 }
ohlaaa 2:9497b91d341a 362
ohlaaa 2:9497b91d341a 363
ohlaaa 2:9497b91d341a 364
ohlaaa 2:9497b91d341a 365 int main() {
ohlaaa 2:9497b91d341a 366 pc.baud(115200);
ohlaaa 2:9497b91d341a 367 evk.baud(115200);
ohlaaa 2:9497b91d341a 368 at.setTimeout(6000);
ohlaaa 2:9497b91d341a 369
ohlaaa 2:9497b91d341a 370
ohlaaa 2:9497b91d341a 371 //evk.attach(&evk_callback);
ohlaaa 2:9497b91d341a 372 pc.attach(&PC_callback);
ohlaaa 2:9497b91d341a 373 modbus.attach(&MODBUS_callback);
ohlaaa 2:9497b91d341a 374
ohlaaa 2:9497b91d341a 375 server_buff[2]=';';
ohlaaa 2:9497b91d341a 376 server_buff[4]=';';
ohlaaa 2:9497b91d341a 377 server_buff[11]=';';
ohlaaa 2:9497b91d341a 378 server_buff[13]=';';
ohlaaa 2:9497b91d341a 379 server_buff[20]=';';
ohlaaa 2:9497b91d341a 380 server_buff[22]=';';
ohlaaa 2:9497b91d341a 381
ohlaaa 2:9497b91d341a 382
ohlaaa 2:9497b91d341a 383 //=========TEST AT=============
ohlaaa 2:9497b91d341a 384 if(at.send("AT")&& at.recv("AT")&&at.recv("OK")){
ohlaaa 2:9497b91d341a 385 pc.printf("AT OK\n");
ohlaaa 2:9497b91d341a 386 // evk.printf("AT OK");
ohlaaa 2:9497b91d341a 387 }
ohlaaa 2:9497b91d341a 388 else pc.printf("AT NO OK\n");
ohlaaa 2:9497b91d341a 389 wait_ms(3000);
ohlaaa 2:9497b91d341a 390 //========SET MQTT IP============
ohlaaa 2:9497b91d341a 391 if(at.send("AT+UMQTT=3,\"140.114.78.141\"") && at.recv("AT+UMQTT=3,\"140.114.78.141\"") && at.recv("+UMQTT: 3,1") && at.recv("OK")) pc.printf("Set IP OK\n");
ohlaaa 2:9497b91d341a 392 else pc.printf("IP NO OK\n");
ohlaaa 2:9497b91d341a 393 //========CONNECT TO MQTT SERVER=======
ohlaaa 2:9497b91d341a 394 if(at.send("AT+UMQTTC=1") && at.recv("AT+UMQTTC=1") && at.recv("+UMQTTC: 1,1") && at.recv("OK") && at.recv("+UUMQTTC: 1,0"))pc.printf("Connected to MQTT Server\n");
ohlaaa 2:9497b91d341a 395 else pc.printf("Connect to Server failed !\n");
ohlaaa 2:9497b91d341a 396 //========SEND MESSAGE =============
ohlaaa 2:9497b91d341a 397 if(at.send("AT+UMQTTC=2,0,0,\"nbiot/gw\",\"HELLO Send From F466RE,466924700006098\"")
ohlaaa 2:9497b91d341a 398 && at.recv("AT+UMQTTC=2,0,0,\"nbiot/gw\",\"HELLO Send From F466RE,466924700006098\"")
ohlaaa 2:9497b91d341a 399 && at.recv("+UMQTTC: 2,1")
ohlaaa 2:9497b91d341a 400 && at.recv("OK"))pc.printf("Send Hello to MQTT Server\r\n");
ohlaaa 2:9497b91d341a 401 else pc.printf("Send Message to Server Failed !\n");
ohlaaa 2:9497b91d341a 402
ohlaaa 2:9497b91d341a 403 while(1) {
ohlaaa 2:9497b91d341a 404 for(int i=1;i<10;i++){
ohlaaa 2:9497b91d341a 405 mdb_control(i);
ohlaaa 2:9497b91d341a 406 if(i==1){
ohlaaa 2:9497b91d341a 407 char buff[2];
ohlaaa 2:9497b91d341a 408
ohlaaa 2:9497b91d341a 409 sprintf((char*)buff,"%02x",mdb_Data[1].Data[0]);
ohlaaa 2:9497b91d341a 410 pc.printf("%02x\r\n",mdb_Data[1].Data[0]);
ohlaaa 2:9497b91d341a 411 server_buff[0]=buff[0];
ohlaaa 2:9497b91d341a 412 server_buff[1]=buff[1];
ohlaaa 2:9497b91d341a 413 }
ohlaaa 2:9497b91d341a 414
ohlaaa 2:9497b91d341a 415 if(i==2){
ohlaaa 2:9497b91d341a 416 if(mdb_Data[1].Data[0]==0x02){
ohlaaa 2:9497b91d341a 417 server_buff[3]='0';
ohlaaa 2:9497b91d341a 418 pc.printf("Y3 OFF\r\n");
ohlaaa 2:9497b91d341a 419 }
ohlaaa 2:9497b91d341a 420 else {
ohlaaa 2:9497b91d341a 421 server_buff[3]='1';
ohlaaa 2:9497b91d341a 422 pc.printf("Y3 ON\r\n");
ohlaaa 2:9497b91d341a 423 }
ohlaaa 2:9497b91d341a 424 }
ohlaaa 2:9497b91d341a 425
ohlaaa 2:9497b91d341a 426 else if(i==3){
ohlaaa 2:9497b91d341a 427 uint16_t data = ((((uint16_t)(mdb_Data[1].Data[0]))<<8) | (uint16_t)(mdb_Data[1].Data[1]));
ohlaaa 2:9497b91d341a 428 pc.printf("counter = %04X(hex) , %d(int)\r\n\n",data,data);
ohlaaa 2:9497b91d341a 429
ohlaaa 2:9497b91d341a 430 char buff[6];
ohlaaa 2:9497b91d341a 431
ohlaaa 2:9497b91d341a 432 sprintf((char*)buff,"%6d",data);
ohlaaa 2:9497b91d341a 433 server_buff[5]=buff[0];
ohlaaa 2:9497b91d341a 434 server_buff[6]=buff[1];
ohlaaa 2:9497b91d341a 435 server_buff[7]=buff[2];
ohlaaa 2:9497b91d341a 436 server_buff[8]=buff[3];
ohlaaa 2:9497b91d341a 437 server_buff[9]=buff[4];
ohlaaa 2:9497b91d341a 438 server_buff[10]=buff[5];
ohlaaa 2:9497b91d341a 439 }
ohlaaa 2:9497b91d341a 440 else if(i==5){
ohlaaa 2:9497b91d341a 441
ohlaaa 2:9497b91d341a 442 if(mdb_Data[1].Data[0]==0x00){
ohlaaa 2:9497b91d341a 443 server_buff[12]='0';
ohlaaa 2:9497b91d341a 444 pc.printf("D1_Y0 OFF\r\n");
ohlaaa 2:9497b91d341a 445 }
ohlaaa 2:9497b91d341a 446 else {
ohlaaa 2:9497b91d341a 447 server_buff[12]='1';
ohlaaa 2:9497b91d341a 448 pc.printf("D1_Y0 ON\r\n");
ohlaaa 2:9497b91d341a 449 }
ohlaaa 2:9497b91d341a 450 }
ohlaaa 2:9497b91d341a 451
ohlaaa 2:9497b91d341a 452 else if(i==6){
ohlaaa 2:9497b91d341a 453 int voltage = (((uint32_t)(mdb_Data[2].Data[0]))<< 8) | (((uint32_t)(mdb_Data[2].Data[1]))) | (((uint32_t)(mdb_Data[2].Data[2]))<< 24) | (((uint32_t)(mdb_Data[2].Data[3]))<< 16);
ohlaaa 2:9497b91d341a 454 float *pf=(float*)&voltage;
ohlaaa 2:9497b91d341a 455 //printf("%f",*pf);
ohlaaa 2:9497b91d341a 456 pc.printf("voltage = %08x(hex) %.2f(float)\r\n",voltage,*pf);
ohlaaa 2:9497b91d341a 457 int a = *pf*100;
ohlaaa 2:9497b91d341a 458 pc.printf("a = %d\r\n",a);
ohlaaa 2:9497b91d341a 459 char buff[5];
ohlaaa 2:9497b91d341a 460 sprintf(buff,"%5d",a);
ohlaaa 2:9497b91d341a 461 pc.printf("buff = %02x%02x%02x%02x%02x\r\n",buff[0],buff[1],buff[2],buff[3],buff[4]);
ohlaaa 2:9497b91d341a 462 server_buff[14]=buff[0];
ohlaaa 2:9497b91d341a 463 server_buff[15]=buff[1];
ohlaaa 2:9497b91d341a 464 server_buff[16]=buff[2];
ohlaaa 2:9497b91d341a 465 server_buff[17]='.';
ohlaaa 2:9497b91d341a 466 server_buff[18]=buff[3];
ohlaaa 2:9497b91d341a 467 server_buff[19]=buff[4];
ohlaaa 2:9497b91d341a 468 }
ohlaaa 2:9497b91d341a 469 if(i==8){
ohlaaa 2:9497b91d341a 470
ohlaaa 2:9497b91d341a 471 if(mdb_Data[3].Data[0]==0x00){
ohlaaa 2:9497b91d341a 472 server_buff[21]='0';
ohlaaa 2:9497b91d341a 473 pc.printf("D3 Y0 OFF\r\n");
ohlaaa 2:9497b91d341a 474 }
ohlaaa 2:9497b91d341a 475 else {
ohlaaa 2:9497b91d341a 476 server_buff[21]='1';
ohlaaa 2:9497b91d341a 477 pc.printf("D3 Y0 ON\r\n");
ohlaaa 2:9497b91d341a 478 }
ohlaaa 2:9497b91d341a 479
ohlaaa 2:9497b91d341a 480 }
ohlaaa 2:9497b91d341a 481
ohlaaa 2:9497b91d341a 482 else if(i==9){
ohlaaa 2:9497b91d341a 483 uint16_t temperature = ((((uint16_t)(mdb_Data[5].Data[0]))<<8) | (uint16_t)(mdb_Data[5].Data[1]));
ohlaaa 2:9497b91d341a 484 pc.printf("temperature = %04x(hex),%d(int)\r\n",temperature,temperature);
ohlaaa 2:9497b91d341a 485
ohlaaa 2:9497b91d341a 486 char buff[3];
ohlaaa 2:9497b91d341a 487
ohlaaa 2:9497b91d341a 488 sprintf(buff,"%3d",temperature);
ohlaaa 2:9497b91d341a 489 server_buff[23]=buff[0];
ohlaaa 2:9497b91d341a 490 server_buff[24]=buff[1];
ohlaaa 2:9497b91d341a 491 server_buff[25]='.';
ohlaaa 2:9497b91d341a 492 server_buff[26]=buff[2];
ohlaaa 2:9497b91d341a 493 AT_publish(server_buff);
ohlaaa 2:9497b91d341a 494 }
ohlaaa 2:9497b91d341a 495
ohlaaa 2:9497b91d341a 496
ohlaaa 2:9497b91d341a 497 pc.printf("============\r\n");
ohlaaa 2:9497b91d341a 498 wait_ms(50);
ohlaaa 2:9497b91d341a 499
ohlaaa 2:9497b91d341a 500 }
ohlaaa 2:9497b91d341a 501
ohlaaa 2:9497b91d341a 502
ohlaaa 2:9497b91d341a 503 }
nobelkuo 0:6fd21c22a0c9 504 }