CHE-WEI LIN
/
modbus_test
share
main.cpp@1:17ea42de4a8a, 2019-08-22 (annotated)
- Committer:
- ohlaaa
- Date:
- Thu Aug 22 07:17:07 2019 +0000
- Revision:
- 1:17ea42de4a8a
- Parent:
- 0:6fd21c22a0c9
- Child:
- 2:9497b91d341a
share;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nobelkuo | 0:6fd21c22a0c9 | 1 | #include "mbed.h" |
nobelkuo | 0:6fd21c22a0c9 | 2 | |
ohlaaa | 1:17ea42de4a8a | 3 | Serial pc(USBTX, USBRX); //((USBTX, USBRX)) |
ohlaaa | 1:17ea42de4a8a | 4 | Serial modbus(PA_9, PA_10,9600);//(tx,rx,baud) |
ohlaaa | 1:17ea42de4a8a | 5 | Serial evk(PC_10,PC_11,115200); //CN7 |
ohlaaa | 1:17ea42de4a8a | 6 | DigitalOut DE_RE(PA_8); |
nobelkuo | 0:6fd21c22a0c9 | 7 | #define Read_Reg 0x03 |
nobelkuo | 0:6fd21c22a0c9 | 8 | |
nobelkuo | 0:6fd21c22a0c9 | 9 | int Max_Byte=8; |
nobelkuo | 0:6fd21c22a0c9 | 10 | uint8_t mdb_Command_buffer[8]; |
nobelkuo | 0:6fd21c22a0c9 | 11 | uint8_t mdb_Message_buffer[8]; |
nobelkuo | 0:6fd21c22a0c9 | 12 | uint16_t RTU_CRC; |
nobelkuo | 0:6fd21c22a0c9 | 13 | int message_in_byte=0; |
nobelkuo | 0:6fd21c22a0c9 | 14 | struct mdb_Data_Set { |
nobelkuo | 0:6fd21c22a0c9 | 15 | uint8_t Addr; |
nobelkuo | 0:6fd21c22a0c9 | 16 | uint8_t Data[8]; |
nobelkuo | 0:6fd21c22a0c9 | 17 | }; |
nobelkuo | 0:6fd21c22a0c9 | 18 | mdb_Data_Set mdb_Data[8]; |
nobelkuo | 0:6fd21c22a0c9 | 19 | |
nobelkuo | 0:6fd21c22a0c9 | 20 | //put PC input into modbus |
nobelkuo | 0:6fd21c22a0c9 | 21 | void PC_callback() { |
nobelkuo | 0:6fd21c22a0c9 | 22 | //modbus.putc(pc.getc()); |
ohlaaa | 1:17ea42de4a8a | 23 | // char incomming_CMD; |
ohlaaa | 1:17ea42de4a8a | 24 | /* while (pc.readable()){ |
ohlaaa | 1:17ea42de4a8a | 25 | incomming_CMD+=pc.getc()-1; |
nobelkuo | 0:6fd21c22a0c9 | 26 | } |
ohlaaa | 1:17ea42de4a8a | 27 | pc.printf("Received Command :%c\r\n", incomming_CMD);*/ |
ohlaaa | 1:17ea42de4a8a | 28 | |
ohlaaa | 1:17ea42de4a8a | 29 | pc.putc(pc.getc()); |
nobelkuo | 0:6fd21c22a0c9 | 30 | } |
nobelkuo | 0:6fd21c22a0c9 | 31 | |
ohlaaa | 1:17ea42de4a8a | 32 | void evk_callback() { |
ohlaaa | 1:17ea42de4a8a | 33 | pc.putc('a'); |
ohlaaa | 1:17ea42de4a8a | 34 | pc.putc(evk.getc()); |
ohlaaa | 1:17ea42de4a8a | 35 | } |
ohlaaa | 1:17ea42de4a8a | 36 | |
ohlaaa | 1:17ea42de4a8a | 37 | |
nobelkuo | 0:6fd21c22a0c9 | 38 | //put modbus reply into PC |
nobelkuo | 0:6fd21c22a0c9 | 39 | void MODBUS_callback() { |
nobelkuo | 0:6fd21c22a0c9 | 40 | //pc.putc(modbus.getc()); |
nobelkuo | 0:6fd21c22a0c9 | 41 | mdb_Message_buffer[message_in_byte]=modbus.getc(); |
nobelkuo | 0:6fd21c22a0c9 | 42 | message_in_byte++; |
nobelkuo | 0:6fd21c22a0c9 | 43 | |
nobelkuo | 0:6fd21c22a0c9 | 44 | } |
nobelkuo | 0:6fd21c22a0c9 | 45 | |
ohlaaa | 1:17ea42de4a8a | 46 | |
nobelkuo | 0:6fd21c22a0c9 | 47 | //MODBUS RTU CRC |
nobelkuo | 0:6fd21c22a0c9 | 48 | uint16_t mdbRTU_CRC(uint8_t message[], int Length) |
nobelkuo | 0:6fd21c22a0c9 | 49 | { |
nobelkuo | 0:6fd21c22a0c9 | 50 | RTU_CRC=0xffff; |
nobelkuo | 0:6fd21c22a0c9 | 51 | for (int pos =0 ; pos < Length ; pos++) |
nobelkuo | 0:6fd21c22a0c9 | 52 | { |
nobelkuo | 0:6fd21c22a0c9 | 53 | RTU_CRC ^= (uint16_t)message[pos]; |
nobelkuo | 0:6fd21c22a0c9 | 54 | |
nobelkuo | 0:6fd21c22a0c9 | 55 | for (int i = 8; i != 0; i--) |
nobelkuo | 0:6fd21c22a0c9 | 56 | { |
nobelkuo | 0:6fd21c22a0c9 | 57 | if ((RTU_CRC & 0x0001) != 0) |
nobelkuo | 0:6fd21c22a0c9 | 58 | { |
nobelkuo | 0:6fd21c22a0c9 | 59 | RTU_CRC >>= 1; |
nobelkuo | 0:6fd21c22a0c9 | 60 | RTU_CRC ^= 0xA001; |
nobelkuo | 0:6fd21c22a0c9 | 61 | } |
nobelkuo | 0:6fd21c22a0c9 | 62 | else |
nobelkuo | 0:6fd21c22a0c9 | 63 | RTU_CRC >>= 1; |
nobelkuo | 0:6fd21c22a0c9 | 64 | } |
nobelkuo | 0:6fd21c22a0c9 | 65 | } |
nobelkuo | 0:6fd21c22a0c9 | 66 | return RTU_CRC; |
nobelkuo | 0:6fd21c22a0c9 | 67 | //pc.printf("hi_mdb_CRC= %02X \r\n", hi_RTU_CRC); |
nobelkuo | 0:6fd21c22a0c9 | 68 | //pc.printf("lo_mdb_CRC= %02X \r\n", lo_RTU_CRC); |
nobelkuo | 0:6fd21c22a0c9 | 69 | } |
nobelkuo | 0:6fd21c22a0c9 | 70 | |
nobelkuo | 0:6fd21c22a0c9 | 71 | int main() { |
ohlaaa | 1:17ea42de4a8a | 72 | pc.baud(115200); |
ohlaaa | 1:17ea42de4a8a | 73 | evk.attach(&evk_callback); |
nobelkuo | 0:6fd21c22a0c9 | 74 | pc.attach(&PC_callback); |
nobelkuo | 0:6fd21c22a0c9 | 75 | modbus.attach(&MODBUS_callback); |
nobelkuo | 0:6fd21c22a0c9 | 76 | uint8_t dev_Addr; |
nobelkuo | 0:6fd21c22a0c9 | 77 | uint16_t Hold_Reg=1; |
nobelkuo | 0:6fd21c22a0c9 | 78 | uint16_t Reg_len=2; |
nobelkuo | 0:6fd21c22a0c9 | 79 | |
nobelkuo | 0:6fd21c22a0c9 | 80 | while(1) { |
nobelkuo | 0:6fd21c22a0c9 | 81 | for (dev_Addr=1; dev_Addr<6; dev_Addr++){ |
nobelkuo | 0:6fd21c22a0c9 | 82 | mdb_Command_buffer[0]=(uint8_t)dev_Addr; |
nobelkuo | 0:6fd21c22a0c9 | 83 | mdb_Command_buffer[1]=Read_Reg; |
nobelkuo | 0:6fd21c22a0c9 | 84 | mdb_Command_buffer[2]=(uint8_t)((Hold_Reg & 0xFF00) >> 8); |
nobelkuo | 0:6fd21c22a0c9 | 85 | mdb_Command_buffer[3]=(uint8_t)(Hold_Reg & 0x00FF); |
nobelkuo | 0:6fd21c22a0c9 | 86 | mdb_Command_buffer[4]=(uint8_t)((Reg_len & 0xFF00) >> 8); |
nobelkuo | 0:6fd21c22a0c9 | 87 | mdb_Command_buffer[5]=(uint8_t)(Reg_len & 0x00FF); |
nobelkuo | 0:6fd21c22a0c9 | 88 | //mdbRTU_CRC(); |
nobelkuo | 0:6fd21c22a0c9 | 89 | RTU_CRC=mdbRTU_CRC(mdb_Command_buffer,6); |
nobelkuo | 0:6fd21c22a0c9 | 90 | //pc.printf("RTU_CRC=%04x\r\n",RTU_CRC); |
nobelkuo | 0:6fd21c22a0c9 | 91 | uint8_t hi_RTU_CRC = (uint8_t)((RTU_CRC & 0xFF00) >> 8); |
nobelkuo | 0:6fd21c22a0c9 | 92 | uint8_t lo_RTU_CRC = (uint8_t)(RTU_CRC & 0x00FF); |
nobelkuo | 0:6fd21c22a0c9 | 93 | mdb_Command_buffer[6]=lo_RTU_CRC; |
nobelkuo | 0:6fd21c22a0c9 | 94 | mdb_Command_buffer[7]=hi_RTU_CRC; |
nobelkuo | 0:6fd21c22a0c9 | 95 | |
nobelkuo | 0:6fd21c22a0c9 | 96 | DE_RE=1; |
nobelkuo | 0:6fd21c22a0c9 | 97 | int byte_of_buffer; |
nobelkuo | 0:6fd21c22a0c9 | 98 | for (byte_of_buffer=0 ; byte_of_buffer<Max_Byte ; byte_of_buffer++) |
nobelkuo | 0:6fd21c22a0c9 | 99 | { |
nobelkuo | 0:6fd21c22a0c9 | 100 | modbus.putc(mdb_Command_buffer[byte_of_buffer]); |
nobelkuo | 0:6fd21c22a0c9 | 101 | } |
nobelkuo | 0:6fd21c22a0c9 | 102 | |
nobelkuo | 0:6fd21c22a0c9 | 103 | wait_ms(2); |
nobelkuo | 0:6fd21c22a0c9 | 104 | message_in_byte=0; |
nobelkuo | 0:6fd21c22a0c9 | 105 | DE_RE =0 ; |
nobelkuo | 0:6fd21c22a0c9 | 106 | wait(1); |
nobelkuo | 0:6fd21c22a0c9 | 107 | mdb_Data[(dev_Addr)].Addr=dev_Addr; |
nobelkuo | 0:6fd21c22a0c9 | 108 | pc.printf("device_ID=%d\r\n", dev_Addr); |
nobelkuo | 0:6fd21c22a0c9 | 109 | int data_len=(int)(mdb_Message_buffer[2]); |
nobelkuo | 0:6fd21c22a0c9 | 110 | RTU_CRC=mdbRTU_CRC(mdb_Message_buffer,data_len+3); |
nobelkuo | 0:6fd21c22a0c9 | 111 | //pc.printf("RTU_CRC=%04x\r\n",RTU_CRC); |
nobelkuo | 0:6fd21c22a0c9 | 112 | hi_RTU_CRC = (uint8_t)((RTU_CRC & 0xFF00) >> 8); |
nobelkuo | 0:6fd21c22a0c9 | 113 | lo_RTU_CRC = (uint8_t)(RTU_CRC & 0x00FF); |
ohlaaa | 1:17ea42de4a8a | 114 | |
nobelkuo | 0:6fd21c22a0c9 | 115 | if ((mdb_Message_buffer[data_len+3]==lo_RTU_CRC)&&(mdb_Message_buffer[data_len+4]==hi_RTU_CRC)) |
nobelkuo | 0:6fd21c22a0c9 | 116 | { |
nobelkuo | 0:6fd21c22a0c9 | 117 | for (int i=0 ; i<data_len ; i++) |
nobelkuo | 0:6fd21c22a0c9 | 118 | { |
nobelkuo | 0:6fd21c22a0c9 | 119 | mdb_Data[(dev_Addr)].Data[i]=mdb_Message_buffer[i+3]; |
nobelkuo | 0:6fd21c22a0c9 | 120 | //pc.putc(mdb_Message_buffer[i]); |
nobelkuo | 0:6fd21c22a0c9 | 121 | pc.printf("mdb_Data= %02x\r\n",mdb_Data[(dev_Addr)].Data[i]); |
ohlaaa | 1:17ea42de4a8a | 122 | // evk.printf("mdb_Data= %02x\r\n",mdb_Data[(dev_Addr)].Data[i]); |
ohlaaa | 1:17ea42de4a8a | 123 | |
nobelkuo | 0:6fd21c22a0c9 | 124 | |
nobelkuo | 0:6fd21c22a0c9 | 125 | } |
nobelkuo | 0:6fd21c22a0c9 | 126 | } |
nobelkuo | 0:6fd21c22a0c9 | 127 | |
nobelkuo | 0:6fd21c22a0c9 | 128 | } |
nobelkuo | 0:6fd21c22a0c9 | 129 | } |
nobelkuo | 0:6fd21c22a0c9 | 130 | } |