Depot Pertamina / Mbed 2 deprecated Gate_FRDM

Dependencies:   EthernetInterface MbedJSONValue SDFileSystem mbed-rtos mbed

Fork of Gate_Aidik_FRDM_v2 by Depot Pertamina

Committer:
irsanjul
Date:
Mon Aug 22 09:27:26 2016 +0000
Revision:
0:fbb75122015f
Child:
1:021273f6b9f4
Child:
2:e870bcd7e63b
testes

Who changed what in which revision?

UserRevisionLine numberNew contents of line
irsanjul 0:fbb75122015f 1 #include "mbed.h"
irsanjul 0:fbb75122015f 2 #include "EthernetInterface.h"
irsanjul 0:fbb75122015f 3 #include "SDFileSystem.h"
irsanjul 0:fbb75122015f 4 #include <string>
irsanjul 0:fbb75122015f 5
irsanjul 0:fbb75122015f 6 #define IP_CONF_PORT 50004
irsanjul 0:fbb75122015f 7
irsanjul 0:fbb75122015f 8 #define ijo 0
irsanjul 0:fbb75122015f 9 #define merah 1
irsanjul 0:fbb75122015f 10
irsanjul 0:fbb75122015f 11 UDPSocket sock, ip_conf, trx_ser; //gen_con,
irsanjul 0:fbb75122015f 12 Endpoint multi, server; //,multi_data;
irsanjul 0:fbb75122015f 13
irsanjul 0:fbb75122015f 14 SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd");
irsanjul 0:fbb75122015f 15
irsanjul 0:fbb75122015f 16 DigitalOut Stdby_R(LED2), Sopir_R(LED3), Res_R(LED1), MT_R(LED4), Gate(PTB9), Sleep(PTA1), Reset(PTB23, 1);
irsanjul 0:fbb75122015f 17 DigitalInOut Std_G(PTC5), Std_R(PTC7), Spr_G(PTC0), Spr_R(PTC9), Mt_G(PTC8), Mt_R(PTC1), Pow(PTB19), iBtn(PTB18);
irsanjul 0:fbb75122015f 18 DigitalIn print_er(PTC2), print_done(PTB2);
irsanjul 0:fbb75122015f 19
irsanjul 0:fbb75122015f 20 Serial dbg(USBTX, USBRX);
irsanjul 0:fbb75122015f 21 Serial pc(PTC17, PTC16);
irsanjul 0:fbb75122015f 22
irsanjul 0:fbb75122015f 23 char IP[16], SUBNET[16], GATEWAY[16], MCAST[16];
irsanjul 0:fbb75122015f 24 char PortListen[6], PortSend[6], PortHeartB[6];
irsanjul 0:fbb75122015f 25 char IDX[4];
irsanjul 0:fbb75122015f 26 char Baud[6];
irsanjul 0:fbb75122015f 27 int BAUD = 19200;
irsanjul 0:fbb75122015f 28
irsanjul 0:fbb75122015f 29 int width, TICK = 0;
irsanjul 0:fbb75122015f 30 unsigned char ID[8], idx, idy, id_addr = 0x33, ix, er_tmp1 = 0, er_tmp2 = 0,
irsanjul 0:fbb75122015f 31 sprmt = 0; // state = 1,
irsanjul 0:fbb75122015f 32 char text[26], ch, rx_buff[6], tx_buff[7], //86
irsanjul 0:fbb75122015f 33 hex[17] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B',
irsanjul 0:fbb75122015f 34 'C', 'D', 'E', 'F' }; //,
irsanjul 0:fbb75122015f 35
irsanjul 0:fbb75122015f 36 /*Contoh format data
irsanjul 0:fbb75122015f 37 <02>GI01S0123456789ABCDEF00<03>
irsanjul 0:fbb75122015f 38 <02>GI01M0123456789ABCDEF00<03>
irsanjul 0:fbb75122015f 39 <02> ->start
irsanjul 0:fbb75122015f 40 GI -> Lokasi <G = Gate> <I = Input> <O = Output>
irsanjul 0:fbb75122015f 41 01 -> ID
irsanjul 0:fbb75122015f 42 S -> Supir <S = Supir> <M = MT>
irsanjul 0:fbb75122015f 43 0123456789ABCDEF -> iButton data
irsanjul 0:fbb75122015f 44 00 -> reserved
irsanjul 0:fbb75122015f 45 <03> -> stop
irsanjul 0:fbb75122015f 46
irsanjul 0:fbb75122015f 47 format heartbeat
irsanjul 0:fbb75122015f 48 <02>GI0100000000<03>
irsanjul 0:fbb75122015f 49
irsanjul 0:fbb75122015f 50 format validasi
irsanjul 0:fbb75122015f 51 <02>V01<03>
irsanjul 0:fbb75122015f 52 1, idle
irsanjul 0:fbb75122015f 53 2, stdby/ idle
irsanjul 0:fbb75122015f 54 3, merah/sopir
irsanjul 0:fbb75122015f 55 4, bitu/ MT
irsanjul 0:fbb75122015f 56 5, buka gate
irsanjul 0:fbb75122015f 57
irsanjul 0:fbb75122015f 58 format ipconfig
irsanjul 0:fbb75122015f 59 <02> 192.168.1.116, 255.255.255.0, 192.168.1.1, 192.168.1.10, 50000, 50001, 50003, GI01$<03>
irsanjul 0:fbb75122015f 60 #192.168.1.116, 255.255.255.0, 192.168.1.1, 192.168.1.10, 50000, 50001, 50003, GI01$
irsanjul 0:fbb75122015f 61 2, stdby
irsanjul 0:fbb75122015f 62 3, merah/sopir
irsanjul 0:fbb75122015f 63 4, biru MT
irsanjul 0:fbb75122015f 64 */
irsanjul 0:fbb75122015f 65
irsanjul 0:fbb75122015f 66 void Standby(bool warna, bool nyala) {
irsanjul 0:fbb75122015f 67 switch (nyala) {
irsanjul 0:fbb75122015f 68 case 0:
irsanjul 0:fbb75122015f 69 if (warna == ijo) {
irsanjul 0:fbb75122015f 70 Stdby_R = 0; // led 2
irsanjul 0:fbb75122015f 71 Std_G.output();
irsanjul 0:fbb75122015f 72 Std_G.write(0);
irsanjul 0:fbb75122015f 73 Std_G.input();
irsanjul 0:fbb75122015f 74 Std_G.mode(PullUp);
irsanjul 0:fbb75122015f 75 } else if (warna == merah) {
irsanjul 0:fbb75122015f 76 Std_R.output();
irsanjul 0:fbb75122015f 77 Std_R.write(0);
irsanjul 0:fbb75122015f 78 Std_R.input();
irsanjul 0:fbb75122015f 79 Std_R.mode(PullUp);
irsanjul 0:fbb75122015f 80 }
irsanjul 0:fbb75122015f 81 break;
irsanjul 0:fbb75122015f 82 case 1:
irsanjul 0:fbb75122015f 83 if (warna == ijo) {
irsanjul 0:fbb75122015f 84 Stdby_R = 1;
irsanjul 0:fbb75122015f 85 Std_G.output();
irsanjul 0:fbb75122015f 86 Std_G.write(1);
irsanjul 0:fbb75122015f 87 } else if (warna == merah) {
irsanjul 0:fbb75122015f 88 Std_R.output();
irsanjul 0:fbb75122015f 89 Std_R.write(1);
irsanjul 0:fbb75122015f 90 }
irsanjul 0:fbb75122015f 91 break;
irsanjul 0:fbb75122015f 92 }
irsanjul 0:fbb75122015f 93 }
irsanjul 0:fbb75122015f 94
irsanjul 0:fbb75122015f 95 void Sopir(bool warna, bool nyala) {
irsanjul 0:fbb75122015f 96 switch (nyala) {
irsanjul 0:fbb75122015f 97 case 0:
irsanjul 0:fbb75122015f 98 if (warna == ijo) {
irsanjul 0:fbb75122015f 99 Sopir_R = 0; // led 3
irsanjul 0:fbb75122015f 100 Spr_G.output();
irsanjul 0:fbb75122015f 101 Spr_G.write(0);
irsanjul 0:fbb75122015f 102 Spr_G.input();
irsanjul 0:fbb75122015f 103 Spr_G.mode(PullUp);
irsanjul 0:fbb75122015f 104 } else if (warna == merah) {
irsanjul 0:fbb75122015f 105 Spr_R.output();
irsanjul 0:fbb75122015f 106 Spr_R.write(0);
irsanjul 0:fbb75122015f 107 Spr_R.input();
irsanjul 0:fbb75122015f 108 Spr_R.mode(PullUp);
irsanjul 0:fbb75122015f 109 }
irsanjul 0:fbb75122015f 110 break;
irsanjul 0:fbb75122015f 111 case 1:
irsanjul 0:fbb75122015f 112 if (warna == ijo) {
irsanjul 0:fbb75122015f 113 Sopir_R = 1;
irsanjul 0:fbb75122015f 114 Spr_G.output();
irsanjul 0:fbb75122015f 115 Spr_G.write(1);
irsanjul 0:fbb75122015f 116 } else if (warna == merah) {
irsanjul 0:fbb75122015f 117 Spr_R.output();
irsanjul 0:fbb75122015f 118 Spr_R.write(1);
irsanjul 0:fbb75122015f 119 }
irsanjul 0:fbb75122015f 120 break;
irsanjul 0:fbb75122015f 121 }
irsanjul 0:fbb75122015f 122 }
irsanjul 0:fbb75122015f 123
irsanjul 0:fbb75122015f 124 void eMTy(bool warna, bool nyala) {
irsanjul 0:fbb75122015f 125 switch (nyala) {
irsanjul 0:fbb75122015f 126 case 0:
irsanjul 0:fbb75122015f 127 if (warna == ijo) {
irsanjul 0:fbb75122015f 128 MT_R = 0; // led 4
irsanjul 0:fbb75122015f 129 Mt_G.output();
irsanjul 0:fbb75122015f 130 Mt_G.write(0);
irsanjul 0:fbb75122015f 131 Mt_G.input();
irsanjul 0:fbb75122015f 132 Mt_G.mode(PullUp);
irsanjul 0:fbb75122015f 133 } else if (warna == merah) {
irsanjul 0:fbb75122015f 134 Mt_R.output();
irsanjul 0:fbb75122015f 135 Mt_R.write(0);
irsanjul 0:fbb75122015f 136 Mt_R.input();
irsanjul 0:fbb75122015f 137 Mt_R.mode(PullUp);
irsanjul 0:fbb75122015f 138 }
irsanjul 0:fbb75122015f 139 break;
irsanjul 0:fbb75122015f 140 case 1:
irsanjul 0:fbb75122015f 141 if (warna == ijo) {
irsanjul 0:fbb75122015f 142 MT_R = 1;
irsanjul 0:fbb75122015f 143 Mt_G.output();
irsanjul 0:fbb75122015f 144 Mt_G.write(1);
irsanjul 0:fbb75122015f 145 } else if (warna == merah) {
irsanjul 0:fbb75122015f 146 Mt_R.output();
irsanjul 0:fbb75122015f 147 Mt_R.write(1);
irsanjul 0:fbb75122015f 148 }
irsanjul 0:fbb75122015f 149 break;
irsanjul 0:fbb75122015f 150 }
irsanjul 0:fbb75122015f 151 }
irsanjul 0:fbb75122015f 152 void mbed_config_init() {
irsanjul 0:fbb75122015f 153 char chr[256];
irsanjul 0:fbb75122015f 154 int ilx = 0;
irsanjul 0:fbb75122015f 155 unsigned char n = 0, x = 0;
irsanjul 0:fbb75122015f 156 FILE *fp = fopen("/sd/IPCONF.TXT", "r");
irsanjul 0:fbb75122015f 157 if (fp == NULL)
irsanjul 0:fbb75122015f 158 dbg.printf("Error opening file");
irsanjul 0:fbb75122015f 159 else {
irsanjul 0:fbb75122015f 160 //dbg.printf("File Exist!\r\n");
irsanjul 0:fbb75122015f 161 do {
irsanjul 0:fbb75122015f 162 ilx++;
irsanjul 0:fbb75122015f 163 chr[ilx] = fgetc(fp);
irsanjul 0:fbb75122015f 164 //dbg.printf("%c",chr[ilx]);
irsanjul 0:fbb75122015f 165 } while (chr[ilx] != '$');
irsanjul 0:fbb75122015f 166 fclose(fp);
irsanjul 0:fbb75122015f 167 }
irsanjul 0:fbb75122015f 168 ilx = 1;
irsanjul 0:fbb75122015f 169 do {
irsanjul 0:fbb75122015f 170 if (chr[ilx] == '#') {
irsanjul 0:fbb75122015f 171 ilx = ilx + 1;
irsanjul 0:fbb75122015f 172 n = 1;
irsanjul 0:fbb75122015f 173 dbg.printf("Start file\r\n");
irsanjul 0:fbb75122015f 174 } else if (chr[ilx] == ',') {
irsanjul 0:fbb75122015f 175 n++;
irsanjul 0:fbb75122015f 176 x = 0;
irsanjul 0:fbb75122015f 177 ilx = ilx + 2;
irsanjul 0:fbb75122015f 178 }
irsanjul 0:fbb75122015f 179 switch (n) {
irsanjul 0:fbb75122015f 180 case 0:
irsanjul 0:fbb75122015f 181 break;
irsanjul 0:fbb75122015f 182 case 1:
irsanjul 0:fbb75122015f 183 IP[x] = chr[ilx];
irsanjul 0:fbb75122015f 184 break;
irsanjul 0:fbb75122015f 185 case 2:
irsanjul 0:fbb75122015f 186 SUBNET[x] = chr[ilx];
irsanjul 0:fbb75122015f 187 break;
irsanjul 0:fbb75122015f 188 case 3:
irsanjul 0:fbb75122015f 189 GATEWAY[x] = chr[ilx];
irsanjul 0:fbb75122015f 190 break;
irsanjul 0:fbb75122015f 191 case 4:
irsanjul 0:fbb75122015f 192 MCAST[x] = chr[ilx];
irsanjul 0:fbb75122015f 193 break;
irsanjul 0:fbb75122015f 194 case 5:
irsanjul 0:fbb75122015f 195 PortListen[x] = chr[ilx];
irsanjul 0:fbb75122015f 196 break;
irsanjul 0:fbb75122015f 197 case 6:
irsanjul 0:fbb75122015f 198 PortSend[x] = chr[ilx];
irsanjul 0:fbb75122015f 199 break;
irsanjul 0:fbb75122015f 200 case 7:
irsanjul 0:fbb75122015f 201 PortHeartB[x] = chr[ilx];
irsanjul 0:fbb75122015f 202 break;
irsanjul 0:fbb75122015f 203 case 8:
irsanjul 0:fbb75122015f 204 IDX[x] = chr[ilx];
irsanjul 0:fbb75122015f 205 break;
irsanjul 0:fbb75122015f 206 case 9:
irsanjul 0:fbb75122015f 207 Baud[x] = chr[ilx];
irsanjul 0:fbb75122015f 208 break;
irsanjul 0:fbb75122015f 209 }
irsanjul 0:fbb75122015f 210 x++;
irsanjul 0:fbb75122015f 211 ilx++;
irsanjul 0:fbb75122015f 212 } while (chr[ilx] != '$');
irsanjul 0:fbb75122015f 213
irsanjul 0:fbb75122015f 214 dbg.printf("IP Device = %s\r\n", IP);
irsanjul 0:fbb75122015f 215 dbg.printf("Subnet = %s\r\n", SUBNET);
irsanjul 0:fbb75122015f 216 dbg.printf("Gateway = %s\r\n", GATEWAY);
irsanjul 0:fbb75122015f 217 dbg.printf("Multicast IP = %s\r\n", MCAST);
irsanjul 0:fbb75122015f 218 dbg.printf("Server to mBed = %s\r\n", PortListen);
irsanjul 0:fbb75122015f 219 dbg.printf("mBed to Multicast = %s\r\n", PortSend);
irsanjul 0:fbb75122015f 220 dbg.printf("HeartBeat Port = %s\r\n", PortHeartB);
irsanjul 0:fbb75122015f 221 dbg.printf("device IDX = %s\r\n", IDX);
irsanjul 0:fbb75122015f 222 dbg.printf("Serial Baudrate = %s\r\n", Baud);
irsanjul 0:fbb75122015f 223 BAUD = atoi(Baud);
irsanjul 0:fbb75122015f 224 }
irsanjul 0:fbb75122015f 225
irsanjul 0:fbb75122015f 226 unsigned char reset(void) {
irsanjul 0:fbb75122015f 227 for (idx = 0; idx < 8; idx++) {
irsanjul 0:fbb75122015f 228 ID[idx] = 0;
irsanjul 0:fbb75122015f 229 }
irsanjul 0:fbb75122015f 230 ix = 0;
irsanjul 0:fbb75122015f 231 iBtn.output();
irsanjul 0:fbb75122015f 232 iBtn.write(0);
irsanjul 0:fbb75122015f 233 wait_us(480);
irsanjul 0:fbb75122015f 234 iBtn.write(1);
irsanjul 0:fbb75122015f 235 iBtn.mode(PullUp);
irsanjul 0:fbb75122015f 236 iBtn.input();
irsanjul 0:fbb75122015f 237
irsanjul 0:fbb75122015f 238 for (int i = 0; i < 180; i++) {
irsanjul 0:fbb75122015f 239 wait_us(1);
irsanjul 0:fbb75122015f 240 if (iBtn == 0) {
irsanjul 0:fbb75122015f 241 ix = 1;
irsanjul 0:fbb75122015f 242 }
irsanjul 0:fbb75122015f 243 }
irsanjul 0:fbb75122015f 244 wait_us(1);
irsanjul 0:fbb75122015f 245 if (ix == 0) {
irsanjul 0:fbb75122015f 246 return 0;
irsanjul 0:fbb75122015f 247 } else
irsanjul 0:fbb75122015f 248 return 1;
irsanjul 0:fbb75122015f 249 }
irsanjul 0:fbb75122015f 250
irsanjul 0:fbb75122015f 251 int write() {
irsanjul 0:fbb75122015f 252 iBtn.output();
irsanjul 0:fbb75122015f 253 iBtn.write(1);
irsanjul 0:fbb75122015f 254 for (idx = 0; idx < 8; idx++) {
irsanjul 0:fbb75122015f 255 for (int i = 0; i < 35; i++) {
irsanjul 0:fbb75122015f 256 wait_us(1);
irsanjul 0:fbb75122015f 257 if (i >= 2) {
irsanjul 0:fbb75122015f 258 iBtn.write((id_addr >> idx) & 1);
irsanjul 0:fbb75122015f 259 } else
irsanjul 0:fbb75122015f 260 iBtn.write(0);
irsanjul 0:fbb75122015f 261 }
irsanjul 0:fbb75122015f 262 iBtn.write(1);
irsanjul 0:fbb75122015f 263 }
irsanjul 0:fbb75122015f 264 wait_us(2);
irsanjul 0:fbb75122015f 265 return 0;
irsanjul 0:fbb75122015f 266 }
irsanjul 0:fbb75122015f 267
irsanjul 0:fbb75122015f 268 int read() {
irsanjul 0:fbb75122015f 269 iBtn.output();
irsanjul 0:fbb75122015f 270 iBtn.write(1);
irsanjul 0:fbb75122015f 271 for (idx = 0; idx < 8; idx++) {
irsanjul 0:fbb75122015f 272 for (idy = 0; idy < 8; idy++) {
irsanjul 0:fbb75122015f 273 width = 0;
irsanjul 0:fbb75122015f 274 iBtn.output();
irsanjul 0:fbb75122015f 275 iBtn.write(0);
irsanjul 0:fbb75122015f 276 wait_us(1);
irsanjul 0:fbb75122015f 277 iBtn.input();
irsanjul 0:fbb75122015f 278 for (int i = 0; i < 35; i++) {
irsanjul 0:fbb75122015f 279 wait_us(1);
irsanjul 0:fbb75122015f 280 if (iBtn.read()) {
irsanjul 0:fbb75122015f 281 width++;
irsanjul 0:fbb75122015f 282 }
irsanjul 0:fbb75122015f 283 }
irsanjul 0:fbb75122015f 284 if (width > 25) {
irsanjul 0:fbb75122015f 285 ID[idx] |= (1 << idy);
irsanjul 0:fbb75122015f 286 }
irsanjul 0:fbb75122015f 287 }
irsanjul 0:fbb75122015f 288 }
irsanjul 0:fbb75122015f 289
irsanjul 0:fbb75122015f 290 for (idx = 0; idx < 8; idx++) {
irsanjul 0:fbb75122015f 291 text[((7 - idx) * 2) + 7] = hex[ID[idx] & 0x0f];
irsanjul 0:fbb75122015f 292 text[((7 - idx) * 2) + 6] = hex[(ID[idx] >> 4) & 0x0f];
irsanjul 0:fbb75122015f 293 }
irsanjul 0:fbb75122015f 294 std::string str;
irsanjul 0:fbb75122015f 295 std::size_t found1;
irsanjul 0:fbb75122015f 296 str += text;
irsanjul 0:fbb75122015f 297
irsanjul 0:fbb75122015f 298 found1 = str.find("FFFFFFFFFF");
irsanjul 0:fbb75122015f 299 if(found1 == std::string::npos)
irsanjul 0:fbb75122015f 300 {
irsanjul 0:fbb75122015f 301 found1 = str.find("000000000000");
irsanjul 0:fbb75122015f 302 if(found1 == std::string::npos)
irsanjul 0:fbb75122015f 303 {
irsanjul 0:fbb75122015f 304 dbg.printf(text);
irsanjul 0:fbb75122015f 305 dbg.printf("\r\n");
irsanjul 0:fbb75122015f 306 }
irsanjul 0:fbb75122015f 307 }
irsanjul 0:fbb75122015f 308 return 0;
irsanjul 0:fbb75122015f 309 }
irsanjul 0:fbb75122015f 310
irsanjul 0:fbb75122015f 311 int cmpstr(const char *str, const char *sub) {
irsanjul 0:fbb75122015f 312 int length = strlen(sub);
irsanjul 0:fbb75122015f 313 if (length == 0)
irsanjul 0:fbb75122015f 314 return 0;
irsanjul 0:fbb75122015f 315 int count = 0;
irsanjul 0:fbb75122015f 316 for (str = strstr(str, sub); str; str = strstr(str + length, sub))
irsanjul 0:fbb75122015f 317 ++count;
irsanjul 0:fbb75122015f 318 return count;
irsanjul 0:fbb75122015f 319 }
irsanjul 0:fbb75122015f 320
irsanjul 0:fbb75122015f 321 void Hartbit(unsigned char std) {
irsanjul 0:fbb75122015f 322 char hb_buff[20], hb_num[10];
irsanjul 0:fbb75122015f 323 static int hb;
irsanjul 0:fbb75122015f 324 hb_buff[0] = IDX[0];
irsanjul 0:fbb75122015f 325 hb_buff[1] = IDX[1];
irsanjul 0:fbb75122015f 326 hb_buff[2] = IDX[2];
irsanjul 0:fbb75122015f 327 hb_buff[3] = IDX[3];
irsanjul 0:fbb75122015f 328
irsanjul 0:fbb75122015f 329 multi.set_address(MCAST, atoi(PortHeartB));
irsanjul 0:fbb75122015f 330 hb++;
irsanjul 0:fbb75122015f 331
irsanjul 0:fbb75122015f 332 switch (std) {
irsanjul 0:fbb75122015f 333 case 0:
irsanjul 0:fbb75122015f 334 //Standby(0, 1);
irsanjul 0:fbb75122015f 335 if (hb < 375) {
irsanjul 0:fbb75122015f 336 Sopir(0, 1);
irsanjul 0:fbb75122015f 337 } else if ((hb >= 375) && (hb < 750)) {
irsanjul 0:fbb75122015f 338 Sopir(0, 0);
irsanjul 0:fbb75122015f 339 } else if ((hb >= 750) && (hb < 1125)) {
irsanjul 0:fbb75122015f 340 Sopir(0, 1);
irsanjul 0:fbb75122015f 341 } else if ((hb >= 1125) && (hb < 1500))
irsanjul 0:fbb75122015f 342 Sopir(0, 0);
irsanjul 0:fbb75122015f 343 if (hb >= 1500) {
irsanjul 0:fbb75122015f 344 sprintf(hb_num, "%d", TICK);
irsanjul 0:fbb75122015f 345 strcat(hb_buff, hb_num);
irsanjul 0:fbb75122015f 346 sock.sendTo(multi, hb_buff, sizeof(hb_buff));
irsanjul 0:fbb75122015f 347 hb = 0;
irsanjul 0:fbb75122015f 348 TICK++;
irsanjul 0:fbb75122015f 349 }
irsanjul 0:fbb75122015f 350 break;
irsanjul 0:fbb75122015f 351 case 1:
irsanjul 0:fbb75122015f 352 Standby(0, 0);
irsanjul 0:fbb75122015f 353 Standby(0, 1);
irsanjul 0:fbb75122015f 354 if (hb < 750) {
irsanjul 0:fbb75122015f 355 Standby(0, 1);
irsanjul 0:fbb75122015f 356 } else if ((hb >= 750) && (hb < 1500)) {
irsanjul 0:fbb75122015f 357 Standby(0, 0);
irsanjul 0:fbb75122015f 358 } else if ((hb >= 1500) && (hb < 2250)) {
irsanjul 0:fbb75122015f 359 Standby(0, 1);
irsanjul 0:fbb75122015f 360 } else if ((hb >= 2250) && (hb < 3000))
irsanjul 0:fbb75122015f 361 Standby(0, 0);
irsanjul 0:fbb75122015f 362 if (hb >= 3000) {
irsanjul 0:fbb75122015f 363 sprintf(hb_num, "%d", TICK);
irsanjul 0:fbb75122015f 364 strcat(hb_buff, hb_num);
irsanjul 0:fbb75122015f 365 sock.sendTo(multi, hb_buff, sizeof(hb_buff));
irsanjul 0:fbb75122015f 366 hb = 0;
irsanjul 0:fbb75122015f 367 TICK++;
irsanjul 0:fbb75122015f 368 }
irsanjul 0:fbb75122015f 369
irsanjul 0:fbb75122015f 370 break;
irsanjul 0:fbb75122015f 371 case 2:
irsanjul 0:fbb75122015f 372 Standby(0, 1);
irsanjul 0:fbb75122015f 373 if (hb >= 7000) {
irsanjul 0:fbb75122015f 374 sprintf(hb_num, "%d", TICK);
irsanjul 0:fbb75122015f 375 strcat(hb_buff, hb_num);
irsanjul 0:fbb75122015f 376 sock.sendTo(multi, hb_buff, sizeof(hb_buff));
irsanjul 0:fbb75122015f 377 hb = 0;
irsanjul 0:fbb75122015f 378 TICK++;
irsanjul 0:fbb75122015f 379 }
irsanjul 0:fbb75122015f 380 break;
irsanjul 0:fbb75122015f 381 }
irsanjul 0:fbb75122015f 382 }
irsanjul 0:fbb75122015f 383
irsanjul 0:fbb75122015f 384 int openForce() {
irsanjul 0:fbb75122015f 385 int n = sock.receiveFrom(server, rx_buff, sizeof(rx_buff));
irsanjul 0:fbb75122015f 386
irsanjul 0:fbb75122015f 387 if (n != 0) {
irsanjul 0:fbb75122015f 388 if ((rx_buff[1] == 'V') && (rx_buff[3] == '5')) {
irsanjul 0:fbb75122015f 389 eMTy(0, 0);
irsanjul 0:fbb75122015f 390 Sopir(0, 0);
irsanjul 0:fbb75122015f 391 wait(1);
irsanjul 0:fbb75122015f 392 Gate = 1; //BYPASS
irsanjul 0:fbb75122015f 393 wait_ms(1500); //BYPASS
irsanjul 0:fbb75122015f 394 Gate = 0; //BYPASS
irsanjul 0:fbb75122015f 395 sprmt = 0;
irsanjul 0:fbb75122015f 396 tx_buff[4] = 'P';
irsanjul 0:fbb75122015f 397 tx_buff[5] = '0';
irsanjul 0:fbb75122015f 398 tx_buff[6] = '0';
irsanjul 0:fbb75122015f 399 multi.set_address(MCAST, atoi(PortSend));
irsanjul 0:fbb75122015f 400 sock.sendTo(multi, tx_buff, sizeof(tx_buff));
irsanjul 0:fbb75122015f 401 } else if (rx_buff[1] == 'R') {
irsanjul 0:fbb75122015f 402 tx_buff[4] = 'P';
irsanjul 0:fbb75122015f 403 tx_buff[5] = '0';
irsanjul 0:fbb75122015f 404 tx_buff[6] = '0';
irsanjul 0:fbb75122015f 405 sprmt = 0;
irsanjul 0:fbb75122015f 406 multi.set_address(MCAST, atoi(PortSend));
irsanjul 0:fbb75122015f 407 sock.sendTo(multi, tx_buff, sizeof(tx_buff));
irsanjul 0:fbb75122015f 408 eMTy(0, 0);
irsanjul 0:fbb75122015f 409 Sopir(0, 0);
irsanjul 0:fbb75122015f 410 Gate = 0;
irsanjul 0:fbb75122015f 411 }
irsanjul 0:fbb75122015f 412 } else {
irsanjul 0:fbb75122015f 413 return 0;
irsanjul 0:fbb75122015f 414 }
irsanjul 0:fbb75122015f 415 return 0;
irsanjul 0:fbb75122015f 416 }
irsanjul 0:fbb75122015f 417
irsanjul 0:fbb75122015f 418 void write_config(char buff[90]){
irsanjul 0:fbb75122015f 419 FILE *fp = fopen("/sd/IPCONF.TXT", "w");
irsanjul 0:fbb75122015f 420 fprintf(fp, buff);
irsanjul 0:fbb75122015f 421 fclose(fp);
irsanjul 0:fbb75122015f 422 }
irsanjul 0:fbb75122015f 423
irsanjul 0:fbb75122015f 424 int set_IP() {
irsanjul 0:fbb75122015f 425 char ptr_data;
irsanjul 0:fbb75122015f 426 //server.set_address(SERVER,ER_SEND_PORT);
irsanjul 0:fbb75122015f 427 //char print_rx_buff[128];
irsanjul 0:fbb75122015f 428 char conf_buff[1024] = { 0 }, print_init[20] = { 0x1b, 0x40, 0x1b, 0x54,
irsanjul 0:fbb75122015f 429 0x1b/*,0x57*/, 0x00, 0x1b, 0x77, 0x00, 0x12, 0x1b, 0x43, 0x00, 0x06,
irsanjul 0:fbb75122015f 430 0x1b, 0x6c, 0x01, 0x12 };
irsanjul 0:fbb75122015f 431 int n = ip_conf.receiveFrom(server, conf_buff, sizeof(conf_buff));
irsanjul 0:fbb75122015f 432 if (n >= 0) {
irsanjul 0:fbb75122015f 433 //printf("Packet from \"%s\": %s\n", server.get_address(), conf_buff);
irsanjul 0:fbb75122015f 434 if (conf_buff[0] == '^') {
irsanjul 0:fbb75122015f 435 if (conf_buff[1] == 'R') {
irsanjul 0:fbb75122015f 436 Reset = 0;
irsanjul 0:fbb75122015f 437 } else if (conf_buff[1] == 'S') {
irsanjul 0:fbb75122015f 438 Sleep = 1;
irsanjul 0:fbb75122015f 439 Gate = 0;
irsanjul 0:fbb75122015f 440
irsanjul 0:fbb75122015f 441 int stop = 0;
irsanjul 0:fbb75122015f 442 while(stop != 1)
irsanjul 0:fbb75122015f 443 {
irsanjul 0:fbb75122015f 444 Spr_G.input();
irsanjul 0:fbb75122015f 445 Spr_G.mode(PullUp);
irsanjul 0:fbb75122015f 446
irsanjul 0:fbb75122015f 447 Mt_G.input();
irsanjul 0:fbb75122015f 448 Mt_G.mode(PullUp);
irsanjul 0:fbb75122015f 449
irsanjul 0:fbb75122015f 450 Std_G.input();
irsanjul 0:fbb75122015f 451 Std_G.mode(PullUp);
irsanjul 0:fbb75122015f 452
irsanjul 0:fbb75122015f 453 ip_conf.receiveFrom(server, conf_buff, sizeof(conf_buff));
irsanjul 0:fbb75122015f 454
irsanjul 0:fbb75122015f 455 if (conf_buff[0] == '^')
irsanjul 0:fbb75122015f 456 {
irsanjul 0:fbb75122015f 457 if (conf_buff[1] == 'W')
irsanjul 0:fbb75122015f 458 {
irsanjul 0:fbb75122015f 459 stop = 1;
irsanjul 0:fbb75122015f 460 Sleep = 0;
irsanjul 0:fbb75122015f 461 }
irsanjul 0:fbb75122015f 462 else if (conf_buff[1] == 'R')
irsanjul 0:fbb75122015f 463 {
irsanjul 0:fbb75122015f 464 stop = 0;
irsanjul 0:fbb75122015f 465 }
irsanjul 0:fbb75122015f 466 else if (conf_buff[1] == 'S')
irsanjul 0:fbb75122015f 467 {
irsanjul 0:fbb75122015f 468 stop = 0;
irsanjul 0:fbb75122015f 469 }
irsanjul 0:fbb75122015f 470 else stop = 0;
irsanjul 0:fbb75122015f 471 }
irsanjul 0:fbb75122015f 472 else if (conf_buff[0] == '2') stop = 0;
irsanjul 0:fbb75122015f 473
irsanjul 0:fbb75122015f 474 else stop = 0;
irsanjul 0:fbb75122015f 475 }
irsanjul 0:fbb75122015f 476 //sock.close(true);
irsanjul 0:fbb75122015f 477 } else if (conf_buff[1] == 'W') {
irsanjul 0:fbb75122015f 478 Sleep = 0;
irsanjul 0:fbb75122015f 479 //sock.bind(atoi(PortListen));
irsanjul 0:fbb75122015f 480 }
irsanjul 0:fbb75122015f 481 } else if (conf_buff[0] == '#') {
irsanjul 0:fbb75122015f 482 write_config(conf_buff);
irsanjul 0:fbb75122015f 483 } else if (conf_buff[0] == 0x0f) {
irsanjul 0:fbb75122015f 484 dbg.printf("%s", print_init);
irsanjul 0:fbb75122015f 485 pc.printf("%s", conf_buff);
irsanjul 0:fbb75122015f 486 ptr_data = *strchr(conf_buff, 0x1d);
irsanjul 0:fbb75122015f 487 if (ptr_data != NULL) {
irsanjul 0:fbb75122015f 488 tx_buff[4] = 'D';
irsanjul 0:fbb75122015f 489 tx_buff[5] = '0';
irsanjul 0:fbb75122015f 490 tx_buff[6] = '1';
irsanjul 0:fbb75122015f 491 multi.set_address(MCAST, atoi(PortSend));
irsanjul 0:fbb75122015f 492 sock.sendTo(multi, tx_buff, sizeof(tx_buff));
irsanjul 0:fbb75122015f 493 dbg.printf("Printer Done\r\n");
irsanjul 0:fbb75122015f 494 }
irsanjul 0:fbb75122015f 495 } else
irsanjul 0:fbb75122015f 496 return 0;
irsanjul 0:fbb75122015f 497 } else {
irsanjul 0:fbb75122015f 498 return 0;
irsanjul 0:fbb75122015f 499 }
irsanjul 0:fbb75122015f 500 return 0;
irsanjul 0:fbb75122015f 501 }
irsanjul 0:fbb75122015f 502
irsanjul 0:fbb75122015f 503 void flush(void)
irsanjul 0:fbb75122015f 504 {
irsanjul 0:fbb75122015f 505 int chk;
irsanjul 0:fbb75122015f 506 do
irsanjul 0:fbb75122015f 507 {
irsanjul 0:fbb75122015f 508 char flush_buff[1024] = { 0 };
irsanjul 0:fbb75122015f 509 chk = ip_conf.receiveFrom(server, flush_buff, sizeof(flush_buff));
irsanjul 0:fbb75122015f 510 //clear flush_buff
irsanjul 0:fbb75122015f 511 for(int i = 0; i <sizeof(flush_buff); i++)
irsanjul 0:fbb75122015f 512 {
irsanjul 0:fbb75122015f 513 flush_buff[i] = '\0';
irsanjul 0:fbb75122015f 514 }
irsanjul 0:fbb75122015f 515 }while (chk > 0);
irsanjul 0:fbb75122015f 516 }
irsanjul 0:fbb75122015f 517
irsanjul 0:fbb75122015f 518 void Set_IP_Manual()
irsanjul 0:fbb75122015f 519 {
irsanjul 0:fbb75122015f 520 sprintf(IP,"192.168.0.14");
irsanjul 0:fbb75122015f 521 sprintf(SUBNET, "255.255.255.0");
irsanjul 0:fbb75122015f 522 sprintf(GATEWAY, "192.168.0.1");
irsanjul 0:fbb75122015f 523 sprintf(MCAST, "224.1.1.8");
irsanjul 0:fbb75122015f 524 sprintf(PortListen, "50007");
irsanjul 0:fbb75122015f 525 sprintf(PortSend, "50001");
irsanjul 0:fbb75122015f 526 sprintf(PortHeartB, "50003");
irsanjul 0:fbb75122015f 527 sprintf(IDX, "GI01");
irsanjul 0:fbb75122015f 528 BAUD = 19200;
irsanjul 0:fbb75122015f 529
irsanjul 0:fbb75122015f 530 dbg.printf("IP Device = %s\r\n", IP);
irsanjul 0:fbb75122015f 531 dbg.printf("Subnet = %s\r\n", SUBNET);
irsanjul 0:fbb75122015f 532 dbg.printf("Gateway = %s\r\n", GATEWAY);
irsanjul 0:fbb75122015f 533 dbg.printf("Multicast IP = %s\r\n", MCAST);
irsanjul 0:fbb75122015f 534 dbg.printf("Server to mBed = %s\r\n", PortListen);
irsanjul 0:fbb75122015f 535 dbg.printf("mBed to Multicast = %s\r\n", PortSend);
irsanjul 0:fbb75122015f 536 dbg.printf("HeartBeat Port = %s\r\n", PortHeartB);
irsanjul 0:fbb75122015f 537 dbg.printf("device IDX = %s\r\n", IDX);
irsanjul 0:fbb75122015f 538 }
irsanjul 0:fbb75122015f 539
irsanjul 0:fbb75122015f 540 int main(void) {
irsanjul 0:fbb75122015f 541 mkdir("/sd/", 0777);
irsanjul 0:fbb75122015f 542
irsanjul 0:fbb75122015f 543 mbed_config_init(); // Digunakan jika setting ip melalui micro sd
irsanjul 0:fbb75122015f 544 Set_IP_Manual(); // Digunakan jika setting ip secara manual
irsanjul 0:fbb75122015f 545
irsanjul 0:fbb75122015f 546 dbg.printf("\r\nPrinter Baud = %d\r\n",BAUD);
irsanjul 0:fbb75122015f 547 pc.baud(BAUD);
irsanjul 0:fbb75122015f 548 dbg.baud(BAUD);
irsanjul 0:fbb75122015f 549 dbg.printf("System Start \r\n");
irsanjul 0:fbb75122015f 550
irsanjul 0:fbb75122015f 551 EthernetInterface eth;
irsanjul 0:fbb75122015f 552 eth.init(IP, SUBNET, GATEWAY);
irsanjul 0:fbb75122015f 553 int n = eth.connect();
irsanjul 0:fbb75122015f 554 if (!n) {
irsanjul 0:fbb75122015f 555 dbg.printf("Ethernet is not connect");
irsanjul 0:fbb75122015f 556 } else
irsanjul 0:fbb75122015f 557 dbg.printf("Ethernet is connect");
irsanjul 0:fbb75122015f 558
irsanjul 0:fbb75122015f 559 ip_conf.bind(IP_CONF_PORT);
irsanjul 0:fbb75122015f 560 ip_conf.set_blocking(false, 0);
irsanjul 0:fbb75122015f 561 server.set_address(MCAST, 50002);
irsanjul 0:fbb75122015f 562
irsanjul 0:fbb75122015f 563 sock.bind(atoi(PortListen));
irsanjul 0:fbb75122015f 564
irsanjul 0:fbb75122015f 565 sock.set_blocking(false, 0);
irsanjul 0:fbb75122015f 566
irsanjul 0:fbb75122015f 567 text[0] = 2;
irsanjul 0:fbb75122015f 568 text[1] = IDX[0];
irsanjul 0:fbb75122015f 569 text[2] = IDX[1];
irsanjul 0:fbb75122015f 570 text[3] = IDX[2];
irsanjul 0:fbb75122015f 571 text[4] = IDX[3];
irsanjul 0:fbb75122015f 572 text[22] = '0';
irsanjul 0:fbb75122015f 573 text[23] = '0';
irsanjul 0:fbb75122015f 574 text[24] = 3;
irsanjul 0:fbb75122015f 575 unsigned char tmp, sndrcv = 0, tmpx = 0;
irsanjul 0:fbb75122015f 576 int timeout = 0;
irsanjul 0:fbb75122015f 577
irsanjul 0:fbb75122015f 578 dbg.printf("Device Ready\r\n");
irsanjul 0:fbb75122015f 579
irsanjul 0:fbb75122015f 580 tx_buff[0] = IDX[0];
irsanjul 0:fbb75122015f 581 tx_buff[1] = IDX[1];
irsanjul 0:fbb75122015f 582 tx_buff[2] = IDX[2];
irsanjul 0:fbb75122015f 583 tx_buff[3] = IDX[3];
irsanjul 0:fbb75122015f 584 tx_buff[4] = 'R';
irsanjul 0:fbb75122015f 585 tx_buff[5] = '0';
irsanjul 0:fbb75122015f 586
irsanjul 0:fbb75122015f 587 multi.set_address(MCAST, atoi(PortSend));
irsanjul 0:fbb75122015f 588 sock.sendTo(multi, tx_buff, sizeof(tx_buff));
irsanjul 0:fbb75122015f 589
irsanjul 0:fbb75122015f 590 eMTy(ijo, 0);
irsanjul 0:fbb75122015f 591 eMTy(merah, 0);
irsanjul 0:fbb75122015f 592 Sopir(ijo, 0);
irsanjul 0:fbb75122015f 593 Sopir(merah, 0);
irsanjul 0:fbb75122015f 594 Standby(ijo, 0);
irsanjul 0:fbb75122015f 595 Standby(merah, 0);
irsanjul 0:fbb75122015f 596 Res_R = 1;
irsanjul 0:fbb75122015f 597
irsanjul 0:fbb75122015f 598 flush(); // clear ethernet port
irsanjul 0:fbb75122015f 599
irsanjul 0:fbb75122015f 600 while (true) {
irsanjul 0:fbb75122015f 601 for (idx = 0; idx < 6; idx++) {
irsanjul 0:fbb75122015f 602 rx_buff[idx] = 0;
irsanjul 0:fbb75122015f 603 }
irsanjul 0:fbb75122015f 604
irsanjul 0:fbb75122015f 605 switch (sndrcv) {
irsanjul 0:fbb75122015f 606 case 0: {
irsanjul 0:fbb75122015f 607 tmp = reset();
irsanjul 0:fbb75122015f 608
irsanjul 0:fbb75122015f 609 if (tmp == 1) {
irsanjul 0:fbb75122015f 610 if (tmpx == 0) {
irsanjul 0:fbb75122015f 611 if (sprmt == 0) {
irsanjul 0:fbb75122015f 612 Sopir(0, 1);
irsanjul 0:fbb75122015f 613 } else {
irsanjul 0:fbb75122015f 614 eMTy(0, 1);
irsanjul 0:fbb75122015f 615 }
irsanjul 0:fbb75122015f 616 wait_ms(70);
irsanjul 0:fbb75122015f 617 reset();
irsanjul 0:fbb75122015f 618 write();
irsanjul 0:fbb75122015f 619 read();
irsanjul 0:fbb75122015f 620 if (sprmt == 0) {
irsanjul 0:fbb75122015f 621 Sopir(0, 0);
irsanjul 0:fbb75122015f 622 text[5] = 'S';
irsanjul 0:fbb75122015f 623 } else {
irsanjul 0:fbb75122015f 624 eMTy(0, 0);
irsanjul 0:fbb75122015f 625 text[5] = 'M';
irsanjul 0:fbb75122015f 626 }
irsanjul 0:fbb75122015f 627 if (cmpstr(text, "F") < 10) {
irsanjul 0:fbb75122015f 628 multi.set_address(MCAST, atoi(PortSend));
irsanjul 0:fbb75122015f 629 sock.sendTo(multi, text, sizeof(text));
irsanjul 0:fbb75122015f 630 sndrcv = 1;
irsanjul 0:fbb75122015f 631 }
irsanjul 0:fbb75122015f 632 }
irsanjul 0:fbb75122015f 633 }
irsanjul 0:fbb75122015f 634
irsanjul 0:fbb75122015f 635 if (sprmt == 0) {
irsanjul 0:fbb75122015f 636 if (sndrcv == 1) {
irsanjul 0:fbb75122015f 637 Hartbit(2);
irsanjul 0:fbb75122015f 638 } else
irsanjul 0:fbb75122015f 639 Hartbit(1);
irsanjul 0:fbb75122015f 640 } else if (sprmt == 1)
irsanjul 0:fbb75122015f 641 Hartbit(0);
irsanjul 0:fbb75122015f 642
irsanjul 0:fbb75122015f 643 tmpx = tmp;
irsanjul 0:fbb75122015f 644 openForce();
irsanjul 0:fbb75122015f 645 break;
irsanjul 0:fbb75122015f 646 }
irsanjul 0:fbb75122015f 647 case 1: {
irsanjul 0:fbb75122015f 648 sock.bind(atoi(PortListen));
irsanjul 0:fbb75122015f 649
irsanjul 0:fbb75122015f 650 int n = sock.receiveFrom(server, rx_buff, sizeof(rx_buff));
irsanjul 0:fbb75122015f 651 if (n != 0) {
irsanjul 0:fbb75122015f 652 dbg.printf("Packet from \"%s\": %s\n", server.get_address(),
irsanjul 0:fbb75122015f 653 rx_buff);
irsanjul 0:fbb75122015f 654 if ((rx_buff[1] == 'V') && (rx_buff[3] == '3')) {
irsanjul 0:fbb75122015f 655 sndrcv = 0;
irsanjul 0:fbb75122015f 656 Sopir(0, 1);
irsanjul 0:fbb75122015f 657 sprmt = 1;
irsanjul 0:fbb75122015f 658 tx_buff[4] = 'O';
irsanjul 0:fbb75122015f 659 tx_buff[5] = 'K';
irsanjul 0:fbb75122015f 660 tx_buff[6] = '!';
irsanjul 0:fbb75122015f 661 multi.set_address(MCAST, atoi(PortSend));
irsanjul 0:fbb75122015f 662 sock.sendTo(multi, tx_buff, sizeof(tx_buff));
irsanjul 0:fbb75122015f 663 dbg.printf("Data S Valid..\r\n");
irsanjul 0:fbb75122015f 664 } else if ((rx_buff[1] == 'A') && (rx_buff[3] == '3')) {
irsanjul 0:fbb75122015f 665 sndrcv = 0;
irsanjul 0:fbb75122015f 666 Sopir(0, 0);
irsanjul 0:fbb75122015f 667 eMTy(0, 1);
irsanjul 0:fbb75122015f 668 wait_ms(200);
irsanjul 0:fbb75122015f 669 eMTy(0, 0);
irsanjul 0:fbb75122015f 670 sprmt = 0;
irsanjul 0:fbb75122015f 671 tx_buff[4] = 'O';
irsanjul 0:fbb75122015f 672 tx_buff[5] = 'K';
irsanjul 0:fbb75122015f 673 tx_buff[6] = '!';
irsanjul 0:fbb75122015f 674 multi.set_address(MCAST, atoi(PortSend));
irsanjul 0:fbb75122015f 675 sock.sendTo(multi, tx_buff, sizeof(tx_buff));
irsanjul 0:fbb75122015f 676 dbg.printf("Data S Not Valid, but M valid, reset!!!!\r\n");
irsanjul 0:fbb75122015f 677 } else if ((rx_buff[1] == 'V') && (rx_buff[3] == '4')) {
irsanjul 0:fbb75122015f 678 Sopir(0, 1);
irsanjul 0:fbb75122015f 679 eMTy(0, 1);
irsanjul 0:fbb75122015f 680 wait_ms(500);
irsanjul 0:fbb75122015f 681 /*
irsanjul 0:fbb75122015f 682 Lakukan perubahan disini menyangkut dengan:
irsanjul 0:fbb75122015f 683 - delay lebih lama
irsanjul 0:fbb75122015f 684 - sprmt yang ditambah
irsanjul 0:fbb75122015f 685 */
irsanjul 0:fbb75122015f 686 tx_buff[4] = 'O';
irsanjul 0:fbb75122015f 687 tx_buff[5] = 'K';
irsanjul 0:fbb75122015f 688 tx_buff[6] = '!';
irsanjul 0:fbb75122015f 689 multi.set_address(MCAST, atoi(PortSend));
irsanjul 0:fbb75122015f 690 sock.sendTo(multi, tx_buff, sizeof(tx_buff));
irsanjul 0:fbb75122015f 691 dbg.printf("Data M Valid..\r\n");
irsanjul 0:fbb75122015f 692 } else if ((rx_buff[1] == 'A') && (rx_buff[3] == '4')) {
irsanjul 0:fbb75122015f 693 sndrcv = 0;
irsanjul 0:fbb75122015f 694 if (sprmt == 0) {
irsanjul 0:fbb75122015f 695 Sopir(0, 0);
irsanjul 0:fbb75122015f 696 } else {
irsanjul 0:fbb75122015f 697 Sopir(0, 1);
irsanjul 0:fbb75122015f 698 }
irsanjul 0:fbb75122015f 699 eMTy(0, 0);
irsanjul 0:fbb75122015f 700 tx_buff[4] = 'O';
irsanjul 0:fbb75122015f 701 tx_buff[5] = 'K';
irsanjul 0:fbb75122015f 702 tx_buff[6] = '!';
irsanjul 0:fbb75122015f 703 multi.set_address(MCAST, atoi(PortSend));
irsanjul 0:fbb75122015f 704 sock.sendTo(multi, tx_buff, sizeof(tx_buff));
irsanjul 0:fbb75122015f 705 dbg.printf("Data M Not Valid, refresh!!!!\r\n");
irsanjul 0:fbb75122015f 706 } else if (rx_buff[1] == 'R') {
irsanjul 0:fbb75122015f 707 sndrcv = 0;
irsanjul 0:fbb75122015f 708 Sopir(0, 0);
irsanjul 0:fbb75122015f 709 eMTy(0, 0);
irsanjul 0:fbb75122015f 710 sprmt = 0;
irsanjul 0:fbb75122015f 711 tx_buff[4] = 'O';
irsanjul 0:fbb75122015f 712 tx_buff[5] = 'K';
irsanjul 0:fbb75122015f 713 tx_buff[6] = '!';
irsanjul 0:fbb75122015f 714 multi.set_address(MCAST, atoi(PortSend));
irsanjul 0:fbb75122015f 715 sock.sendTo(multi, tx_buff, sizeof(tx_buff));
irsanjul 0:fbb75122015f 716 dbg.printf("All Data Not Valid, reset!!!!\r\n");
irsanjul 0:fbb75122015f 717 }
irsanjul 0:fbb75122015f 718 } else {
irsanjul 0:fbb75122015f 719 timeout++;
irsanjul 0:fbb75122015f 720 if (timeout >= 12000) {
irsanjul 0:fbb75122015f 721 sndrcv = 0;
irsanjul 0:fbb75122015f 722 timeout = 0;
irsanjul 0:fbb75122015f 723 }
irsanjul 0:fbb75122015f 724 }
irsanjul 0:fbb75122015f 725 Stdby_R = 1;
irsanjul 0:fbb75122015f 726 openForce();
irsanjul 0:fbb75122015f 727 break;
irsanjul 0:fbb75122015f 728 }
irsanjul 0:fbb75122015f 729 }
irsanjul 0:fbb75122015f 730
irsanjul 0:fbb75122015f 731 // membaca perintah dari server dari port 5004
irsanjul 0:fbb75122015f 732 set_IP();
irsanjul 0:fbb75122015f 733
irsanjul 0:fbb75122015f 734 // cek printer
irsanjul 0:fbb75122015f 735 if (!print_er) {
irsanjul 0:fbb75122015f 736 er_tmp2 = 0;
irsanjul 0:fbb75122015f 737 if (er_tmp1 < 3) {
irsanjul 0:fbb75122015f 738 tx_buff[4] = 'X';
irsanjul 0:fbb75122015f 739 tx_buff[5] = '0';
irsanjul 0:fbb75122015f 740 tx_buff[6] = '1';
irsanjul 0:fbb75122015f 741 multi.set_address(MCAST, atoi(PortSend));
irsanjul 0:fbb75122015f 742 dbg.printf("Printer Error\r\n");
irsanjul 0:fbb75122015f 743 sock.sendTo(multi, tx_buff, sizeof(tx_buff));
irsanjul 0:fbb75122015f 744 } else
irsanjul 0:fbb75122015f 745 er_tmp1 = 5;
irsanjul 0:fbb75122015f 746 er_tmp1++;
irsanjul 0:fbb75122015f 747 } else if (print_er) {
irsanjul 0:fbb75122015f 748 er_tmp1 = 0;
irsanjul 0:fbb75122015f 749 if (er_tmp2 < 3) {
irsanjul 0:fbb75122015f 750 tx_buff[4] = 'N';
irsanjul 0:fbb75122015f 751 tx_buff[5] = '0';
irsanjul 0:fbb75122015f 752 tx_buff[6] = '1';
irsanjul 0:fbb75122015f 753 dbg.printf("Printer Fixed\r\n");
irsanjul 0:fbb75122015f 754 multi.set_address(MCAST, atoi(PortSend));
irsanjul 0:fbb75122015f 755 sock.sendTo(multi, tx_buff, sizeof(tx_buff));
irsanjul 0:fbb75122015f 756 } else
irsanjul 0:fbb75122015f 757 er_tmp2 = 5;
irsanjul 0:fbb75122015f 758 er_tmp2++;
irsanjul 0:fbb75122015f 759 }
irsanjul 0:fbb75122015f 760 }
irsanjul 0:fbb75122015f 761 }