bosko lekovic
/
EthTCPclient_05_03
p1
main.cpp@1:10e2a0bef1b4, 2020-02-15 (annotated)
- Committer:
- markoc
- Date:
- Sat Feb 15 15:38:25 2020 +0000
- Revision:
- 1:10e2a0bef1b4
- Parent:
- 0:b01306ccbbe1
- Child:
- 2:4deba4264f65
radna verzija
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bosko001 | 0:b01306ccbbe1 | 1 | /* mbed Microcontroller Library |
bosko001 | 0:b01306ccbbe1 | 2 | * Copyright (c) 2019 ARM Limited |
bosko001 | 0:b01306ccbbe1 | 3 | * SPDX-License-Identifier: Apache-2.0 |
bosko001 | 0:b01306ccbbe1 | 4 | */ |
bosko001 | 0:b01306ccbbe1 | 5 | |
bosko001 | 0:b01306ccbbe1 | 6 | #include "mbed.h" |
bosko001 | 0:b01306ccbbe1 | 7 | #include "platform/mbed_thread.h" |
bosko001 | 0:b01306ccbbe1 | 8 | |
bosko001 | 0:b01306ccbbe1 | 9 | #include "EthernetInterface.h" |
bosko001 | 0:b01306ccbbe1 | 10 | |
bosko001 | 0:b01306ccbbe1 | 11 | #include <stdio.h> |
bosko001 | 0:b01306ccbbe1 | 12 | #include <string.h> |
markoc | 1:10e2a0bef1b4 | 13 | #include <sstream> |
markoc | 1:10e2a0bef1b4 | 14 | #include <iostream> |
bosko001 | 0:b01306ccbbe1 | 15 | #include "TCPSocket.h" |
bosko001 | 0:b01306ccbbe1 | 16 | |
bosko001 | 0:b01306ccbbe1 | 17 | |
bosko001 | 0:b01306ccbbe1 | 18 | void putOnDispNo(int broj, char boja); |
bosko001 | 0:b01306ccbbe1 | 19 | void putOnDisp(char *s, char boja); |
markoc | 1:10e2a0bef1b4 | 20 | void requestMessageThread(struct s_rm *s); |
bosko001 | 0:b01306ccbbe1 | 21 | void slanjefun(); |
bosko001 | 0:b01306ccbbe1 | 22 | |
bosko001 | 0:b01306ccbbe1 | 23 | #define IP_ADDR "192.168.2.11" |
bosko001 | 0:b01306ccbbe1 | 24 | #define NET_MASK "255.255.248.0" |
bosko001 | 0:b01306ccbbe1 | 25 | #define GATW_ADDR "192.168.1.1" |
bosko001 | 0:b01306ccbbe1 | 26 | #define IP_ADDR_R "192.168.1.9" |
bosko001 | 0:b01306ccbbe1 | 27 | |
bosko001 | 0:b01306ccbbe1 | 28 | |
bosko001 | 0:b01306ccbbe1 | 29 | |
bosko001 | 0:b01306ccbbe1 | 30 | EthernetInterface eth; |
bosko001 | 0:b01306ccbbe1 | 31 | nsapi_error_t err; |
bosko001 | 0:b01306ccbbe1 | 32 | |
markoc | 1:10e2a0bef1b4 | 33 | char * extract_string( char delimiter, char *ulazni_str, int n_str ); |
markoc | 1:10e2a0bef1b4 | 34 | void putOnDisp( char *s, char boja); |
bosko001 | 0:b01306ccbbe1 | 35 | |
markoc | 1:10e2a0bef1b4 | 36 | int strcount( char *ps) |
bosko001 | 0:b01306ccbbe1 | 37 | { |
bosko001 | 0:b01306ccbbe1 | 38 | int i=0; |
bosko001 | 0:b01306ccbbe1 | 39 | for(i=0; ps[i] != 0; i++); |
bosko001 | 0:b01306ccbbe1 | 40 | return i; |
bosko001 | 0:b01306ccbbe1 | 41 | } |
bosko001 | 0:b01306ccbbe1 | 42 | |
bosko001 | 0:b01306ccbbe1 | 43 | void tcpThread_fun( EthernetInterface *e); |
markoc | 1:10e2a0bef1b4 | 44 | void requestMessage( EthernetInterface *e); |
bosko001 | 0:b01306ccbbe1 | 45 | |
bosko001 | 0:b01306ccbbe1 | 46 | DigitalOut tast(PTB19,0); |
bosko001 | 0:b01306ccbbe1 | 47 | //RawSerial rs485(PTC17,PTC16); |
bosko001 | 0:b01306ccbbe1 | 48 | RawSerial rs485(PTD3,PTD2); |
markoc | 1:10e2a0bef1b4 | 49 | |
markoc | 1:10e2a0bef1b4 | 50 | struct s_rm |
markoc | 1:10e2a0bef1b4 | 51 | { |
markoc | 1:10e2a0bef1b4 | 52 | EthernetInterface *e; |
markoc | 1:10e2a0bef1b4 | 53 | SocketAddress *sa; |
markoc | 1:10e2a0bef1b4 | 54 | char *tx_msg; |
markoc | 1:10e2a0bef1b4 | 55 | void (*fun_rec)(char*); |
markoc | 1:10e2a0bef1b4 | 56 | }; |
markoc | 1:10e2a0bef1b4 | 57 | |
markoc | 1:10e2a0bef1b4 | 58 | char crm_req[]={'{','1','|','1','}'};//"{1|1}"; |
markoc | 1:10e2a0bef1b4 | 59 | char crm_resp[100]; |
markoc | 1:10e2a0bef1b4 | 60 | void crm_fun( char *rx) |
markoc | 1:10e2a0bef1b4 | 61 | { |
markoc | 1:10e2a0bef1b4 | 62 | printf("crm_resp: %s\n\r",rx); |
markoc | 1:10e2a0bef1b4 | 63 | strcmp(crm_resp, rx); |
markoc | 1:10e2a0bef1b4 | 64 | } |
markoc | 1:10e2a0bef1b4 | 65 | |
markoc | 1:10e2a0bef1b4 | 66 | char srm_req[]="{5|1|1}"; |
markoc | 1:10e2a0bef1b4 | 67 | void srm_fun( char *rx ) |
markoc | 1:10e2a0bef1b4 | 68 | { |
markoc | 1:10e2a0bef1b4 | 69 | printf("srm_resp: %s\n\r",rx); |
markoc | 1:10e2a0bef1b4 | 70 | char *strno= extract_string(';',rx, 2); |
markoc | 1:10e2a0bef1b4 | 71 | char boja = 0; |
markoc | 1:10e2a0bef1b4 | 72 | if(atoi(strno) == 0) { boja = 1; } |
markoc | 1:10e2a0bef1b4 | 73 | else if(atoi(strno) >0 && atoi(strno)<5) { boja = 4; } |
markoc | 1:10e2a0bef1b4 | 74 | else { boja = 2; } |
markoc | 1:10e2a0bef1b4 | 75 | printf("broj: %d - boja: %d\n\r", atoi(strno),boja); |
markoc | 1:10e2a0bef1b4 | 76 | putOnDisp((char*)strno, boja); |
markoc | 1:10e2a0bef1b4 | 77 | } |
markoc | 1:10e2a0bef1b4 | 78 | |
markoc | 1:10e2a0bef1b4 | 79 | |
markoc | 1:10e2a0bef1b4 | 80 | |
bosko001 | 0:b01306ccbbe1 | 81 | |
bosko001 | 0:b01306ccbbe1 | 82 | int main(void) |
bosko001 | 0:b01306ccbbe1 | 83 | { |
markoc | 1:10e2a0bef1b4 | 84 | |
markoc | 1:10e2a0bef1b4 | 85 | |
bosko001 | 0:b01306ccbbe1 | 86 | |
bosko001 | 0:b01306ccbbe1 | 87 | rs485.format(8, mbed::RawSerial::Even, 1); |
bosko001 | 0:b01306ccbbe1 | 88 | |
bosko001 | 0:b01306ccbbe1 | 89 | Thread t; |
bosko001 | 0:b01306ccbbe1 | 90 | // t.start( slanjefun ); |
bosko001 | 0:b01306ccbbe1 | 91 | |
bosko001 | 0:b01306ccbbe1 | 92 | |
bosko001 | 0:b01306ccbbe1 | 93 | |
bosko001 | 0:b01306ccbbe1 | 94 | if( (err = eth.set_network(IP_ADDR,NET_MASK,GATW_ADDR))!= NSAPI_ERROR_OK ) { printf(" greska setovanja mreze %d \n\r", err); return 1; } |
bosko001 | 0:b01306ccbbe1 | 95 | if( (err = eth.connect()) != NSAPI_ERROR_OK ) { printf(" greska konekcije %d \n\r", err); return 1; } |
bosko001 | 0:b01306ccbbe1 | 96 | |
bosko001 | 0:b01306ccbbe1 | 97 | const char *ip = eth.get_ip_address() ; |
bosko001 | 0:b01306ccbbe1 | 98 | |
bosko001 | 0:b01306ccbbe1 | 99 | |
bosko001 | 0:b01306ccbbe1 | 100 | printf( "MAC adresa: %s\n\r", eth.get_mac_address() ); |
bosko001 | 0:b01306ccbbe1 | 101 | printf( "IP adresa: %s\n\r", ip); |
bosko001 | 0:b01306ccbbe1 | 102 | printf( "net mask: %s\n\r", eth.get_netmask() ); |
bosko001 | 0:b01306ccbbe1 | 103 | printf( "GW adresa: %s\n\r", eth.get_gateway() ); |
markoc | 1:10e2a0bef1b4 | 104 | |
markoc | 1:10e2a0bef1b4 | 105 | SocketAddress sa("192.168.2.254", 12197); |
markoc | 1:10e2a0bef1b4 | 106 | |
markoc | 1:10e2a0bef1b4 | 107 | struct s_rm s_crm ={ ð, &sa, crm_req, crm_fun }; |
markoc | 1:10e2a0bef1b4 | 108 | struct s_rm s_srm ={ ð, &sa, srm_req, srm_fun }; |
bosko001 | 0:b01306ccbbe1 | 109 | |
markoc | 1:10e2a0bef1b4 | 110 | // Thread TCPThread; |
markoc | 1:10e2a0bef1b4 | 111 | // TCPThread.start( callback( tcpThread_fun, ð )); |
markoc | 1:10e2a0bef1b4 | 112 | |
markoc | 1:10e2a0bef1b4 | 113 | |
markoc | 1:10e2a0bef1b4 | 114 | Thread CRMThread; |
markoc | 1:10e2a0bef1b4 | 115 | CRMThread.start( callback( requestMessageThread, &s_crm )); |
markoc | 1:10e2a0bef1b4 | 116 | CRMThread.join(); |
bosko001 | 0:b01306ccbbe1 | 117 | int i=0; |
bosko001 | 0:b01306ccbbe1 | 118 | |
bosko001 | 0:b01306ccbbe1 | 119 | while(true) |
bosko001 | 0:b01306ccbbe1 | 120 | { |
markoc | 1:10e2a0bef1b4 | 121 | thread_sleep_for(10000); |
markoc | 1:10e2a0bef1b4 | 122 | Thread SRMThread; |
markoc | 1:10e2a0bef1b4 | 123 | printf("Thread id= %d\n\r",(int)&SRMThread); |
markoc | 1:10e2a0bef1b4 | 124 | SRMThread.start( callback( requestMessageThread, &s_srm )); |
markoc | 1:10e2a0bef1b4 | 125 | SRMThread.join(); |
bosko001 | 0:b01306ccbbe1 | 126 | printf("main %d\n\r",i++); |
bosko001 | 0:b01306ccbbe1 | 127 | } |
bosko001 | 0:b01306ccbbe1 | 128 | // printf("\n\rmain: Kraj, error=%d\n\r",err); fflush(stdout); |
bosko001 | 0:b01306ccbbe1 | 129 | } |
bosko001 | 0:b01306ccbbe1 | 130 | |
bosko001 | 0:b01306ccbbe1 | 131 | |
bosko001 | 0:b01306ccbbe1 | 132 | |
bosko001 | 0:b01306ccbbe1 | 133 | |
bosko001 | 0:b01306ccbbe1 | 134 | unsigned char sendBuffer[]= "HTTP/1.1 200 OK\r\nConnection: close\r\nContent-Type: text/html\r\n\r\nMarko je ovde"; // HTTP Message Body, length = 11 |
bosko001 | 0:b01306ccbbe1 | 135 | unsigned char sb[1000]="HTTP/1.1 200 OK\r\nConnection: close\r\nContent-Type: text/html\r\n\r\n"; |
bosko001 | 0:b01306ccbbe1 | 136 | |
bosko001 | 0:b01306ccbbe1 | 137 | |
bosko001 | 0:b01306ccbbe1 | 138 | |
bosko001 | 0:b01306ccbbe1 | 139 | unsigned char txbuf[100]="test konekcije"; |
bosko001 | 0:b01306ccbbe1 | 140 | unsigned char rxbuf[100]; |
bosko001 | 0:b01306ccbbe1 | 141 | |
bosko001 | 0:b01306ccbbe1 | 142 | void tcpThread_fun( EthernetInterface *e) |
bosko001 | 0:b01306ccbbe1 | 143 | { |
bosko001 | 0:b01306ccbbe1 | 144 | |
bosko001 | 0:b01306ccbbe1 | 145 | TCPSocket tcpSocket; |
bosko001 | 0:b01306ccbbe1 | 146 | nsapi_error_t err=NULL; |
markoc | 1:10e2a0bef1b4 | 147 | SocketAddress sa("192.168.1.26", 12197); |
bosko001 | 0:b01306ccbbe1 | 148 | printf("ip= %s, ver= %d, port= %d\n\r", sa.get_ip_address(), sa.get_ip_version(), sa.get_port() ); |
bosko001 | 0:b01306ccbbe1 | 149 | int i=1; |
bosko001 | 0:b01306ccbbe1 | 150 | |
bosko001 | 0:b01306ccbbe1 | 151 | while(true) |
bosko001 | 0:b01306ccbbe1 | 152 | { |
bosko001 | 0:b01306ccbbe1 | 153 | if((err=tcpSocket.open( e )) ==0 ) |
bosko001 | 0:b01306ccbbe1 | 154 | { |
bosko001 | 0:b01306ccbbe1 | 155 | if((err = tcpSocket.connect(sa)) == 0) |
bosko001 | 0:b01306ccbbe1 | 156 | { |
bosko001 | 0:b01306ccbbe1 | 157 | while( true) |
bosko001 | 0:b01306ccbbe1 | 158 | { |
bosko001 | 0:b01306ccbbe1 | 159 | printf("iteracija %d \n\r", i); |
markoc | 1:10e2a0bef1b4 | 160 | if(strcount((char*)txbuf) ) |
bosko001 | 0:b01306ccbbe1 | 161 | { |
markoc | 1:10e2a0bef1b4 | 162 | printf(" za slanje %d bajtova: %s\n\r", strcount((char*)txbuf), txbuf); |
markoc | 1:10e2a0bef1b4 | 163 | if( (err = tcpSocket.send(txbuf,strcount((char*)txbuf))<0 ) ) { printf("send err %d\n\r", err); break; } |
bosko001 | 0:b01306ccbbe1 | 164 | else(" poslano %d bajtova od: %s\n\r", err, txbuf); |
bosko001 | 0:b01306ccbbe1 | 165 | memset(txbuf, 0, sizeof(txbuf) ); |
bosko001 | 0:b01306ccbbe1 | 166 | } |
bosko001 | 0:b01306ccbbe1 | 167 | else |
bosko001 | 0:b01306ccbbe1 | 168 | { |
bosko001 | 0:b01306ccbbe1 | 169 | if( (err = tcpSocket.recv(rxbuf,sizeof(rxbuf)) <0 )) { printf("recv err %d\n\r", err); break; } |
bosko001 | 0:b01306ccbbe1 | 170 | else |
bosko001 | 0:b01306ccbbe1 | 171 | { |
bosko001 | 0:b01306ccbbe1 | 172 | |
bosko001 | 0:b01306ccbbe1 | 173 | printf(" prim tel %d: %s\n\r", err, rxbuf) ; |
bosko001 | 0:b01306ccbbe1 | 174 | { |
bosko001 | 0:b01306ccbbe1 | 175 | extern void putOnDisp(char *, char ); |
bosko001 | 0:b01306ccbbe1 | 176 | putOnDisp((char*)rxbuf, 1); |
bosko001 | 0:b01306ccbbe1 | 177 | } |
bosko001 | 0:b01306ccbbe1 | 178 | memcpy(txbuf, rxbuf, sizeof(txbuf) ); |
bosko001 | 0:b01306ccbbe1 | 179 | memset(rxbuf, 0, sizeof(rxbuf)); |
bosko001 | 0:b01306ccbbe1 | 180 | } |
bosko001 | 0:b01306ccbbe1 | 181 | } |
bosko001 | 0:b01306ccbbe1 | 182 | printf("kraj iteracije %d \n\r", i++); |
bosko001 | 0:b01306ccbbe1 | 183 | fflush(stdout); |
bosko001 | 0:b01306ccbbe1 | 184 | } |
bosko001 | 0:b01306ccbbe1 | 185 | } |
bosko001 | 0:b01306ccbbe1 | 186 | else printf("greska otvaranja konekcije %d\n\r", err); |
bosko001 | 0:b01306ccbbe1 | 187 | } |
bosko001 | 0:b01306ccbbe1 | 188 | else printf("greska otvaranja socketa %d\n\r", err); |
bosko001 | 0:b01306ccbbe1 | 189 | |
bosko001 | 0:b01306ccbbe1 | 190 | printf("zatvaranje konekcije\n\r"); |
bosko001 | 0:b01306ccbbe1 | 191 | tcpSocket.close(); |
bosko001 | 0:b01306ccbbe1 | 192 | thread_sleep_for(2000); |
bosko001 | 0:b01306ccbbe1 | 193 | } |
bosko001 | 0:b01306ccbbe1 | 194 | } |
bosko001 | 0:b01306ccbbe1 | 195 | |
markoc | 1:10e2a0bef1b4 | 196 | char conf_server_ip[] = "192.168.1.26"; |
markoc | 1:10e2a0bef1b4 | 197 | int conf_server_port = 12197; |
markoc | 1:10e2a0bef1b4 | 198 | unsigned char msg_request_message[] = "{5|1|1;2}"; |
markoc | 1:10e2a0bef1b4 | 199 | unsigned char msg_catalogue_request_message[] = "{1|1}"; |
markoc | 1:10e2a0bef1b4 | 200 | |
markoc | 1:10e2a0bef1b4 | 201 | |
markoc | 1:10e2a0bef1b4 | 202 | |
markoc | 1:10e2a0bef1b4 | 203 | |
markoc | 1:10e2a0bef1b4 | 204 | void requestMessage(EthernetInterface *e) |
markoc | 1:10e2a0bef1b4 | 205 | { |
bosko001 | 0:b01306ccbbe1 | 206 | |
markoc | 1:10e2a0bef1b4 | 207 | while(true) |
markoc | 1:10e2a0bef1b4 | 208 | { |
markoc | 1:10e2a0bef1b4 | 209 | |
markoc | 1:10e2a0bef1b4 | 210 | TCPSocket tcpSocket; |
markoc | 1:10e2a0bef1b4 | 211 | nsapi_error_t err=NULL; |
markoc | 1:10e2a0bef1b4 | 212 | SocketAddress sa(conf_server_ip, conf_server_port); |
markoc | 1:10e2a0bef1b4 | 213 | char rxbuf[1024]; |
markoc | 1:10e2a0bef1b4 | 214 | memset(rxbuf, 0, sizeof(rxbuf)); |
markoc | 1:10e2a0bef1b4 | 215 | if((err=tcpSocket.open( e )) ==0 ) |
markoc | 1:10e2a0bef1b4 | 216 | { |
markoc | 1:10e2a0bef1b4 | 217 | if((err = tcpSocket.connect(sa)) == 0) |
markoc | 1:10e2a0bef1b4 | 218 | { |
markoc | 1:10e2a0bef1b4 | 219 | if( (err = tcpSocket.send(msg_request_message,strcount((char*)msg_request_message))<0 ) ) |
markoc | 1:10e2a0bef1b4 | 220 | { |
markoc | 1:10e2a0bef1b4 | 221 | printf("requestMessage - send err: %d\n\r", err); |
markoc | 1:10e2a0bef1b4 | 222 | } |
markoc | 1:10e2a0bef1b4 | 223 | else |
markoc | 1:10e2a0bef1b4 | 224 | { |
markoc | 1:10e2a0bef1b4 | 225 | printf("requestMessage - poslano: %d bajtova od: %s\n\r", err, msg_request_message); |
markoc | 1:10e2a0bef1b4 | 226 | if( (err = tcpSocket.recv(rxbuf,sizeof(rxbuf)) <0 )) { printf("requestMessage - recv err %d\n\r", err); } |
markoc | 1:10e2a0bef1b4 | 227 | else |
markoc | 1:10e2a0bef1b4 | 228 | { |
markoc | 1:10e2a0bef1b4 | 229 | |
markoc | 1:10e2a0bef1b4 | 230 | printf("requestMessage - prim tel %d: %s\n\r", err, rxbuf) ; |
markoc | 1:10e2a0bef1b4 | 231 | |
markoc | 1:10e2a0bef1b4 | 232 | // char* parts[100]; |
markoc | 1:10e2a0bef1b4 | 233 | // int partcount = 0; |
markoc | 1:10e2a0bef1b4 | 234 | // |
markoc | 1:10e2a0bef1b4 | 235 | // parts[partcount++] = rxbuf; |
markoc | 1:10e2a0bef1b4 | 236 | // |
markoc | 1:10e2a0bef1b4 | 237 | // char* ptr = rxbuf; |
markoc | 1:10e2a0bef1b4 | 238 | // while(*ptr) { //check if the string is over |
markoc | 1:10e2a0bef1b4 | 239 | // if(*ptr == ';') { |
markoc | 1:10e2a0bef1b4 | 240 | // *ptr = 0; |
markoc | 1:10e2a0bef1b4 | 241 | // parts[partcount++] = ptr + 1; |
markoc | 1:10e2a0bef1b4 | 242 | // } |
markoc | 1:10e2a0bef1b4 | 243 | // ptr++; |
markoc | 1:10e2a0bef1b4 | 244 | // } |
markoc | 1:10e2a0bef1b4 | 245 | |
markoc | 1:10e2a0bef1b4 | 246 | extern char * extract_string( char delimiter, char *ulazni_str, int n_str ); |
bosko001 | 0:b01306ccbbe1 | 247 | |
markoc | 1:10e2a0bef1b4 | 248 | char *strno= extract_string(';',rxbuf, 2); |
markoc | 1:10e2a0bef1b4 | 249 | printf("%s\n\r",strno); |
markoc | 1:10e2a0bef1b4 | 250 | int color = 0; |
markoc | 1:10e2a0bef1b4 | 251 | int brojMesta = atoi(strno); |
markoc | 1:10e2a0bef1b4 | 252 | printf("Broj mesta: %d\n\r",brojMesta); |
markoc | 1:10e2a0bef1b4 | 253 | if (brojMesta==0) { color=1; } |
markoc | 1:10e2a0bef1b4 | 254 | else if(brojMesta>0&&brojMesta<10){ color=4; } |
markoc | 1:10e2a0bef1b4 | 255 | else { color=2; } |
markoc | 1:10e2a0bef1b4 | 256 | printf("Color: %d\n\r",color); |
markoc | 1:10e2a0bef1b4 | 257 | { |
markoc | 1:10e2a0bef1b4 | 258 | extern void putOnDisp(char *, char ); |
markoc | 1:10e2a0bef1b4 | 259 | putOnDisp((char*)strno, color); |
markoc | 1:10e2a0bef1b4 | 260 | } |
markoc | 1:10e2a0bef1b4 | 261 | } |
markoc | 1:10e2a0bef1b4 | 262 | } |
markoc | 1:10e2a0bef1b4 | 263 | } |
markoc | 1:10e2a0bef1b4 | 264 | |
markoc | 1:10e2a0bef1b4 | 265 | printf("requestMessage - zatvaranje konekcije\n\r"); |
markoc | 1:10e2a0bef1b4 | 266 | tcpSocket.close(); |
markoc | 1:10e2a0bef1b4 | 267 | } |
markoc | 1:10e2a0bef1b4 | 268 | |
markoc | 1:10e2a0bef1b4 | 269 | thread_sleep_for(30000); |
markoc | 1:10e2a0bef1b4 | 270 | } |
markoc | 1:10e2a0bef1b4 | 271 | |
markoc | 1:10e2a0bef1b4 | 272 | } |
markoc | 1:10e2a0bef1b4 | 273 | |
markoc | 1:10e2a0bef1b4 | 274 | // |
markoc | 1:10e2a0bef1b4 | 275 | //struct s_rm |
markoc | 1:10e2a0bef1b4 | 276 | //{ |
markoc | 1:10e2a0bef1b4 | 277 | // EthernetInterface *e; |
markoc | 1:10e2a0bef1b4 | 278 | // SocketAddress *sa; |
markoc | 1:10e2a0bef1b4 | 279 | // char *tx_msg; |
markoc | 1:10e2a0bef1b4 | 280 | // void (*fun_rec)(char*); |
markoc | 1:10e2a0bef1b4 | 281 | //}; |
markoc | 1:10e2a0bef1b4 | 282 | |
markoc | 1:10e2a0bef1b4 | 283 | |
markoc | 1:10e2a0bef1b4 | 284 | void requestMessageThread(struct s_rm *s) |
markoc | 1:10e2a0bef1b4 | 285 | { |
markoc | 1:10e2a0bef1b4 | 286 | |
markoc | 1:10e2a0bef1b4 | 287 | TCPSocket tcpSocket; |
markoc | 1:10e2a0bef1b4 | 288 | nsapi_error_t err=NULL; |
markoc | 1:10e2a0bef1b4 | 289 | char rxbuf[1024]; |
markoc | 1:10e2a0bef1b4 | 290 | memset(rxbuf, 0, sizeof(rxbuf)); |
markoc | 1:10e2a0bef1b4 | 291 | if((err=tcpSocket.open( s->e )) ==0 ) |
markoc | 1:10e2a0bef1b4 | 292 | { |
markoc | 1:10e2a0bef1b4 | 293 | if((err = tcpSocket.connect(*(s->sa))) == 0) |
markoc | 1:10e2a0bef1b4 | 294 | { |
markoc | 1:10e2a0bef1b4 | 295 | char *ss = s->tx_msg; |
markoc | 1:10e2a0bef1b4 | 296 | if( (err = tcpSocket.send(s->tx_msg,strcount(ss))<0 ) ) |
markoc | 1:10e2a0bef1b4 | 297 | { |
markoc | 1:10e2a0bef1b4 | 298 | printf("requestMessage - send err: %d\n\r", err); |
markoc | 1:10e2a0bef1b4 | 299 | } |
markoc | 1:10e2a0bef1b4 | 300 | else |
markoc | 1:10e2a0bef1b4 | 301 | { |
markoc | 1:10e2a0bef1b4 | 302 | printf("requestMessage - poslano: %d bajtova od: %s\n\r", err, s->tx_msg); |
markoc | 1:10e2a0bef1b4 | 303 | if( (err = tcpSocket.recv(rxbuf,sizeof(rxbuf)) <0 )) { printf("requestMessage - recv err %d\n\r", err); } |
markoc | 1:10e2a0bef1b4 | 304 | else |
markoc | 1:10e2a0bef1b4 | 305 | { |
markoc | 1:10e2a0bef1b4 | 306 | |
markoc | 1:10e2a0bef1b4 | 307 | printf("requestMessage - prim tel %d: %s\n\r", err, rxbuf) ; |
markoc | 1:10e2a0bef1b4 | 308 | s->fun_rec( rxbuf ); |
markoc | 1:10e2a0bef1b4 | 309 | } |
markoc | 1:10e2a0bef1b4 | 310 | } |
markoc | 1:10e2a0bef1b4 | 311 | } |
markoc | 1:10e2a0bef1b4 | 312 | |
markoc | 1:10e2a0bef1b4 | 313 | printf("requestMessage - zatvaranje konekcije\n\r"); |
markoc | 1:10e2a0bef1b4 | 314 | tcpSocket.close(); |
markoc | 1:10e2a0bef1b4 | 315 | } |
markoc | 1:10e2a0bef1b4 | 316 | |
markoc | 1:10e2a0bef1b4 | 317 | } |
bosko001 | 0:b01306ccbbe1 | 318 | |
bosko001 | 0:b01306ccbbe1 | 319 | // |
bosko001 | 0:b01306ccbbe1 | 320 | //enum nsapi_error { |
bosko001 | 0:b01306ccbbe1 | 321 | // NSAPI_ERROR_OK = 0, /*!< no error */ |
bosko001 | 0:b01306ccbbe1 | 322 | // NSAPI_ERROR_WOULD_BLOCK = -3001, /*!< no data is not available but call is non-blocking */ |
bosko001 | 0:b01306ccbbe1 | 323 | // NSAPI_ERROR_UNSUPPORTED = -3002, /*!< unsupported functionality */ |
bosko001 | 0:b01306ccbbe1 | 324 | // NSAPI_ERROR_PARAMETER = -3003, /*!< invalid configuration */ |
bosko001 | 0:b01306ccbbe1 | 325 | // NSAPI_ERROR_NO_CONNECTION = -3004, /*!< not connected to a network */ |
bosko001 | 0:b01306ccbbe1 | 326 | // NSAPI_ERROR_NO_SOCKET = -3005, /*!< socket not available for use */ |
bosko001 | 0:b01306ccbbe1 | 327 | // NSAPI_ERROR_NO_ADDRESS = -3006, /*!< IP address is not known */ |
bosko001 | 0:b01306ccbbe1 | 328 | // NSAPI_ERROR_NO_MEMORY = -3007, /*!< memory resource not available */ |
bosko001 | 0:b01306ccbbe1 | 329 | // NSAPI_ERROR_NO_SSID = -3008, /*!< ssid not found */ |
bosko001 | 0:b01306ccbbe1 | 330 | // NSAPI_ERROR_DNS_FAILURE = -3009, /*!< DNS failed to complete successfully */ |
bosko001 | 0:b01306ccbbe1 | 331 | // NSAPI_ERROR_DHCP_FAILURE = -3010, /*!< DHCP failed to complete successfully */ |
bosko001 | 0:b01306ccbbe1 | 332 | // NSAPI_ERROR_AUTH_FAILURE = -3011, /*!< connection to access point failed */ |
bosko001 | 0:b01306ccbbe1 | 333 | // NSAPI_ERROR_DEVICE_ERROR = -3012, /*!< failure interfacing with the network processor */ |
bosko001 | 0:b01306ccbbe1 | 334 | // NSAPI_ERROR_IN_PROGRESS = -3013, /*!< operation (eg connect) in progress */ |
bosko001 | 0:b01306ccbbe1 | 335 | // NSAPI_ERROR_ALREADY = -3014, /*!< operation (eg connect) already in progress */ |
bosko001 | 0:b01306ccbbe1 | 336 | // NSAPI_ERROR_IS_CONNECTED = -3015, /*!< socket is already connected */ |
bosko001 | 0:b01306ccbbe1 | 337 | // NSAPI_ERROR_CONNECTION_LOST = -3016, /*!< connection lost */ |
bosko001 | 0:b01306ccbbe1 | 338 | // NSAPI_ERROR_CONNECTION_TIMEOUT = -3017, /*!< connection timed out */ |
bosko001 | 0:b01306ccbbe1 | 339 | //}; |
bosko001 | 0:b01306ccbbe1 | 340 | |
bosko001 | 0:b01306ccbbe1 | 341 | |
bosko001 | 0:b01306ccbbe1 | 342 | |
bosko001 | 0:b01306ccbbe1 | 343 | |
bosko001 | 0:b01306ccbbe1 | 344 | |
bosko001 | 0:b01306ccbbe1 | 345 | void ev(int){tast=0;} |
bosko001 | 0:b01306ccbbe1 | 346 | |
bosko001 | 0:b01306ccbbe1 | 347 | unsigned char buf_tel[100] = {0x71,0x00,0x0D,0x71,0x43,0x01,0x1B,0x4A,0x01,0x01,0x1B,0x43,1,0x33,0xD5,0x16}; |
bosko001 | 0:b01306ccbbe1 | 348 | void putOnDisp( char *s, char boja) |
bosko001 | 0:b01306ccbbe1 | 349 | { |
markoc | 1:10e2a0bef1b4 | 350 | int ubb = 13+strlen(s)+1+1-4-2; // ubb = ukupan broj bajtova |
bosko001 | 0:b01306ccbbe1 | 351 | unsigned char lh, ll; |
bosko001 | 0:b01306ccbbe1 | 352 | ll=ubb; |
bosko001 | 0:b01306ccbbe1 | 353 | lh=ubb>>8; |
markoc | 1:10e2a0bef1b4 | 354 | printf("Boja: %d\n\r",boja); |
markoc | 1:10e2a0bef1b4 | 355 | buf_tel[12]=boja; |
bosko001 | 0:b01306ccbbe1 | 356 | memcpy(buf_tel+13, s, strlen(s)+2); |
bosko001 | 0:b01306ccbbe1 | 357 | |
bosko001 | 0:b01306ccbbe1 | 358 | int suma=0; |
bosko001 | 0:b01306ccbbe1 | 359 | for(int i = 4; i < 13+strlen(s); i++) { |
bosko001 | 0:b01306ccbbe1 | 360 | suma +=buf_tel[i]; |
bosko001 | 0:b01306ccbbe1 | 361 | } |
bosko001 | 0:b01306ccbbe1 | 362 | |
bosko001 | 0:b01306ccbbe1 | 363 | buf_tel[13+strlen(s)] = suma; |
bosko001 | 0:b01306ccbbe1 | 364 | buf_tel[13+strlen(s)+1] = 22; |
bosko001 | 0:b01306ccbbe1 | 365 | buf_tel[1]=lh; |
bosko001 | 0:b01306ccbbe1 | 366 | buf_tel[2]=ll; |
bosko001 | 0:b01306ccbbe1 | 367 | |
bosko001 | 0:b01306ccbbe1 | 368 | fflush(stdout); |
bosko001 | 0:b01306ccbbe1 | 369 | |
bosko001 | 0:b01306ccbbe1 | 370 | tast=1; |
bosko001 | 0:b01306ccbbe1 | 371 | rs485.write(buf_tel, 21,ev); |
bosko001 | 0:b01306ccbbe1 | 372 | } |
bosko001 | 0:b01306ccbbe1 | 373 | void putOnDispNo(int broj, char boja) |
bosko001 | 0:b01306ccbbe1 | 374 | { |
bosko001 | 0:b01306ccbbe1 | 375 | char s[10]; |
bosko001 | 0:b01306ccbbe1 | 376 | sprintf( s, "%d", broj); |
bosko001 | 0:b01306ccbbe1 | 377 | putOnDisp( s, boja); |
bosko001 | 0:b01306ccbbe1 | 378 | } |
bosko001 | 0:b01306ccbbe1 | 379 | |
bosko001 | 0:b01306ccbbe1 | 380 | |
bosko001 | 0:b01306ccbbe1 | 381 | void slanjefun(){ |
bosko001 | 0:b01306ccbbe1 | 382 | |
bosko001 | 0:b01306ccbbe1 | 383 | int j=0; |
bosko001 | 0:b01306ccbbe1 | 384 | |
bosko001 | 0:b01306ccbbe1 | 385 | |
bosko001 | 0:b01306ccbbe1 | 386 | while (true) { |
bosko001 | 0:b01306ccbbe1 | 387 | // printf("disp thred %d\n\r",j); |
bosko001 | 0:b01306ccbbe1 | 388 | if(j >10 && j<15 ) putOnDisp("des",1); |
bosko001 | 0:b01306ccbbe1 | 389 | else putOnDispNo(j,1); |
bosko001 | 0:b01306ccbbe1 | 390 | j++; |
bosko001 | 0:b01306ccbbe1 | 391 | thread_sleep_for(1000); |
bosko001 | 0:b01306ccbbe1 | 392 | } |
bosko001 | 0:b01306ccbbe1 | 393 | } |
markoc | 1:10e2a0bef1b4 | 394 | |
markoc | 1:10e2a0bef1b4 | 395 | |
markoc | 1:10e2a0bef1b4 | 396 | char * extract_string( char delimiter, char *ulazni_str, int n_str ) |
markoc | 1:10e2a0bef1b4 | 397 | { |
markoc | 1:10e2a0bef1b4 | 398 | int br_str=0; |
markoc | 1:10e2a0bef1b4 | 399 | char *ret_str=ulazni_str; |
markoc | 1:10e2a0bef1b4 | 400 | char* ptr = ulazni_str; |
markoc | 1:10e2a0bef1b4 | 401 | |
markoc | 1:10e2a0bef1b4 | 402 | while(*ptr) |
markoc | 1:10e2a0bef1b4 | 403 | { |
markoc | 1:10e2a0bef1b4 | 404 | if(*ptr == delimiter) |
markoc | 1:10e2a0bef1b4 | 405 | { |
markoc | 1:10e2a0bef1b4 | 406 | *ptr = 0; |
markoc | 1:10e2a0bef1b4 | 407 | if( br_str == n_str ) return ret_str; |
markoc | 1:10e2a0bef1b4 | 408 | else |
markoc | 1:10e2a0bef1b4 | 409 | { |
markoc | 1:10e2a0bef1b4 | 410 | br_str++; |
markoc | 1:10e2a0bef1b4 | 411 | ret_str = ptr + 1; |
markoc | 1:10e2a0bef1b4 | 412 | } |
markoc | 1:10e2a0bef1b4 | 413 | } |
markoc | 1:10e2a0bef1b4 | 414 | ptr++; |
markoc | 1:10e2a0bef1b4 | 415 | } |
markoc | 1:10e2a0bef1b4 | 416 | if( br_str == n_str ) return ret_str; |
markoc | 1:10e2a0bef1b4 | 417 | return NULL; |
markoc | 1:10e2a0bef1b4 | 418 | } |
markoc | 1:10e2a0bef1b4 | 419 |