bosko lekovic
/
EthTCPclient_05_03
p1
main.cpp@0:b01306ccbbe1, 2020-02-12 (annotated)
- Committer:
- bosko001
- Date:
- Wed Feb 12 20:25:05 2020 +0000
- Revision:
- 0:b01306ccbbe1
- Child:
- 1:10e2a0bef1b4
verzija 2
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> |
bosko001 | 0:b01306ccbbe1 | 13 | #include "TCPSocket.h" |
bosko001 | 0:b01306ccbbe1 | 14 | |
bosko001 | 0:b01306ccbbe1 | 15 | |
bosko001 | 0:b01306ccbbe1 | 16 | void putOnDispNo(int broj, char boja); |
bosko001 | 0:b01306ccbbe1 | 17 | void putOnDisp(char *s, char boja); |
bosko001 | 0:b01306ccbbe1 | 18 | void slanjefun(); |
bosko001 | 0:b01306ccbbe1 | 19 | |
bosko001 | 0:b01306ccbbe1 | 20 | #define IP_ADDR "192.168.2.11" |
bosko001 | 0:b01306ccbbe1 | 21 | #define NET_MASK "255.255.248.0" |
bosko001 | 0:b01306ccbbe1 | 22 | #define GATW_ADDR "192.168.1.1" |
bosko001 | 0:b01306ccbbe1 | 23 | #define IP_ADDR_R "192.168.1.9" |
bosko001 | 0:b01306ccbbe1 | 24 | |
bosko001 | 0:b01306ccbbe1 | 25 | |
bosko001 | 0:b01306ccbbe1 | 26 | |
bosko001 | 0:b01306ccbbe1 | 27 | EthernetInterface eth; |
bosko001 | 0:b01306ccbbe1 | 28 | nsapi_error_t err; |
bosko001 | 0:b01306ccbbe1 | 29 | |
bosko001 | 0:b01306ccbbe1 | 30 | |
bosko001 | 0:b01306ccbbe1 | 31 | int strcount( unsigned char *ps) |
bosko001 | 0:b01306ccbbe1 | 32 | { |
bosko001 | 0:b01306ccbbe1 | 33 | int i=0; |
bosko001 | 0:b01306ccbbe1 | 34 | for(i=0; ps[i] != 0; i++); |
bosko001 | 0:b01306ccbbe1 | 35 | return i; |
bosko001 | 0:b01306ccbbe1 | 36 | } |
bosko001 | 0:b01306ccbbe1 | 37 | |
bosko001 | 0:b01306ccbbe1 | 38 | void tcpThread_fun( EthernetInterface *e); |
bosko001 | 0:b01306ccbbe1 | 39 | |
bosko001 | 0:b01306ccbbe1 | 40 | DigitalOut tast(PTB19,0); |
bosko001 | 0:b01306ccbbe1 | 41 | //RawSerial rs485(PTC17,PTC16); |
bosko001 | 0:b01306ccbbe1 | 42 | RawSerial rs485(PTD3,PTD2); |
bosko001 | 0:b01306ccbbe1 | 43 | |
bosko001 | 0:b01306ccbbe1 | 44 | |
bosko001 | 0:b01306ccbbe1 | 45 | int main(void) |
bosko001 | 0:b01306ccbbe1 | 46 | { |
bosko001 | 0:b01306ccbbe1 | 47 | |
bosko001 | 0:b01306ccbbe1 | 48 | rs485.format(8, mbed::RawSerial::Even, 1); |
bosko001 | 0:b01306ccbbe1 | 49 | |
bosko001 | 0:b01306ccbbe1 | 50 | Thread t; |
bosko001 | 0:b01306ccbbe1 | 51 | // t.start( slanjefun ); |
bosko001 | 0:b01306ccbbe1 | 52 | |
bosko001 | 0:b01306ccbbe1 | 53 | |
bosko001 | 0:b01306ccbbe1 | 54 | |
bosko001 | 0:b01306ccbbe1 | 55 | 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 | 56 | if( (err = eth.connect()) != NSAPI_ERROR_OK ) { printf(" greska konekcije %d \n\r", err); return 1; } |
bosko001 | 0:b01306ccbbe1 | 57 | |
bosko001 | 0:b01306ccbbe1 | 58 | const char *ip = eth.get_ip_address() ; |
bosko001 | 0:b01306ccbbe1 | 59 | |
bosko001 | 0:b01306ccbbe1 | 60 | |
bosko001 | 0:b01306ccbbe1 | 61 | printf( "MAC adresa: %s\n\r", eth.get_mac_address() ); |
bosko001 | 0:b01306ccbbe1 | 62 | printf( "IP adresa: %s\n\r", ip); |
bosko001 | 0:b01306ccbbe1 | 63 | printf( "net mask: %s\n\r", eth.get_netmask() ); |
bosko001 | 0:b01306ccbbe1 | 64 | printf( "GW adresa: %s\n\r", eth.get_gateway() ); |
bosko001 | 0:b01306ccbbe1 | 65 | |
bosko001 | 0:b01306ccbbe1 | 66 | Thread TCPThread; |
bosko001 | 0:b01306ccbbe1 | 67 | TCPThread.start( callback( tcpThread_fun, ð )); |
bosko001 | 0:b01306ccbbe1 | 68 | |
bosko001 | 0:b01306ccbbe1 | 69 | int i=0; |
bosko001 | 0:b01306ccbbe1 | 70 | |
bosko001 | 0:b01306ccbbe1 | 71 | while(true) |
bosko001 | 0:b01306ccbbe1 | 72 | { |
bosko001 | 0:b01306ccbbe1 | 73 | thread_sleep_for(20000); |
bosko001 | 0:b01306ccbbe1 | 74 | printf("main %d\n\r",i++); |
bosko001 | 0:b01306ccbbe1 | 75 | } |
bosko001 | 0:b01306ccbbe1 | 76 | // printf("\n\rmain: Kraj, error=%d\n\r",err); fflush(stdout); |
bosko001 | 0:b01306ccbbe1 | 77 | } |
bosko001 | 0:b01306ccbbe1 | 78 | |
bosko001 | 0:b01306ccbbe1 | 79 | |
bosko001 | 0:b01306ccbbe1 | 80 | |
bosko001 | 0:b01306ccbbe1 | 81 | |
bosko001 | 0:b01306ccbbe1 | 82 | 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 | 83 | unsigned char sb[1000]="HTTP/1.1 200 OK\r\nConnection: close\r\nContent-Type: text/html\r\n\r\n"; |
bosko001 | 0:b01306ccbbe1 | 84 | |
bosko001 | 0:b01306ccbbe1 | 85 | |
bosko001 | 0:b01306ccbbe1 | 86 | |
bosko001 | 0:b01306ccbbe1 | 87 | unsigned char txbuf[100]="test konekcije"; |
bosko001 | 0:b01306ccbbe1 | 88 | unsigned char rxbuf[100]; |
bosko001 | 0:b01306ccbbe1 | 89 | |
bosko001 | 0:b01306ccbbe1 | 90 | void tcpThread_fun( EthernetInterface *e) |
bosko001 | 0:b01306ccbbe1 | 91 | { |
bosko001 | 0:b01306ccbbe1 | 92 | |
bosko001 | 0:b01306ccbbe1 | 93 | TCPSocket tcpSocket; |
bosko001 | 0:b01306ccbbe1 | 94 | nsapi_error_t err=NULL; |
bosko001 | 0:b01306ccbbe1 | 95 | SocketAddress sa("192.168.2.254", 12197); |
bosko001 | 0:b01306ccbbe1 | 96 | printf("ip= %s, ver= %d, port= %d\n\r", sa.get_ip_address(), sa.get_ip_version(), sa.get_port() ); |
bosko001 | 0:b01306ccbbe1 | 97 | int i=1; |
bosko001 | 0:b01306ccbbe1 | 98 | |
bosko001 | 0:b01306ccbbe1 | 99 | while(true) |
bosko001 | 0:b01306ccbbe1 | 100 | { |
bosko001 | 0:b01306ccbbe1 | 101 | if((err=tcpSocket.open( e )) ==0 ) |
bosko001 | 0:b01306ccbbe1 | 102 | { |
bosko001 | 0:b01306ccbbe1 | 103 | if((err = tcpSocket.connect(sa)) == 0) |
bosko001 | 0:b01306ccbbe1 | 104 | { |
bosko001 | 0:b01306ccbbe1 | 105 | while( true) |
bosko001 | 0:b01306ccbbe1 | 106 | { |
bosko001 | 0:b01306ccbbe1 | 107 | printf("iteracija %d \n\r", i); |
bosko001 | 0:b01306ccbbe1 | 108 | if(strcount(txbuf) ) |
bosko001 | 0:b01306ccbbe1 | 109 | { |
bosko001 | 0:b01306ccbbe1 | 110 | printf(" za slanje %d bajtova: %s\n\r", strcount(txbuf), txbuf); |
bosko001 | 0:b01306ccbbe1 | 111 | if( (err = tcpSocket.send(txbuf,strcount(txbuf))<0 ) ) { printf("send err %d\n\r", err); break; } |
bosko001 | 0:b01306ccbbe1 | 112 | else(" poslano %d bajtova od: %s\n\r", err, txbuf); |
bosko001 | 0:b01306ccbbe1 | 113 | memset(txbuf, 0, sizeof(txbuf) ); |
bosko001 | 0:b01306ccbbe1 | 114 | } |
bosko001 | 0:b01306ccbbe1 | 115 | else |
bosko001 | 0:b01306ccbbe1 | 116 | { |
bosko001 | 0:b01306ccbbe1 | 117 | if( (err = tcpSocket.recv(rxbuf,sizeof(rxbuf)) <0 )) { printf("recv err %d\n\r", err); break; } |
bosko001 | 0:b01306ccbbe1 | 118 | else |
bosko001 | 0:b01306ccbbe1 | 119 | { |
bosko001 | 0:b01306ccbbe1 | 120 | |
bosko001 | 0:b01306ccbbe1 | 121 | printf(" prim tel %d: %s\n\r", err, rxbuf) ; |
bosko001 | 0:b01306ccbbe1 | 122 | { |
bosko001 | 0:b01306ccbbe1 | 123 | extern void putOnDisp(char *, char ); |
bosko001 | 0:b01306ccbbe1 | 124 | putOnDisp((char*)rxbuf, 1); |
bosko001 | 0:b01306ccbbe1 | 125 | } |
bosko001 | 0:b01306ccbbe1 | 126 | memcpy(txbuf, rxbuf, sizeof(txbuf) ); |
bosko001 | 0:b01306ccbbe1 | 127 | memset(rxbuf, 0, sizeof(rxbuf)); |
bosko001 | 0:b01306ccbbe1 | 128 | } |
bosko001 | 0:b01306ccbbe1 | 129 | } |
bosko001 | 0:b01306ccbbe1 | 130 | printf("kraj iteracije %d \n\r", i++); |
bosko001 | 0:b01306ccbbe1 | 131 | fflush(stdout); |
bosko001 | 0:b01306ccbbe1 | 132 | } |
bosko001 | 0:b01306ccbbe1 | 133 | } |
bosko001 | 0:b01306ccbbe1 | 134 | else printf("greska otvaranja konekcije %d\n\r", err); |
bosko001 | 0:b01306ccbbe1 | 135 | } |
bosko001 | 0:b01306ccbbe1 | 136 | else printf("greska otvaranja socketa %d\n\r", err); |
bosko001 | 0:b01306ccbbe1 | 137 | |
bosko001 | 0:b01306ccbbe1 | 138 | printf("zatvaranje konekcije\n\r"); |
bosko001 | 0:b01306ccbbe1 | 139 | tcpSocket.close(); |
bosko001 | 0:b01306ccbbe1 | 140 | thread_sleep_for(2000); |
bosko001 | 0:b01306ccbbe1 | 141 | } |
bosko001 | 0:b01306ccbbe1 | 142 | } |
bosko001 | 0:b01306ccbbe1 | 143 | |
bosko001 | 0:b01306ccbbe1 | 144 | |
bosko001 | 0:b01306ccbbe1 | 145 | |
bosko001 | 0:b01306ccbbe1 | 146 | |
bosko001 | 0:b01306ccbbe1 | 147 | // |
bosko001 | 0:b01306ccbbe1 | 148 | //enum nsapi_error { |
bosko001 | 0:b01306ccbbe1 | 149 | // NSAPI_ERROR_OK = 0, /*!< no error */ |
bosko001 | 0:b01306ccbbe1 | 150 | // NSAPI_ERROR_WOULD_BLOCK = -3001, /*!< no data is not available but call is non-blocking */ |
bosko001 | 0:b01306ccbbe1 | 151 | // NSAPI_ERROR_UNSUPPORTED = -3002, /*!< unsupported functionality */ |
bosko001 | 0:b01306ccbbe1 | 152 | // NSAPI_ERROR_PARAMETER = -3003, /*!< invalid configuration */ |
bosko001 | 0:b01306ccbbe1 | 153 | // NSAPI_ERROR_NO_CONNECTION = -3004, /*!< not connected to a network */ |
bosko001 | 0:b01306ccbbe1 | 154 | // NSAPI_ERROR_NO_SOCKET = -3005, /*!< socket not available for use */ |
bosko001 | 0:b01306ccbbe1 | 155 | // NSAPI_ERROR_NO_ADDRESS = -3006, /*!< IP address is not known */ |
bosko001 | 0:b01306ccbbe1 | 156 | // NSAPI_ERROR_NO_MEMORY = -3007, /*!< memory resource not available */ |
bosko001 | 0:b01306ccbbe1 | 157 | // NSAPI_ERROR_NO_SSID = -3008, /*!< ssid not found */ |
bosko001 | 0:b01306ccbbe1 | 158 | // NSAPI_ERROR_DNS_FAILURE = -3009, /*!< DNS failed to complete successfully */ |
bosko001 | 0:b01306ccbbe1 | 159 | // NSAPI_ERROR_DHCP_FAILURE = -3010, /*!< DHCP failed to complete successfully */ |
bosko001 | 0:b01306ccbbe1 | 160 | // NSAPI_ERROR_AUTH_FAILURE = -3011, /*!< connection to access point failed */ |
bosko001 | 0:b01306ccbbe1 | 161 | // NSAPI_ERROR_DEVICE_ERROR = -3012, /*!< failure interfacing with the network processor */ |
bosko001 | 0:b01306ccbbe1 | 162 | // NSAPI_ERROR_IN_PROGRESS = -3013, /*!< operation (eg connect) in progress */ |
bosko001 | 0:b01306ccbbe1 | 163 | // NSAPI_ERROR_ALREADY = -3014, /*!< operation (eg connect) already in progress */ |
bosko001 | 0:b01306ccbbe1 | 164 | // NSAPI_ERROR_IS_CONNECTED = -3015, /*!< socket is already connected */ |
bosko001 | 0:b01306ccbbe1 | 165 | // NSAPI_ERROR_CONNECTION_LOST = -3016, /*!< connection lost */ |
bosko001 | 0:b01306ccbbe1 | 166 | // NSAPI_ERROR_CONNECTION_TIMEOUT = -3017, /*!< connection timed out */ |
bosko001 | 0:b01306ccbbe1 | 167 | //}; |
bosko001 | 0:b01306ccbbe1 | 168 | |
bosko001 | 0:b01306ccbbe1 | 169 | |
bosko001 | 0:b01306ccbbe1 | 170 | |
bosko001 | 0:b01306ccbbe1 | 171 | |
bosko001 | 0:b01306ccbbe1 | 172 | |
bosko001 | 0:b01306ccbbe1 | 173 | void ev(int){tast=0;} |
bosko001 | 0:b01306ccbbe1 | 174 | |
bosko001 | 0:b01306ccbbe1 | 175 | unsigned char buf_tel[100] = {0x71,0x00,0x0D,0x71,0x43,0x01,0x1B,0x4A,0x01,0x01,0x1B,0x43,1,0x33,0xD5,0x16}; |
bosko001 | 0:b01306ccbbe1 | 176 | void putOnDisp( char *s, char boja) |
bosko001 | 0:b01306ccbbe1 | 177 | { |
bosko001 | 0:b01306ccbbe1 | 178 | int ubb = 13+strlen(s)+1+1-4-2; |
bosko001 | 0:b01306ccbbe1 | 179 | unsigned char lh, ll; |
bosko001 | 0:b01306ccbbe1 | 180 | ll=ubb; |
bosko001 | 0:b01306ccbbe1 | 181 | lh=ubb>>8; |
bosko001 | 0:b01306ccbbe1 | 182 | |
bosko001 | 0:b01306ccbbe1 | 183 | memcpy(buf_tel+13, s, strlen(s)+2); |
bosko001 | 0:b01306ccbbe1 | 184 | |
bosko001 | 0:b01306ccbbe1 | 185 | int suma=0; |
bosko001 | 0:b01306ccbbe1 | 186 | for(int i = 4; i < 13+strlen(s); i++) { |
bosko001 | 0:b01306ccbbe1 | 187 | suma +=buf_tel[i]; |
bosko001 | 0:b01306ccbbe1 | 188 | } |
bosko001 | 0:b01306ccbbe1 | 189 | |
bosko001 | 0:b01306ccbbe1 | 190 | buf_tel[13+strlen(s)] = suma; |
bosko001 | 0:b01306ccbbe1 | 191 | buf_tel[13+strlen(s)+1] = 22; |
bosko001 | 0:b01306ccbbe1 | 192 | buf_tel[1]=lh; |
bosko001 | 0:b01306ccbbe1 | 193 | buf_tel[2]=ll; |
bosko001 | 0:b01306ccbbe1 | 194 | |
bosko001 | 0:b01306ccbbe1 | 195 | fflush(stdout); |
bosko001 | 0:b01306ccbbe1 | 196 | |
bosko001 | 0:b01306ccbbe1 | 197 | tast=1; |
bosko001 | 0:b01306ccbbe1 | 198 | rs485.write(buf_tel, 21,ev); |
bosko001 | 0:b01306ccbbe1 | 199 | } |
bosko001 | 0:b01306ccbbe1 | 200 | void putOnDispNo(int broj, char boja) |
bosko001 | 0:b01306ccbbe1 | 201 | { |
bosko001 | 0:b01306ccbbe1 | 202 | char s[10]; |
bosko001 | 0:b01306ccbbe1 | 203 | sprintf( s, "%d", broj); |
bosko001 | 0:b01306ccbbe1 | 204 | putOnDisp( s, boja); |
bosko001 | 0:b01306ccbbe1 | 205 | } |
bosko001 | 0:b01306ccbbe1 | 206 | |
bosko001 | 0:b01306ccbbe1 | 207 | |
bosko001 | 0:b01306ccbbe1 | 208 | void slanjefun(){ |
bosko001 | 0:b01306ccbbe1 | 209 | |
bosko001 | 0:b01306ccbbe1 | 210 | int j=0; |
bosko001 | 0:b01306ccbbe1 | 211 | |
bosko001 | 0:b01306ccbbe1 | 212 | |
bosko001 | 0:b01306ccbbe1 | 213 | while (true) { |
bosko001 | 0:b01306ccbbe1 | 214 | // printf("disp thred %d\n\r",j); |
bosko001 | 0:b01306ccbbe1 | 215 | if(j >10 && j<15 ) putOnDisp("des",1); |
bosko001 | 0:b01306ccbbe1 | 216 | else putOnDispNo(j,1); |
bosko001 | 0:b01306ccbbe1 | 217 | j++; |
bosko001 | 0:b01306ccbbe1 | 218 | thread_sleep_for(1000); |
bosko001 | 0:b01306ccbbe1 | 219 | } |
bosko001 | 0:b01306ccbbe1 | 220 | } |