bosko lekovic
/
EthToCom_11
nova proba
Diff: main.cpp
- Revision:
- 1:e1bde4e82763
- Parent:
- 0:fe8cd064ea08
- Child:
- 2:45b351b4fc2a
--- a/main.cpp Thu Feb 06 21:05:30 2020 +0000 +++ b/main.cpp Tue Mar 17 12:08:22 2020 +0000 @@ -1,45 +1,95 @@ #include "mbed.h" #include "EthernetInterface.h" -#define WADDR -#ifdef WADDR -#define IP_ADDR "169.254.65.200" -#define NET_MASK "255.255.0.0" -#define GATW_ADDR "169.254.65.1" -#define IP_ADDR_REMOTE "169.254.65.199" -#else -#define IP_ADDR "192.168.1.10" -#define NET_MASK "255.255.255.0" -#define GATW_ADDR "192.168.1.1" -#define IP_ADDR_R "192.168.1.9" -#endif +struct S_EthernetAddress +{ + char ip[16]; + char mask[16]; + char gate[16]; + int port; +} s_EthAdd = {"192.168.1.20", "255.255.255.0", "192.168.1.1", 13000}; - +struct S_EthernetAddress_Remote +{ + char ip[16]; + int port; +} s_EthAddRemote = {"192.168.1.14", 13000 }, *ps_ear = &s_EthAddRemote, + s_EthAddBroadcast = {"0.0.0.0", 13000 }, *ps_eab = &s_EthAddBroadcast; EthernetInterface eth; + +struct S_net +{ + struct S_EthernetAddress *ps_ea; + EthernetInterface *p_eth; +} s_net = { &s_EthAdd, ð}; + nsapi_error_t err; +void ethernet_setup( struct S_net * ); +UDPSocket udpSocket; + +void sendUdp( struct S_EthernetAddress_Remote *ps_ear, char *buffer, int val ) +{ + printf(" UDPpaket ip=%s port=%d val=%d\n\r", ps_ear->ip, ps_ear->port, val); + udpSocket.sendto(ps_ear->ip, ps_ear->port, (const uint8_t*)buffer, val); +} + + +//UARTSerial scom(PTC17, PTC16);//(PTC4, PTC3);//(PTC17, PTC16); + +UARTSerial *pscom; + + +volatile uint8_t readbuff[1500]; +volatile int totno=0; +volatile int readflag = 0; +volatile int noreaded=0; + +extern UARTSerial *pscom; +Thread thread_scomrx; +void scomrx_fun( void ) +{ + while(true) + { + if( pscom->readable() ) + { + noreaded = pscom->read( (uint8_t *)(readbuff+totno), 1500); + totno += noreaded; + wait(0.01); + } + else if(totno) + { + printf("PRIJEM na rs232 com totno = %d \n\r", totno); + sendUdp( ps_ear, (char *)readbuff, totno); + totno = 0; + } + } +} + +Thread udpBroadcast_thread; +void udpBroadcast_fun( void ) +{ + while(true) + { + sendUdp( ps_eab, (char *)"test broadcast", 14); + wait(5); + } +} int main(void) { - // if( (err = eth.disconnect()) != NSAPI_ERROR_OK ) { printf(" greska diskonekcije %d \n\r", err); return 1; } - // ((NetworkInterface *)ð)->set_network("192.168.1.11","255.255.255.0","192.168.1.1"); + + printf("\n\r -------- POCETAK ------------- \n\r"); + + ethernet_setup( &s_net ); - if( (err = eth.set_network(IP_ADDR,NET_MASK,GATW_ADDR))!= NSAPI_ERROR_OK ) { printf(" greska setovanja mreze %d \n\r", err); return 1; } - - - if( (err = eth.connect()) != NSAPI_ERROR_OK ) { printf(" greska konekcije %d \n\r", err); return 1; } - - const char *ip = eth.get_ip_address() ; - - - printf( "MAC adresa: %s\n\r", eth.get_mac_address() ); - printf( "IP adresa: %s\n\r", ip); - printf( "net mask: %s\n\r", eth.get_netmask() ); - printf( "GW adresa: %s\n\r", eth.get_gateway() ); - + UARTSerial _scom(PTC17, PTC16, 38400); + pscom = &_scom; + + thread_scomrx.start( scomrx_fun ); /* TCPSocket socket; @@ -47,34 +97,50 @@ socket.connect( */ - UDPSocket udpSocket; if( (err = udpSocket.open( ð )) != NSAPI_ERROR_OK ) { printf(" greska otvaranja soketa %d \n\r", err); return 1; } - udpSocket.bind(2000); - - char buff[]="text poruke"; - char inbuff[100]; - printf("POCINJE\r\n"); - int i=0; + udpSocket.bind(ps_ear->port); + + int ret; + + char inbuff[1500]; + + + udpBroadcast_thread.start(udpBroadcast_fun); + while(1) { printf("cekam prijem \n\r"); SocketAddress sockAddr; + int n = udpSocket.recvfrom(&sockAddr, &inbuff, sizeof(inbuff)); printf(" primljeno = %d bajtova ",n); - for(i=0;i<n;i++) printf(" %c ", inbuff[i] ); - if(0 > udpSocket.sendto(sockAddr.get_ip_address(), sockAddr.get_port(), buff, sizeof(buff))) - { - printf("\n\rError sending data\n"); - return -1; - } - else printf("\n\rPredaja %s na adresu %s , port %d\r\n",buff,sockAddr.get_ip_address(), sockAddr.get_port()); - - wait(1); + pscom->write( (const uint8_t*) inbuff, n ); } } + +void ethernet_setup( struct S_net *ps_net ) +{ + // if( (err = eth.disconnect()) != NSAPI_ERROR_OK ) { printf(" greska diskonekcije %d \n\r", err); return 1; } + // ((NetworkInterface *)ð)->set_network("192.168.1.11","255.255.255.0","192.168.1.1"); + + if( (err = ps_net->p_eth->set_network(ps_net->ps_ea->ip, ps_net->ps_ea->mask, ps_net->ps_ea->gate))!= NSAPI_ERROR_OK ) { printf(" greska setovanja mreze %d \n\r", err); } + + + if( (err = ps_net->p_eth->connect()) != NSAPI_ERROR_OK ) { printf(" greska konekcije %d \n\r", err);} + + + + printf( "MAC adresa: %s\n\r", ps_net->p_eth->get_mac_address() ); + printf( "IP adresa: %s\n\r", ps_net->p_eth->get_ip_address() ); + printf( "net mask: %s\n\r", ps_net->p_eth->get_netmask() ); + printf( "GW adresa: %s\n\r", ps_net->p_eth->get_gateway() ); +} + + + // //enum nsapi_error { // NSAPI_ERROR_OK = 0, /*!< no error */