share

Dependencies:   mbed ATParser2

Committer:
nobelkuo
Date:
Tue Jul 16 14:42:16 2019 +0000
Revision:
0:6fd21c22a0c9
Child:
1:17ea42de4a8a
Version_20190519

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nobelkuo 0:6fd21c22a0c9 1 #include "mbed.h"
nobelkuo 0:6fd21c22a0c9 2
nobelkuo 0:6fd21c22a0c9 3 Serial pc(USBTX, USBRX);
nobelkuo 0:6fd21c22a0c9 4 Serial modbus(PB_9, PB_8,9600);//(tx,rx,baud)
nobelkuo 0:6fd21c22a0c9 5 DigitalOut DE_RE(PB_0);
nobelkuo 0:6fd21c22a0c9 6 #define Read_Reg 0x03
nobelkuo 0:6fd21c22a0c9 7
nobelkuo 0:6fd21c22a0c9 8 int Max_Byte=8;
nobelkuo 0:6fd21c22a0c9 9 uint8_t mdb_Command_buffer[8];
nobelkuo 0:6fd21c22a0c9 10 uint8_t mdb_Message_buffer[8];
nobelkuo 0:6fd21c22a0c9 11 uint16_t RTU_CRC;
nobelkuo 0:6fd21c22a0c9 12 int message_in_byte=0;
nobelkuo 0:6fd21c22a0c9 13 struct mdb_Data_Set {
nobelkuo 0:6fd21c22a0c9 14 uint8_t Addr;
nobelkuo 0:6fd21c22a0c9 15 uint8_t Data[8];
nobelkuo 0:6fd21c22a0c9 16 };
nobelkuo 0:6fd21c22a0c9 17 mdb_Data_Set mdb_Data[8];
nobelkuo 0:6fd21c22a0c9 18
nobelkuo 0:6fd21c22a0c9 19 //put PC input into modbus
nobelkuo 0:6fd21c22a0c9 20 void PC_callback() {
nobelkuo 0:6fd21c22a0c9 21 //modbus.putc(pc.getc());
nobelkuo 0:6fd21c22a0c9 22 char incomming_CMD;
nobelkuo 0:6fd21c22a0c9 23 while (pc.readable()){
nobelkuo 0:6fd21c22a0c9 24 incomming_CMD+=pc.getc();
nobelkuo 0:6fd21c22a0c9 25 }
nobelkuo 0:6fd21c22a0c9 26 pc.printf("Received Command :%c\r\n", incomming_CMD);
nobelkuo 0:6fd21c22a0c9 27 }
nobelkuo 0:6fd21c22a0c9 28
nobelkuo 0:6fd21c22a0c9 29 //put modbus reply into PC
nobelkuo 0:6fd21c22a0c9 30 void MODBUS_callback() {
nobelkuo 0:6fd21c22a0c9 31 //pc.putc(modbus.getc());
nobelkuo 0:6fd21c22a0c9 32 mdb_Message_buffer[message_in_byte]=modbus.getc();
nobelkuo 0:6fd21c22a0c9 33 message_in_byte++;
nobelkuo 0:6fd21c22a0c9 34
nobelkuo 0:6fd21c22a0c9 35 }
nobelkuo 0:6fd21c22a0c9 36
nobelkuo 0:6fd21c22a0c9 37 //MODBUS RTU CRC
nobelkuo 0:6fd21c22a0c9 38 uint16_t mdbRTU_CRC(uint8_t message[], int Length)
nobelkuo 0:6fd21c22a0c9 39 {
nobelkuo 0:6fd21c22a0c9 40 RTU_CRC=0xffff;
nobelkuo 0:6fd21c22a0c9 41 for (int pos =0 ; pos < Length ; pos++)
nobelkuo 0:6fd21c22a0c9 42 {
nobelkuo 0:6fd21c22a0c9 43 RTU_CRC ^= (uint16_t)message[pos];
nobelkuo 0:6fd21c22a0c9 44
nobelkuo 0:6fd21c22a0c9 45 for (int i = 8; i != 0; i--)
nobelkuo 0:6fd21c22a0c9 46 {
nobelkuo 0:6fd21c22a0c9 47 if ((RTU_CRC & 0x0001) != 0)
nobelkuo 0:6fd21c22a0c9 48 {
nobelkuo 0:6fd21c22a0c9 49 RTU_CRC >>= 1;
nobelkuo 0:6fd21c22a0c9 50 RTU_CRC ^= 0xA001;
nobelkuo 0:6fd21c22a0c9 51 }
nobelkuo 0:6fd21c22a0c9 52 else
nobelkuo 0:6fd21c22a0c9 53 RTU_CRC >>= 1;
nobelkuo 0:6fd21c22a0c9 54 }
nobelkuo 0:6fd21c22a0c9 55 }
nobelkuo 0:6fd21c22a0c9 56 return RTU_CRC;
nobelkuo 0:6fd21c22a0c9 57 //pc.printf("hi_mdb_CRC= %02X \r\n", hi_RTU_CRC);
nobelkuo 0:6fd21c22a0c9 58 //pc.printf("lo_mdb_CRC= %02X \r\n", lo_RTU_CRC);
nobelkuo 0:6fd21c22a0c9 59 }
nobelkuo 0:6fd21c22a0c9 60
nobelkuo 0:6fd21c22a0c9 61 int main() {
nobelkuo 0:6fd21c22a0c9 62
nobelkuo 0:6fd21c22a0c9 63 pc.attach(&PC_callback);
nobelkuo 0:6fd21c22a0c9 64 modbus.attach(&MODBUS_callback);
nobelkuo 0:6fd21c22a0c9 65 uint8_t dev_Addr;
nobelkuo 0:6fd21c22a0c9 66 uint16_t Hold_Reg=1;
nobelkuo 0:6fd21c22a0c9 67 uint16_t Reg_len=2;
nobelkuo 0:6fd21c22a0c9 68
nobelkuo 0:6fd21c22a0c9 69 while(1) {
nobelkuo 0:6fd21c22a0c9 70 for (dev_Addr=1; dev_Addr<6; dev_Addr++){
nobelkuo 0:6fd21c22a0c9 71 mdb_Command_buffer[0]=(uint8_t)dev_Addr;
nobelkuo 0:6fd21c22a0c9 72 mdb_Command_buffer[1]=Read_Reg;
nobelkuo 0:6fd21c22a0c9 73 mdb_Command_buffer[2]=(uint8_t)((Hold_Reg & 0xFF00) >> 8);
nobelkuo 0:6fd21c22a0c9 74 mdb_Command_buffer[3]=(uint8_t)(Hold_Reg & 0x00FF);
nobelkuo 0:6fd21c22a0c9 75 mdb_Command_buffer[4]=(uint8_t)((Reg_len & 0xFF00) >> 8);
nobelkuo 0:6fd21c22a0c9 76 mdb_Command_buffer[5]=(uint8_t)(Reg_len & 0x00FF);
nobelkuo 0:6fd21c22a0c9 77 //mdbRTU_CRC();
nobelkuo 0:6fd21c22a0c9 78 RTU_CRC=mdbRTU_CRC(mdb_Command_buffer,6);
nobelkuo 0:6fd21c22a0c9 79 //pc.printf("RTU_CRC=%04x\r\n",RTU_CRC);
nobelkuo 0:6fd21c22a0c9 80 uint8_t hi_RTU_CRC = (uint8_t)((RTU_CRC & 0xFF00) >> 8);
nobelkuo 0:6fd21c22a0c9 81 uint8_t lo_RTU_CRC = (uint8_t)(RTU_CRC & 0x00FF);
nobelkuo 0:6fd21c22a0c9 82 mdb_Command_buffer[6]=lo_RTU_CRC;
nobelkuo 0:6fd21c22a0c9 83 mdb_Command_buffer[7]=hi_RTU_CRC;
nobelkuo 0:6fd21c22a0c9 84
nobelkuo 0:6fd21c22a0c9 85 DE_RE=1;
nobelkuo 0:6fd21c22a0c9 86 int byte_of_buffer;
nobelkuo 0:6fd21c22a0c9 87 for (byte_of_buffer=0 ; byte_of_buffer<Max_Byte ; byte_of_buffer++)
nobelkuo 0:6fd21c22a0c9 88 {
nobelkuo 0:6fd21c22a0c9 89 modbus.putc(mdb_Command_buffer[byte_of_buffer]);
nobelkuo 0:6fd21c22a0c9 90 }
nobelkuo 0:6fd21c22a0c9 91
nobelkuo 0:6fd21c22a0c9 92 wait_ms(2);
nobelkuo 0:6fd21c22a0c9 93 message_in_byte=0;
nobelkuo 0:6fd21c22a0c9 94 DE_RE =0 ;
nobelkuo 0:6fd21c22a0c9 95 wait(1);
nobelkuo 0:6fd21c22a0c9 96 mdb_Data[(dev_Addr)].Addr=dev_Addr;
nobelkuo 0:6fd21c22a0c9 97 pc.printf("device_ID=%d\r\n", dev_Addr);
nobelkuo 0:6fd21c22a0c9 98 int data_len=(int)(mdb_Message_buffer[2]);
nobelkuo 0:6fd21c22a0c9 99 RTU_CRC=mdbRTU_CRC(mdb_Message_buffer,data_len+3);
nobelkuo 0:6fd21c22a0c9 100 //pc.printf("RTU_CRC=%04x\r\n",RTU_CRC);
nobelkuo 0:6fd21c22a0c9 101 hi_RTU_CRC = (uint8_t)((RTU_CRC & 0xFF00) >> 8);
nobelkuo 0:6fd21c22a0c9 102 lo_RTU_CRC = (uint8_t)(RTU_CRC & 0x00FF);
nobelkuo 0:6fd21c22a0c9 103 if ((mdb_Message_buffer[data_len+3]==lo_RTU_CRC)&&(mdb_Message_buffer[data_len+4]==hi_RTU_CRC))
nobelkuo 0:6fd21c22a0c9 104 {
nobelkuo 0:6fd21c22a0c9 105 for (int i=0 ; i<data_len ; i++)
nobelkuo 0:6fd21c22a0c9 106 {
nobelkuo 0:6fd21c22a0c9 107 mdb_Data[(dev_Addr)].Data[i]=mdb_Message_buffer[i+3];
nobelkuo 0:6fd21c22a0c9 108 //pc.putc(mdb_Message_buffer[i]);
nobelkuo 0:6fd21c22a0c9 109 pc.printf("mdb_Data= %02x\r\n",mdb_Data[(dev_Addr)].Data[i]);
nobelkuo 0:6fd21c22a0c9 110
nobelkuo 0:6fd21c22a0c9 111 }
nobelkuo 0:6fd21c22a0c9 112 }
nobelkuo 0:6fd21c22a0c9 113
nobelkuo 0:6fd21c22a0c9 114 }
nobelkuo 0:6fd21c22a0c9 115 }
nobelkuo 0:6fd21c22a0c9 116 }