First Working Master with Adaptronic simulator

Dependencies:   mbed

Committer:
tecnosys
Date:
Sun Jul 24 05:04:06 2011 +0000
Revision:
0:150538eb0bf3

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tecnosys 0:150538eb0bf3 1 #include "mbed.h"
tecnosys 0:150538eb0bf3 2 #define PROTOPLATFORM
tecnosys 0:150538eb0bf3 3 //#define MBEDPLATFORM
tecnosys 0:150538eb0bf3 4 #include "Protocol/modbus.h"
tecnosys 0:150538eb0bf3 5
tecnosys 0:150538eb0bf3 6 //LEDs
tecnosys 0:150538eb0bf3 7 #define LED_1 (LED1)
tecnosys 0:150538eb0bf3 8 #define LED_2 (LED1)
tecnosys 0:150538eb0bf3 9
tecnosys 0:150538eb0bf3 10 //Serial(1) rs232
tecnosys 0:150538eb0bf3 11 #define RS232_TX (p13)
tecnosys 0:150538eb0bf3 12 #define RS232_RX (p14)
tecnosys 0:150538eb0bf3 13
tecnosys 0:150538eb0bf3 14 //Serial(2) rs232/ttl/iphone
tecnosys 0:150538eb0bf3 15 #define TTL_TX (p28)
tecnosys 0:150538eb0bf3 16 #define TTL_RX (p27)
tecnosys 0:150538eb0bf3 17 #define TTL_GND (p26)
tecnosys 0:150538eb0bf3 18
tecnosys 0:150538eb0bf3 19 //Console
tecnosys 0:150538eb0bf3 20 #define CONSOLETX (USBTX)
tecnosys 0:150538eb0bf3 21 #define CONSOLERX (USBRX)
tecnosys 0:150538eb0bf3 22
tecnosys 0:150538eb0bf3 23
tecnosys 0:150538eb0bf3 24
tecnosys 0:150538eb0bf3 25
tecnosys 0:150538eb0bf3 26 //MODBUS STUFF
tecnosys 0:150538eb0bf3 27 #define LOOP 1
tecnosys 0:150538eb0bf3 28 #define SLAVE 0x01
tecnosys 0:150538eb0bf3 29 #define ADDRESS_START 4096
tecnosys 0:150538eb0bf3 30 #define ADDRESS_END 4100
tecnosys 0:150538eb0bf3 31
tecnosys 0:150538eb0bf3 32
tecnosys 0:150538eb0bf3 33 int nb = ADDRESS_END - ADDRESS_START;;
tecnosys 0:150538eb0bf3 34
tecnosys 0:150538eb0bf3 35 uint8_t tab_rq_status[ADDRESS_END - ADDRESS_START];
tecnosys 0:150538eb0bf3 36 uint8_t tab_rp_status[ADDRESS_END - ADDRESS_START];
tecnosys 0:150538eb0bf3 37 uint16_t tab_rq_registers[ADDRESS_END - ADDRESS_START];
tecnosys 0:150538eb0bf3 38 uint16_t tab_rp_registers[ADDRESS_END - ADDRESS_START];
tecnosys 0:150538eb0bf3 39
tecnosys 0:150538eb0bf3 40 DigitalOut myled(LED_1);
tecnosys 0:150538eb0bf3 41 Serial console_port(CONSOLETX, CONSOLERX);
tecnosys 0:150538eb0bf3 42
tecnosys 0:150538eb0bf3 43 Serial serial(RS232_TX,RS232_RX);
tecnosys 0:150538eb0bf3 44
tecnosys 0:150538eb0bf3 45 modbus_param_t mb_param;
tecnosys 0:150538eb0bf3 46 uint8_t query[MIN_QUERY_LENGTH];
tecnosys 0:150538eb0bf3 47 uint8_t response[MAX_MESSAGE_LENGTH];
tecnosys 0:150538eb0bf3 48
tecnosys 0:150538eb0bf3 49 void handleInterrupt() {
tecnosys 0:150538eb0bf3 50
tecnosys 0:150538eb0bf3 51 int ret;
tecnosys 0:150538eb0bf3 52 int nb_fail;
tecnosys 0:150538eb0bf3 53 int nb_loop;
tecnosys 0:150538eb0bf3 54 int addr;
tecnosys 0:150538eb0bf3 55
tecnosys 0:150538eb0bf3 56
tecnosys 0:150538eb0bf3 57
tecnosys 0:150538eb0bf3 58 printf("**");
tecnosys 0:150538eb0bf3 59
tecnosys 0:150538eb0bf3 60
tecnosys 0:150538eb0bf3 61 modbus_receive(&mb_param, query, response);
tecnosys 0:150538eb0bf3 62
tecnosys 0:150538eb0bf3 63 // if (ret > 0) {
tecnosys 0:150538eb0bf3 64 int offset;
tecnosys 0:150538eb0bf3 65 int i;
tecnosys 0:150538eb0bf3 66
tecnosys 0:150538eb0bf3 67 // ret = modbus_receive(mb_param, query, response);
tecnosys 0:150538eb0bf3 68
tecnosys 0:150538eb0bf3 69 offset = mb_param.header_length;
tecnosys 0:150538eb0bf3 70
tecnosys 0:150538eb0bf3 71 // If ret is negative, the loop is jumped !
tecnosys 0:150538eb0bf3 72 for (i = 0; i < ret; i++) {
tecnosys 0:150538eb0bf3 73 // shift reg hi_byte to temp OR with lo_byte
tecnosys 0:150538eb0bf3 74 tab_rq_registers[i] = (response[offset + 3 + (i << 1)] << 8) |
tecnosys 0:150538eb0bf3 75 response[offset + 4 + (i << 1)];
tecnosys 0:150538eb0bf3 76 printf("%d -- %d", tab_rq_registers[i],i);
tecnosys 0:150538eb0bf3 77 }
tecnosys 0:150538eb0bf3 78 // }
tecnosys 0:150538eb0bf3 79
tecnosys 0:150538eb0bf3 80 /*
tecnosys 0:150538eb0bf3 81 for (int i=0; i<nb; i++) {
tecnosys 0:150538eb0bf3 82 if (tab_rq_registers[i] != tab_rp_registers[i]) {
tecnosys 0:150538eb0bf3 83 printf("ERROR read_holding_registers\n");
tecnosys 0:150538eb0bf3 84 printf("Slave = %d, address = %d, value %d (0x%X) != %d (0x%X)\n",
tecnosys 0:150538eb0bf3 85 SLAVE, addr,
tecnosys 0:150538eb0bf3 86 tab_rq_registers[i], tab_rq_registers[i],
tecnosys 0:150538eb0bf3 87 tab_rp_registers[i], tab_rp_registers[i]);
tecnosys 0:150538eb0bf3 88 nb_fail++;
tecnosys 0:150538eb0bf3 89 }
tecnosys 0:150538eb0bf3 90 }
tecnosys 0:150538eb0bf3 91 */
tecnosys 0:150538eb0bf3 92
tecnosys 0:150538eb0bf3 93 }
tecnosys 0:150538eb0bf3 94
tecnosys 0:150538eb0bf3 95 void handleInterrupt2() {
tecnosys 0:150538eb0bf3 96
tecnosys 0:150538eb0bf3 97 while(serial.readable()) {
tecnosys 0:150538eb0bf3 98 printf("<%.2X>", serial.getc());
tecnosys 0:150538eb0bf3 99 }
tecnosys 0:150538eb0bf3 100
tecnosys 0:150538eb0bf3 101 }
tecnosys 0:150538eb0bf3 102
tecnosys 0:150538eb0bf3 103 void modbus_test() {
tecnosys 0:150538eb0bf3 104
tecnosys 0:150538eb0bf3 105
tecnosys 0:150538eb0bf3 106 int ret;
tecnosys 0:150538eb0bf3 107 int nb_fail;
tecnosys 0:150538eb0bf3 108 int nb_loop;
tecnosys 0:150538eb0bf3 109 int addr;
tecnosys 0:150538eb0bf3 110
tecnosys 0:150538eb0bf3 111 modbus_init_rtu(&mb_param, "/dev/ttyS0", 19200, "none", 8, 1);
tecnosys 0:150538eb0bf3 112
tecnosys 0:150538eb0bf3 113 //modbus_set_debug(&mb_param, TRUE);
tecnosys 0:150538eb0bf3 114 if (modbus_connect(&mb_param) == -1) {
tecnosys 0:150538eb0bf3 115 printf("ERROR Connection failed\n");
tecnosys 0:150538eb0bf3 116 exit(1);
tecnosys 0:150538eb0bf3 117 }
tecnosys 0:150538eb0bf3 118
tecnosys 0:150538eb0bf3 119 nb = ADDRESS_END - ADDRESS_START;
tecnosys 0:150538eb0bf3 120
tecnosys 0:150538eb0bf3 121 /*
tecnosys 0:150538eb0bf3 122 tab_rq_status = (uint8_t *) malloc(nb * sizeof(uint8_t));
tecnosys 0:150538eb0bf3 123 memset(tab_rq_status, 0, nb * sizeof(uint8_t));
tecnosys 0:150538eb0bf3 124
tecnosys 0:150538eb0bf3 125 tab_rp_status = (uint8_t *) malloc(nb * sizeof(uint8_t));
tecnosys 0:150538eb0bf3 126 memset(tab_rp_status, 0, nb * sizeof(uint8_t));
tecnosys 0:150538eb0bf3 127
tecnosys 0:150538eb0bf3 128 tab_rq_registers = (uint16_t *) malloc(nb * sizeof(uint16_t));
tecnosys 0:150538eb0bf3 129 memset(tab_rq_registers, 0, nb * sizeof(uint16_t));
tecnosys 0:150538eb0bf3 130
tecnosys 0:150538eb0bf3 131 tab_rp_registers = (uint16_t *) malloc(nb * sizeof(uint16_t));
tecnosys 0:150538eb0bf3 132 memset(tab_rp_registers, 0, nb * sizeof(uint16_t));
tecnosys 0:150538eb0bf3 133 */
tecnosys 0:150538eb0bf3 134 serial.attach( handleInterrupt2);
tecnosys 0:150538eb0bf3 135
tecnosys 0:150538eb0bf3 136 nb_loop = nb_fail = 0;
tecnosys 0:150538eb0bf3 137 while (nb_loop++ < LOOP) {
tecnosys 0:150538eb0bf3 138 for (addr = ADDRESS_START; addr <= ADDRESS_END; addr++) {
tecnosys 0:150538eb0bf3 139 int i;
tecnosys 0:150538eb0bf3 140
tecnosys 0:150538eb0bf3 141 for (i=0; i<nb; i++) {
tecnosys 0:150538eb0bf3 142 tab_rq_registers[i] = (uint16_t) 44097+i;;
tecnosys 0:150538eb0bf3 143 tab_rq_status[i] = tab_rq_registers[i] % 2;
tecnosys 0:150538eb0bf3 144 }
tecnosys 0:150538eb0bf3 145 nb = ADDRESS_END - addr;
tecnosys 0:150538eb0bf3 146 /*
tecnosys 0:150538eb0bf3 147 ret = preset_multiple_registers(&mb_param, SLAVE,
tecnosys 0:150538eb0bf3 148 addr, nb, tab_rq_registers);
tecnosys 0:150538eb0bf3 149 if (ret != nb) {
tecnosys 0:150538eb0bf3 150 printf("ERROR preset_multiple_registers (%d)\n", ret);
tecnosys 0:150538eb0bf3 151 printf("Slave = %d, address = %d, nb = %d\n",
tecnosys 0:150538eb0bf3 152 SLAVE, addr, nb);
tecnosys 0:150538eb0bf3 153 nb_fail++;
tecnosys 0:150538eb0bf3 154 } else {
tecnosys 0:150538eb0bf3 155 */
tecnosys 0:150538eb0bf3 156 /*
tecnosys 0:150538eb0bf3 157 ret = read_holding_registers(&mb_param, SLAVE,
tecnosys 0:150538eb0bf3 158 addr, nb, tab_rp_registers);
tecnosys 0:150538eb0bf3 159
tecnosys 0:150538eb0bf3 160
tecnosys 0:150538eb0bf3 161 printf("HERER");
tecnosys 0:150538eb0bf3 162 */
tecnosys 0:150538eb0bf3 163
tecnosys 0:150538eb0bf3 164 int ret;
tecnosys 0:150538eb0bf3 165 int query_length;
tecnosys 0:150538eb0bf3 166
tecnosys 0:150538eb0bf3 167
tecnosys 0:150538eb0bf3 168 query_length = build_query_basis_rtu(SLAVE, FC_READ_HOLDING_REGISTERS, addr, nb, query);
tecnosys 0:150538eb0bf3 169 // query_length = build_query_basis(&mb_param, SLAVE, FC_READ_HOLDING_REGISTERS, addr, nb, query);
tecnosys 0:150538eb0bf3 170
tecnosys 0:150538eb0bf3 171 ret = modbus_send(&mb_param, query, query_length);
tecnosys 0:150538eb0bf3 172
tecnosys 0:150538eb0bf3 173 //}
tecnosys 0:150538eb0bf3 174
tecnosys 0:150538eb0bf3 175 }
tecnosys 0:150538eb0bf3 176
tecnosys 0:150538eb0bf3 177 printf("Test: ");
tecnosys 0:150538eb0bf3 178 if (nb_fail)
tecnosys 0:150538eb0bf3 179 printf("%d FAILS\n", nb_fail);
tecnosys 0:150538eb0bf3 180 else
tecnosys 0:150538eb0bf3 181 printf("SUCCESS\n");
tecnosys 0:150538eb0bf3 182 }
tecnosys 0:150538eb0bf3 183 // handleInterrupt();
tecnosys 0:150538eb0bf3 184 /*
tecnosys 0:150538eb0bf3 185 free(tab_rq_status);
tecnosys 0:150538eb0bf3 186 free(tab_rp_status);
tecnosys 0:150538eb0bf3 187 free(tab_rq_registers);
tecnosys 0:150538eb0bf3 188 free(tab_rp_registers);
tecnosys 0:150538eb0bf3 189
tecnosys 0:150538eb0bf3 190 modbus_close(&mb_param);
tecnosys 0:150538eb0bf3 191 */
tecnosys 0:150538eb0bf3 192 }
tecnosys 0:150538eb0bf3 193
tecnosys 0:150538eb0bf3 194
tecnosys 0:150538eb0bf3 195 int main() {
tecnosys 0:150538eb0bf3 196 myled = 1;
tecnosys 0:150538eb0bf3 197 console_port.baud(230400);
tecnosys 0:150538eb0bf3 198 serial.baud(57600);
tecnosys 0:150538eb0bf3 199
tecnosys 0:150538eb0bf3 200 printf("Starting\n");
tecnosys 0:150538eb0bf3 201
tecnosys 0:150538eb0bf3 202 modbus_test();
tecnosys 0:150538eb0bf3 203
tecnosys 0:150538eb0bf3 204 while(1) {
tecnosys 0:150538eb0bf3 205 myled = 1;
tecnosys 0:150538eb0bf3 206 wait(0.2);
tecnosys 0:150538eb0bf3 207 myled = 0;
tecnosys 0:150538eb0bf3 208 wait(0.2);
tecnosys 0:150538eb0bf3 209 }
tecnosys 0:150538eb0bf3 210 }