bosko lekovic
/
EthToCom_11
nova proba
UdpToScom.cpp@2:45b351b4fc2a, 2020-04-05 (annotated)
- Committer:
- bosko001
- Date:
- Sun Apr 05 12:56:41 2020 +0000
- Revision:
- 2:45b351b4fc2a
- Child:
- 9:893843262a1f
n0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bosko001 | 2:45b351b4fc2a | 1 | #include "mbed.h" |
bosko001 | 2:45b351b4fc2a | 2 | #include "clubbing.h" |
bosko001 | 2:45b351b4fc2a | 3 | #include "UdpToScom.h" |
bosko001 | 2:45b351b4fc2a | 4 | #include "EthernetSetup.h" |
bosko001 | 2:45b351b4fc2a | 5 | |
bosko001 | 2:45b351b4fc2a | 6 | |
bosko001 | 2:45b351b4fc2a | 7 | |
bosko001 | 2:45b351b4fc2a | 8 | struct S_EthernetAddress_Remote |
bosko001 | 2:45b351b4fc2a | 9 | /*{ |
bosko001 | 2:45b351b4fc2a | 10 | char ip[16]; |
bosko001 | 2:45b351b4fc2a | 11 | int port; |
bosko001 | 2:45b351b4fc2a | 12 | }*/ s_EthAddRemote = {"192.168.1.14", 13000 }, *ps_ear = &s_EthAddRemote, |
bosko001 | 2:45b351b4fc2a | 13 | s_EthAddBroadcast = {"0.0.0.0", 13000 }, *ps_eab = &s_EthAddBroadcast; |
bosko001 | 2:45b351b4fc2a | 14 | |
bosko001 | 2:45b351b4fc2a | 15 | char lokaUdpPort[6] = "13000", localUdpPortFlash[6] = "65535"; |
bosko001 | 2:45b351b4fc2a | 16 | |
bosko001 | 2:45b351b4fc2a | 17 | #define SerialTxPin PTC17 |
bosko001 | 2:45b351b4fc2a | 18 | #define SerialRxPin PTC16 |
bosko001 | 2:45b351b4fc2a | 19 | char scomBaud[7] = "9600", scomBaudFlash[7] = "200000"; |
bosko001 | 2:45b351b4fc2a | 20 | |
bosko001 | 2:45b351b4fc2a | 21 | |
bosko001 | 2:45b351b4fc2a | 22 | /* globalni pointeri*/ |
bosko001 | 2:45b351b4fc2a | 23 | |
bosko001 | 2:45b351b4fc2a | 24 | |
bosko001 | 2:45b351b4fc2a | 25 | EthernetInterface *gp_eth; |
bosko001 | 2:45b351b4fc2a | 26 | |
bosko001 | 2:45b351b4fc2a | 27 | UDPSocket *gp_udpSocket = NULL; |
bosko001 | 2:45b351b4fc2a | 28 | Thread *gp_udpRxThread = NULL; |
bosko001 | 2:45b351b4fc2a | 29 | |
bosko001 | 2:45b351b4fc2a | 30 | Thread *gp_scomRxThread=NULL; |
bosko001 | 2:45b351b4fc2a | 31 | UARTSerial *gp_scom=NULL; |
bosko001 | 2:45b351b4fc2a | 32 | |
bosko001 | 2:45b351b4fc2a | 33 | |
bosko001 | 2:45b351b4fc2a | 34 | int get_baud( void ) |
bosko001 | 2:45b351b4fc2a | 35 | { |
bosko001 | 2:45b351b4fc2a | 36 | int a = atoi(scomBaudFlash), b = atoi(scomBaud), ret; |
bosko001 | 2:45b351b4fc2a | 37 | if( (a > 90) && (a < 100000) ) ret = a; |
bosko001 | 2:45b351b4fc2a | 38 | else if( (b > 90) && (b < 100000) ) ret = b; |
bosko001 | 2:45b351b4fc2a | 39 | else ret = 96/*00*/; |
bosko001 | 2:45b351b4fc2a | 40 | printf(" baud rates = %d %d final %d\n\r", a, b, ret ); |
bosko001 | 2:45b351b4fc2a | 41 | return ret; |
bosko001 | 2:45b351b4fc2a | 42 | } |
bosko001 | 2:45b351b4fc2a | 43 | |
bosko001 | 2:45b351b4fc2a | 44 | /***************** Predaja na UDP ****************************/ |
bosko001 | 2:45b351b4fc2a | 45 | void sendUdp( struct S_EthernetAddress_Remote *ps_ear, char *buffer, int val ) |
bosko001 | 2:45b351b4fc2a | 46 | { |
bosko001 | 2:45b351b4fc2a | 47 | if( gp_udpSocket ) |
bosko001 | 2:45b351b4fc2a | 48 | { |
bosko001 | 2:45b351b4fc2a | 49 | printf(" UDPpaket ip=%s port=%d val=%d\n\r", ps_ear->ip, ps_ear->port, val); |
bosko001 | 2:45b351b4fc2a | 50 | gp_udpSocket->sendto(ps_ear->ip, ps_ear->port, (const uint8_t*)buffer, val); |
bosko001 | 2:45b351b4fc2a | 51 | } |
bosko001 | 2:45b351b4fc2a | 52 | else printf("UDPsocket == NULL\n\r"); |
bosko001 | 2:45b351b4fc2a | 53 | } |
bosko001 | 2:45b351b4fc2a | 54 | |
bosko001 | 2:45b351b4fc2a | 55 | /**************** Predaja na Serial Com ********************************/ |
bosko001 | 2:45b351b4fc2a | 56 | |
bosko001 | 2:45b351b4fc2a | 57 | void sendScom( struct UARTSerial *p_scom, char *buffer, int val) |
bosko001 | 2:45b351b4fc2a | 58 | { |
bosko001 | 2:45b351b4fc2a | 59 | if( p_scom ) |
bosko001 | 2:45b351b4fc2a | 60 | { |
bosko001 | 2:45b351b4fc2a | 61 | int num; |
bosko001 | 2:45b351b4fc2a | 62 | if(val>0) num = p_scom->write( (const uint8_t*) buffer, val ); |
bosko001 | 2:45b351b4fc2a | 63 | printf(" Poslan serial com paket od %d bajtova", num); |
bosko001 | 2:45b351b4fc2a | 64 | } |
bosko001 | 2:45b351b4fc2a | 65 | else printf("Serial com neinicijalizovan\n\r"); |
bosko001 | 2:45b351b4fc2a | 66 | } |
bosko001 | 2:45b351b4fc2a | 67 | |
bosko001 | 2:45b351b4fc2a | 68 | /************** Prijem na serial com i slanje na UDP *********************/ |
bosko001 | 2:45b351b4fc2a | 69 | |
bosko001 | 2:45b351b4fc2a | 70 | void scomrx_fun( void ) |
bosko001 | 2:45b351b4fc2a | 71 | { |
bosko001 | 2:45b351b4fc2a | 72 | |
bosko001 | 2:45b351b4fc2a | 73 | uint8_t readbuff[1500]; |
bosko001 | 2:45b351b4fc2a | 74 | int totno=0; |
bosko001 | 2:45b351b4fc2a | 75 | int noreaded=0; |
bosko001 | 2:45b351b4fc2a | 76 | // if(gp_scom) delete gp_scom; |
bosko001 | 2:45b351b4fc2a | 77 | gp_scom = new UARTSerial(SerialTxPin,SerialRxPin,get_baud()); //38400 za KSS |
bosko001 | 2:45b351b4fc2a | 78 | |
bosko001 | 2:45b351b4fc2a | 79 | printf("Rx serial com initialised\n\r"); |
bosko001 | 2:45b351b4fc2a | 80 | |
bosko001 | 2:45b351b4fc2a | 81 | while( !(ThisThread::flags_get( ) & 1) ) |
bosko001 | 2:45b351b4fc2a | 82 | { |
bosko001 | 2:45b351b4fc2a | 83 | if( gp_scom->readable() ) |
bosko001 | 2:45b351b4fc2a | 84 | { |
bosko001 | 2:45b351b4fc2a | 85 | noreaded = gp_scom->read( (uint8_t *)(readbuff+totno), 1500); |
bosko001 | 2:45b351b4fc2a | 86 | totno += noreaded; |
bosko001 | 2:45b351b4fc2a | 87 | wait(0.01); |
bosko001 | 2:45b351b4fc2a | 88 | } |
bosko001 | 2:45b351b4fc2a | 89 | else if(totno) |
bosko001 | 2:45b351b4fc2a | 90 | { |
bosko001 | 2:45b351b4fc2a | 91 | printf("PRIJEM na rs232 com totno = %d \n\r", totno); |
bosko001 | 2:45b351b4fc2a | 92 | // if(readbuff[0] == 'q') {extern Thread udpBroadcast_thread; udpBroadcast_thread.terminate();} |
bosko001 | 2:45b351b4fc2a | 93 | |
bosko001 | 2:45b351b4fc2a | 94 | if(readbuff[0] == 'c'){ extern void udpBroadcast_fun( void );Thread *th = new Thread; th->start( udpBroadcast_fun);} |
bosko001 | 2:45b351b4fc2a | 95 | if(readbuff[0] == 'r'){ extern void inic_1(void); extern PFV pfv; pfv = inic_1; } |
bosko001 | 2:45b351b4fc2a | 96 | if(readbuff[0] == 'q'){ if(gp_udpSocket)gp_udpSocket->close();gp_udpSocket=NULL; } |
bosko001 | 2:45b351b4fc2a | 97 | sendUdp( ps_ear, (char *)readbuff, totno); |
bosko001 | 2:45b351b4fc2a | 98 | totno = 0; |
bosko001 | 2:45b351b4fc2a | 99 | } |
bosko001 | 2:45b351b4fc2a | 100 | } |
bosko001 | 2:45b351b4fc2a | 101 | if(gp_scom) {delete gp_scom; gp_scom = NULL;} |
bosko001 | 2:45b351b4fc2a | 102 | printf(" Ugasen serial com Thread\n\r"); |
bosko001 | 2:45b351b4fc2a | 103 | } |
bosko001 | 2:45b351b4fc2a | 104 | |
bosko001 | 2:45b351b4fc2a | 105 | |
bosko001 | 2:45b351b4fc2a | 106 | /******************* Broadcast thread **********************/ |
bosko001 | 2:45b351b4fc2a | 107 | Thread udpBroadcast_thread; |
bosko001 | 2:45b351b4fc2a | 108 | void udpBroadcast_fun( void ) |
bosko001 | 2:45b351b4fc2a | 109 | { |
bosko001 | 2:45b351b4fc2a | 110 | while(true) |
bosko001 | 2:45b351b4fc2a | 111 | { |
bosko001 | 2:45b351b4fc2a | 112 | sendUdp( ps_eab, (char *)"test broadcast", 14); |
bosko001 | 2:45b351b4fc2a | 113 | wait(5); |
bosko001 | 2:45b351b4fc2a | 114 | } |
bosko001 | 2:45b351b4fc2a | 115 | } |
bosko001 | 2:45b351b4fc2a | 116 | |
bosko001 | 2:45b351b4fc2a | 117 | /*************** Prijem na UDP socket i predaja na Serial com ***************/ |
bosko001 | 2:45b351b4fc2a | 118 | |
bosko001 | 2:45b351b4fc2a | 119 | void udpRx_fun( void ) |
bosko001 | 2:45b351b4fc2a | 120 | { |
bosko001 | 2:45b351b4fc2a | 121 | //int *p = (int*)pp; |
bosko001 | 2:45b351b4fc2a | 122 | nsapi_error_t err; |
bosko001 | 2:45b351b4fc2a | 123 | |
bosko001 | 2:45b351b4fc2a | 124 | char inbuff[1500]; |
bosko001 | 2:45b351b4fc2a | 125 | |
bosko001 | 2:45b351b4fc2a | 126 | // if(gp_udpSocket == NULL) |
bosko001 | 2:45b351b4fc2a | 127 | { |
bosko001 | 2:45b351b4fc2a | 128 | gp_udpSocket = new UDPSocket; |
bosko001 | 2:45b351b4fc2a | 129 | printf("___novi UDP socket %x\n\r", gp_udpSocket); |
bosko001 | 2:45b351b4fc2a | 130 | if( (err = gp_udpSocket->open( gp_eth )) != NSAPI_ERROR_OK ) |
bosko001 | 2:45b351b4fc2a | 131 | { printf(" greska otvaranja soketa %d \n\r", err); } |
bosko001 | 2:45b351b4fc2a | 132 | gp_udpSocket->bind(ps_ear->port); |
bosko001 | 2:45b351b4fc2a | 133 | gp_udpSocket->set_timeout(1000); |
bosko001 | 2:45b351b4fc2a | 134 | } |
bosko001 | 2:45b351b4fc2a | 135 | printf("UDPrx inicijalizovan \n\r"); |
bosko001 | 2:45b351b4fc2a | 136 | |
bosko001 | 2:45b351b4fc2a | 137 | |
bosko001 | 2:45b351b4fc2a | 138 | signed int n=0; |
bosko001 | 2:45b351b4fc2a | 139 | while( !(ThisThread::flags_get( ) & 1) ) |
bosko001 | 2:45b351b4fc2a | 140 | { |
bosko001 | 2:45b351b4fc2a | 141 | SocketAddress sockAddr; |
bosko001 | 2:45b351b4fc2a | 142 | if( (n>=0) && gp_udpSocket ) |
bosko001 | 2:45b351b4fc2a | 143 | { |
bosko001 | 2:45b351b4fc2a | 144 | int n = gp_udpSocket->recvfrom(&sockAddr, &inbuff, sizeof(inbuff)); |
bosko001 | 2:45b351b4fc2a | 145 | if(n>0) sendScom( gp_scom, inbuff, n);//gp_scom->write( (const uint8_t*) inbuff, n ); |
bosko001 | 2:45b351b4fc2a | 146 | else if(n==-3001) n = 0; |
bosko001 | 2:45b351b4fc2a | 147 | else break; |
bosko001 | 2:45b351b4fc2a | 148 | } |
bosko001 | 2:45b351b4fc2a | 149 | //else wait(0.1); |
bosko001 | 2:45b351b4fc2a | 150 | } |
bosko001 | 2:45b351b4fc2a | 151 | |
bosko001 | 2:45b351b4fc2a | 152 | if(gp_udpSocket) { delete gp_udpSocket; gp_udpSocket = NULL;} |
bosko001 | 2:45b351b4fc2a | 153 | printf("udpSocket obrisan\n\r"); |
bosko001 | 2:45b351b4fc2a | 154 | fflush(stdout); |
bosko001 | 2:45b351b4fc2a | 155 | } |
bosko001 | 2:45b351b4fc2a | 156 | |
bosko001 | 2:45b351b4fc2a | 157 | |
bosko001 | 2:45b351b4fc2a | 158 | |
bosko001 | 2:45b351b4fc2a | 159 | void inic_1(void) |
bosko001 | 2:45b351b4fc2a | 160 | { |
bosko001 | 2:45b351b4fc2a | 161 | |
bosko001 | 2:45b351b4fc2a | 162 | printf("\n\r -------- REINICIJALIZACIJA ------------- \n\r"); |
bosko001 | 2:45b351b4fc2a | 163 | |
bosko001 | 2:45b351b4fc2a | 164 | /* Gasenje UDP threada */ |
bosko001 | 2:45b351b4fc2a | 165 | if( gp_udpRxThread->get_state( ) && gp_udpRxThread->get_state( )<16 ) |
bosko001 | 2:45b351b4fc2a | 166 | gp_udpRxThread->flags_set( 1 ); |
bosko001 | 2:45b351b4fc2a | 167 | gp_udpRxThread->join( ); |
bosko001 | 2:45b351b4fc2a | 168 | if(gp_udpRxThread) {delete gp_udpRxThread; gp_udpRxThread = NULL;} |
bosko001 | 2:45b351b4fc2a | 169 | printf("udp thread zavrsio\n\r"); |
bosko001 | 2:45b351b4fc2a | 170 | |
bosko001 | 2:45b351b4fc2a | 171 | /* gasenje serial com thread-a */ |
bosko001 | 2:45b351b4fc2a | 172 | int st = gp_scomRxThread->get_state(); |
bosko001 | 2:45b351b4fc2a | 173 | if( st && st<16 )gp_scomRxThread->flags_set(1); |
bosko001 | 2:45b351b4fc2a | 174 | gp_scomRxThread->join(); |
bosko001 | 2:45b351b4fc2a | 175 | printf("ScomRx Thread upravo zavrsio\n\r"); |
bosko001 | 2:45b351b4fc2a | 176 | if(gp_scomRxThread) { delete gp_scomRxThread; gp_scomRxThread = NULL;} |
bosko001 | 2:45b351b4fc2a | 177 | |
bosko001 | 2:45b351b4fc2a | 178 | gp_eth->disconnect(); /* gasenje ethernta */ |
bosko001 | 2:45b351b4fc2a | 179 | |
bosko001 | 2:45b351b4fc2a | 180 | gp_eth = ethernet_setup( ); /*paljenje etherneta*/ |
bosko001 | 2:45b351b4fc2a | 181 | if(gp_eth == NULL) printf("ethernet greska\n\r"); |
bosko001 | 2:45b351b4fc2a | 182 | else printf("eth OK\n\r"); |
bosko001 | 2:45b351b4fc2a | 183 | |
bosko001 | 2:45b351b4fc2a | 184 | /* paljenje UDP thread-a */ |
bosko001 | 2:45b351b4fc2a | 185 | gp_udpRxThread = new Thread( udpRx_fun ); |
bosko001 | 2:45b351b4fc2a | 186 | |
bosko001 | 2:45b351b4fc2a | 187 | |
bosko001 | 2:45b351b4fc2a | 188 | /* paljenje serial com thread-a */ |
bosko001 | 2:45b351b4fc2a | 189 | gp_scomRxThread = new Thread(scomrx_fun); |
bosko001 | 2:45b351b4fc2a | 190 | |
bosko001 | 2:45b351b4fc2a | 191 | |
bosko001 | 2:45b351b4fc2a | 192 | // |
bosko001 | 2:45b351b4fc2a | 193 | fflush(stdout); |
bosko001 | 2:45b351b4fc2a | 194 | |
bosko001 | 2:45b351b4fc2a | 195 | } |
bosko001 | 2:45b351b4fc2a | 196 | |
bosko001 | 2:45b351b4fc2a | 197 | void init_EthToScom( void ) |
bosko001 | 2:45b351b4fc2a | 198 | { |
bosko001 | 2:45b351b4fc2a | 199 | gp_eth = ethernet_setup( ); |
bosko001 | 2:45b351b4fc2a | 200 | |
bosko001 | 2:45b351b4fc2a | 201 | if(gp_eth == NULL) printf("ethernet greska"); |
bosko001 | 2:45b351b4fc2a | 202 | else printf("eth OK\n\r"); |
bosko001 | 2:45b351b4fc2a | 203 | |
bosko001 | 2:45b351b4fc2a | 204 | |
bosko001 | 2:45b351b4fc2a | 205 | if(gp_scomRxThread) delete gp_scomRxThread; |
bosko001 | 2:45b351b4fc2a | 206 | gp_scomRxThread = new Thread(scomrx_fun); |
bosko001 | 2:45b351b4fc2a | 207 | |
bosko001 | 2:45b351b4fc2a | 208 | |
bosko001 | 2:45b351b4fc2a | 209 | /* |
bosko001 | 2:45b351b4fc2a | 210 | TCPSocket socket; |
bosko001 | 2:45b351b4fc2a | 211 | socket.open(ð); |
bosko001 | 2:45b351b4fc2a | 212 | socket.connect( |
bosko001 | 2:45b351b4fc2a | 213 | */ |
bosko001 | 2:45b351b4fc2a | 214 | |
bosko001 | 2:45b351b4fc2a | 215 | |
bosko001 | 2:45b351b4fc2a | 216 | gp_udpRxThread = new Thread( udpRx_fun ); |
bosko001 | 2:45b351b4fc2a | 217 | |
bosko001 | 2:45b351b4fc2a | 218 | } |