老 姚
/
modbusServer
modbus slaver
main.cpp@0:998a68b8defc, 2019-09-17 (annotated)
- Committer:
- yao6116601
- Date:
- Tue Sep 17 01:59:40 2019 +0000
- Revision:
- 0:998a68b8defc
modbus server
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
yao6116601 | 0:998a68b8defc | 1 | #include "mbed.h" |
yao6116601 | 0:998a68b8defc | 2 | #include "modbus.h" |
yao6116601 | 0:998a68b8defc | 3 | DigitalOut led1(PC_6); |
yao6116601 | 0:998a68b8defc | 4 | |
yao6116601 | 0:998a68b8defc | 5 | DigitalIn DI0(PF_5); |
yao6116601 | 0:998a68b8defc | 6 | DigitalIn DI1(PF_3); |
yao6116601 | 0:998a68b8defc | 7 | DigitalIn DI2(PF_7); |
yao6116601 | 0:998a68b8defc | 8 | DigitalIn DI3(PF_6); |
yao6116601 | 0:998a68b8defc | 9 | DigitalIn DI4(PA_5); |
yao6116601 | 0:998a68b8defc | 10 | DigitalIn DI5(PB_8); |
yao6116601 | 0:998a68b8defc | 11 | DigitalIn DI6(PF_10); |
yao6116601 | 0:998a68b8defc | 12 | DigitalIn DI7(PF_4); |
yao6116601 | 0:998a68b8defc | 13 | DigitalIn DI8(PF_8); |
yao6116601 | 0:998a68b8defc | 14 | DigitalIn DI9(PF_9); |
yao6116601 | 0:998a68b8defc | 15 | |
yao6116601 | 0:998a68b8defc | 16 | DigitalOut DO0(PE_11); |
yao6116601 | 0:998a68b8defc | 17 | DigitalOut DO1(PE_7); |
yao6116601 | 0:998a68b8defc | 18 | DigitalOut DO2(PB_0); |
yao6116601 | 0:998a68b8defc | 19 | DigitalOut DO3(PA_4); |
yao6116601 | 0:998a68b8defc | 20 | DigitalOut DO4(PC_2); |
yao6116601 | 0:998a68b8defc | 21 | DigitalOut DO5(PF_2); |
yao6116601 | 0:998a68b8defc | 22 | DigitalOut DO6(PF_0); |
yao6116601 | 0:998a68b8defc | 23 | DigitalOut DO7(PF_1); |
yao6116601 | 0:998a68b8defc | 24 | DigitalOut DO8(PC_0); |
yao6116601 | 0:998a68b8defc | 25 | DigitalOut DO9(PC_3); |
yao6116601 | 0:998a68b8defc | 26 | // |
yao6116601 | 0:998a68b8defc | 27 | DigitalOut HF(PD_14); |
yao6116601 | 0:998a68b8defc | 28 | Modbus slave(PB_6,PB_7,PD_12,9600); |
yao6116601 | 0:998a68b8defc | 29 | uint8_t myAddress=0x01; |
yao6116601 | 0:998a68b8defc | 30 | uint16_t data[32]; |
yao6116601 | 0:998a68b8defc | 31 | uint16_t readDI(uint16_t address) |
yao6116601 | 0:998a68b8defc | 32 | { |
yao6116601 | 0:998a68b8defc | 33 | uint16_t terminal=address-100; |
yao6116601 | 0:998a68b8defc | 34 | switch(terminal) |
yao6116601 | 0:998a68b8defc | 35 | { |
yao6116601 | 0:998a68b8defc | 36 | case 0:{ |
yao6116601 | 0:998a68b8defc | 37 | if (DI0) return 0xffff; |
yao6116601 | 0:998a68b8defc | 38 | else return 0; |
yao6116601 | 0:998a68b8defc | 39 | } |
yao6116601 | 0:998a68b8defc | 40 | case 1:{ |
yao6116601 | 0:998a68b8defc | 41 | if (DI1) return 0xffff; |
yao6116601 | 0:998a68b8defc | 42 | else return 0; |
yao6116601 | 0:998a68b8defc | 43 | } |
yao6116601 | 0:998a68b8defc | 44 | case 2:{ |
yao6116601 | 0:998a68b8defc | 45 | if (DI2) return 0xffff; |
yao6116601 | 0:998a68b8defc | 46 | else return 0; |
yao6116601 | 0:998a68b8defc | 47 | } |
yao6116601 | 0:998a68b8defc | 48 | case 3:{ |
yao6116601 | 0:998a68b8defc | 49 | if (DI3) return 0xffff; |
yao6116601 | 0:998a68b8defc | 50 | else return 0; |
yao6116601 | 0:998a68b8defc | 51 | } |
yao6116601 | 0:998a68b8defc | 52 | case 4:{ |
yao6116601 | 0:998a68b8defc | 53 | if (DI4) return 0xffff; |
yao6116601 | 0:998a68b8defc | 54 | else return 0; |
yao6116601 | 0:998a68b8defc | 55 | } |
yao6116601 | 0:998a68b8defc | 56 | case 5:{ |
yao6116601 | 0:998a68b8defc | 57 | if (DI5) return 0xffff; |
yao6116601 | 0:998a68b8defc | 58 | else return 0; |
yao6116601 | 0:998a68b8defc | 59 | } |
yao6116601 | 0:998a68b8defc | 60 | case 6:{ |
yao6116601 | 0:998a68b8defc | 61 | if (DI6) return 0xffff; |
yao6116601 | 0:998a68b8defc | 62 | else return 0; |
yao6116601 | 0:998a68b8defc | 63 | } |
yao6116601 | 0:998a68b8defc | 64 | case 7:{ |
yao6116601 | 0:998a68b8defc | 65 | if (DI7) return 0xffff; |
yao6116601 | 0:998a68b8defc | 66 | else return 0; |
yao6116601 | 0:998a68b8defc | 67 | } |
yao6116601 | 0:998a68b8defc | 68 | case 8:{ |
yao6116601 | 0:998a68b8defc | 69 | if (DI8) return 0xffff; |
yao6116601 | 0:998a68b8defc | 70 | else return 0; |
yao6116601 | 0:998a68b8defc | 71 | } |
yao6116601 | 0:998a68b8defc | 72 | case 9:{ |
yao6116601 | 0:998a68b8defc | 73 | if (DI9) return 0xffff; |
yao6116601 | 0:998a68b8defc | 74 | else return 0; |
yao6116601 | 0:998a68b8defc | 75 | } |
yao6116601 | 0:998a68b8defc | 76 | } |
yao6116601 | 0:998a68b8defc | 77 | return 0; |
yao6116601 | 0:998a68b8defc | 78 | } |
yao6116601 | 0:998a68b8defc | 79 | uint16_t writeDO(uint16_t address,uint16_t value) |
yao6116601 | 0:998a68b8defc | 80 | { |
yao6116601 | 0:998a68b8defc | 81 | uint16_t terminal=address-100; |
yao6116601 | 0:998a68b8defc | 82 | switch(terminal) |
yao6116601 | 0:998a68b8defc | 83 | { |
yao6116601 | 0:998a68b8defc | 84 | case 0:{ |
yao6116601 | 0:998a68b8defc | 85 | DO0=value&0x0001; |
yao6116601 | 0:998a68b8defc | 86 | if (DO0) return 0xffff; |
yao6116601 | 0:998a68b8defc | 87 | else return 0; |
yao6116601 | 0:998a68b8defc | 88 | } |
yao6116601 | 0:998a68b8defc | 89 | case 1:{ |
yao6116601 | 0:998a68b8defc | 90 | DO1=value&0x0001; |
yao6116601 | 0:998a68b8defc | 91 | if (DO1) return 0xffff; |
yao6116601 | 0:998a68b8defc | 92 | else return 0; |
yao6116601 | 0:998a68b8defc | 93 | } |
yao6116601 | 0:998a68b8defc | 94 | case 2:{ |
yao6116601 | 0:998a68b8defc | 95 | DO2=value&0x0001; |
yao6116601 | 0:998a68b8defc | 96 | if (DO2) return 0xffff; |
yao6116601 | 0:998a68b8defc | 97 | else return 0; |
yao6116601 | 0:998a68b8defc | 98 | } |
yao6116601 | 0:998a68b8defc | 99 | case 3:{ |
yao6116601 | 0:998a68b8defc | 100 | DO3=value&0x0001; |
yao6116601 | 0:998a68b8defc | 101 | if (DO3) return 0xffff; |
yao6116601 | 0:998a68b8defc | 102 | else return 0; |
yao6116601 | 0:998a68b8defc | 103 | } |
yao6116601 | 0:998a68b8defc | 104 | case 4:{ |
yao6116601 | 0:998a68b8defc | 105 | DO4=value&0x0001; |
yao6116601 | 0:998a68b8defc | 106 | if (DO4) return 0xffff; |
yao6116601 | 0:998a68b8defc | 107 | else return 0; |
yao6116601 | 0:998a68b8defc | 108 | } |
yao6116601 | 0:998a68b8defc | 109 | case 5:{ |
yao6116601 | 0:998a68b8defc | 110 | DO5=value&0x0001; |
yao6116601 | 0:998a68b8defc | 111 | if (DO5) return 0xffff; |
yao6116601 | 0:998a68b8defc | 112 | else return 0; |
yao6116601 | 0:998a68b8defc | 113 | } |
yao6116601 | 0:998a68b8defc | 114 | case 6:{ |
yao6116601 | 0:998a68b8defc | 115 | DO6=value&0x0001; |
yao6116601 | 0:998a68b8defc | 116 | if (DO6) return 0xffff; |
yao6116601 | 0:998a68b8defc | 117 | else return 0; |
yao6116601 | 0:998a68b8defc | 118 | } |
yao6116601 | 0:998a68b8defc | 119 | case 7:{ |
yao6116601 | 0:998a68b8defc | 120 | DO7=value&0x0001; |
yao6116601 | 0:998a68b8defc | 121 | if (DO7) return 0xffff; |
yao6116601 | 0:998a68b8defc | 122 | else return 0; |
yao6116601 | 0:998a68b8defc | 123 | } |
yao6116601 | 0:998a68b8defc | 124 | case 8:{ |
yao6116601 | 0:998a68b8defc | 125 | DO8=value&0x0001; |
yao6116601 | 0:998a68b8defc | 126 | if (DO8) return 0xffff; |
yao6116601 | 0:998a68b8defc | 127 | else return 0; |
yao6116601 | 0:998a68b8defc | 128 | } |
yao6116601 | 0:998a68b8defc | 129 | case 9:{ |
yao6116601 | 0:998a68b8defc | 130 | DO9=value&0x0001; |
yao6116601 | 0:998a68b8defc | 131 | if (DO9) return 0xffff; |
yao6116601 | 0:998a68b8defc | 132 | else return 0; |
yao6116601 | 0:998a68b8defc | 133 | } |
yao6116601 | 0:998a68b8defc | 134 | } |
yao6116601 | 0:998a68b8defc | 135 | return 0; |
yao6116601 | 0:998a68b8defc | 136 | } |
yao6116601 | 0:998a68b8defc | 137 | void waitQuery() |
yao6116601 | 0:998a68b8defc | 138 | { uint8_t address; |
yao6116601 | 0:998a68b8defc | 139 | uint8_t function; |
yao6116601 | 0:998a68b8defc | 140 | uint16_t startAddress; |
yao6116601 | 0:998a68b8defc | 141 | uint16_t points; |
yao6116601 | 0:998a68b8defc | 142 | int i; |
yao6116601 | 0:998a68b8defc | 143 | if(slave.waitQuery(address,function,startAddress,points)) |
yao6116601 | 0:998a68b8defc | 144 | { |
yao6116601 | 0:998a68b8defc | 145 | if ((address==myAddress)&&(function==0x03)) |
yao6116601 | 0:998a68b8defc | 146 | { //read reigster |
yao6116601 | 0:998a68b8defc | 147 | uint16_t p=startAddress; |
yao6116601 | 0:998a68b8defc | 148 | for (i=0;i<points;i++) |
yao6116601 | 0:998a68b8defc | 149 | { |
yao6116601 | 0:998a68b8defc | 150 | data[i]= readDI(p++); |
yao6116601 | 0:998a68b8defc | 151 | } |
yao6116601 | 0:998a68b8defc | 152 | slave.sendResponse((uint8_t *)data,(uint8_t)(points*2)); |
yao6116601 | 0:998a68b8defc | 153 | } else |
yao6116601 | 0:998a68b8defc | 154 | if ((address==myAddress)&&(function==0x06)) |
yao6116601 | 0:998a68b8defc | 155 | { //write register |
yao6116601 | 0:998a68b8defc | 156 | |
yao6116601 | 0:998a68b8defc | 157 | data[0]= writeDO(startAddress,points); |
yao6116601 | 0:998a68b8defc | 158 | slave.sendResponse((uint8_t *)data,2); |
yao6116601 | 0:998a68b8defc | 159 | } |
yao6116601 | 0:998a68b8defc | 160 | } |
yao6116601 | 0:998a68b8defc | 161 | } |
yao6116601 | 0:998a68b8defc | 162 | int main() { |
yao6116601 | 0:998a68b8defc | 163 | int i; |
yao6116601 | 0:998a68b8defc | 164 | printf("modbus controller\n"); |
yao6116601 | 0:998a68b8defc | 165 | HF=1; |
yao6116601 | 0:998a68b8defc | 166 | while (true) { |
yao6116601 | 0:998a68b8defc | 167 | waitQuery(); |
yao6116601 | 0:998a68b8defc | 168 | led1 = !led1; |
yao6116601 | 0:998a68b8defc | 169 | |
yao6116601 | 0:998a68b8defc | 170 | |
yao6116601 | 0:998a68b8defc | 171 | } |
yao6116601 | 0:998a68b8defc | 172 | } |
yao6116601 | 0:998a68b8defc | 173 |