bosko lekovic
/
ETHtoCOM_10
nova verzija tcp+udp->serial com
main.cpp@1:e1bde4e82763, 2020-03-17 (annotated)
- Committer:
- bosko001
- Date:
- Tue Mar 17 12:08:22 2020 +0000
- Revision:
- 1:e1bde4e82763
- Parent:
- 0:fe8cd064ea08
- Child:
- 2:45b351b4fc2a
UDP na rs232 serial (uart3 FRDMk64 tx=PTC17 rx=PTC16), baud=38.400
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bosko001 | 0:fe8cd064ea08 | 1 | #include "mbed.h" |
bosko001 | 0:fe8cd064ea08 | 2 | #include "EthernetInterface.h" |
bosko001 | 0:fe8cd064ea08 | 3 | |
bosko001 | 0:fe8cd064ea08 | 4 | |
bosko001 | 1:e1bde4e82763 | 5 | struct S_EthernetAddress |
bosko001 | 1:e1bde4e82763 | 6 | { |
bosko001 | 1:e1bde4e82763 | 7 | char ip[16]; |
bosko001 | 1:e1bde4e82763 | 8 | char mask[16]; |
bosko001 | 1:e1bde4e82763 | 9 | char gate[16]; |
bosko001 | 1:e1bde4e82763 | 10 | int port; |
bosko001 | 1:e1bde4e82763 | 11 | } s_EthAdd = {"192.168.1.20", "255.255.255.0", "192.168.1.1", 13000}; |
bosko001 | 0:fe8cd064ea08 | 12 | |
bosko001 | 1:e1bde4e82763 | 13 | struct S_EthernetAddress_Remote |
bosko001 | 1:e1bde4e82763 | 14 | { |
bosko001 | 1:e1bde4e82763 | 15 | char ip[16]; |
bosko001 | 1:e1bde4e82763 | 16 | int port; |
bosko001 | 1:e1bde4e82763 | 17 | } s_EthAddRemote = {"192.168.1.14", 13000 }, *ps_ear = &s_EthAddRemote, |
bosko001 | 1:e1bde4e82763 | 18 | s_EthAddBroadcast = {"0.0.0.0", 13000 }, *ps_eab = &s_EthAddBroadcast; |
bosko001 | 0:fe8cd064ea08 | 19 | |
bosko001 | 0:fe8cd064ea08 | 20 | |
bosko001 | 0:fe8cd064ea08 | 21 | EthernetInterface eth; |
bosko001 | 1:e1bde4e82763 | 22 | |
bosko001 | 1:e1bde4e82763 | 23 | struct S_net |
bosko001 | 1:e1bde4e82763 | 24 | { |
bosko001 | 1:e1bde4e82763 | 25 | struct S_EthernetAddress *ps_ea; |
bosko001 | 1:e1bde4e82763 | 26 | EthernetInterface *p_eth; |
bosko001 | 1:e1bde4e82763 | 27 | } s_net = { &s_EthAdd, ð}; |
bosko001 | 1:e1bde4e82763 | 28 | |
bosko001 | 0:fe8cd064ea08 | 29 | nsapi_error_t err; |
bosko001 | 1:e1bde4e82763 | 30 | void ethernet_setup( struct S_net * ); |
bosko001 | 0:fe8cd064ea08 | 31 | |
bosko001 | 1:e1bde4e82763 | 32 | UDPSocket udpSocket; |
bosko001 | 1:e1bde4e82763 | 33 | |
bosko001 | 1:e1bde4e82763 | 34 | void sendUdp( struct S_EthernetAddress_Remote *ps_ear, char *buffer, int val ) |
bosko001 | 1:e1bde4e82763 | 35 | { |
bosko001 | 1:e1bde4e82763 | 36 | printf(" UDPpaket ip=%s port=%d val=%d\n\r", ps_ear->ip, ps_ear->port, val); |
bosko001 | 1:e1bde4e82763 | 37 | udpSocket.sendto(ps_ear->ip, ps_ear->port, (const uint8_t*)buffer, val); |
bosko001 | 1:e1bde4e82763 | 38 | } |
bosko001 | 1:e1bde4e82763 | 39 | |
bosko001 | 1:e1bde4e82763 | 40 | |
bosko001 | 1:e1bde4e82763 | 41 | //UARTSerial scom(PTC17, PTC16);//(PTC4, PTC3);//(PTC17, PTC16); |
bosko001 | 1:e1bde4e82763 | 42 | |
bosko001 | 1:e1bde4e82763 | 43 | UARTSerial *pscom; |
bosko001 | 1:e1bde4e82763 | 44 | |
bosko001 | 1:e1bde4e82763 | 45 | |
bosko001 | 1:e1bde4e82763 | 46 | volatile uint8_t readbuff[1500]; |
bosko001 | 1:e1bde4e82763 | 47 | volatile int totno=0; |
bosko001 | 1:e1bde4e82763 | 48 | volatile int readflag = 0; |
bosko001 | 1:e1bde4e82763 | 49 | volatile int noreaded=0; |
bosko001 | 1:e1bde4e82763 | 50 | |
bosko001 | 1:e1bde4e82763 | 51 | extern UARTSerial *pscom; |
bosko001 | 1:e1bde4e82763 | 52 | Thread thread_scomrx; |
bosko001 | 1:e1bde4e82763 | 53 | void scomrx_fun( void ) |
bosko001 | 1:e1bde4e82763 | 54 | { |
bosko001 | 1:e1bde4e82763 | 55 | while(true) |
bosko001 | 1:e1bde4e82763 | 56 | { |
bosko001 | 1:e1bde4e82763 | 57 | if( pscom->readable() ) |
bosko001 | 1:e1bde4e82763 | 58 | { |
bosko001 | 1:e1bde4e82763 | 59 | noreaded = pscom->read( (uint8_t *)(readbuff+totno), 1500); |
bosko001 | 1:e1bde4e82763 | 60 | totno += noreaded; |
bosko001 | 1:e1bde4e82763 | 61 | wait(0.01); |
bosko001 | 1:e1bde4e82763 | 62 | } |
bosko001 | 1:e1bde4e82763 | 63 | else if(totno) |
bosko001 | 1:e1bde4e82763 | 64 | { |
bosko001 | 1:e1bde4e82763 | 65 | printf("PRIJEM na rs232 com totno = %d \n\r", totno); |
bosko001 | 1:e1bde4e82763 | 66 | sendUdp( ps_ear, (char *)readbuff, totno); |
bosko001 | 1:e1bde4e82763 | 67 | totno = 0; |
bosko001 | 1:e1bde4e82763 | 68 | } |
bosko001 | 1:e1bde4e82763 | 69 | } |
bosko001 | 1:e1bde4e82763 | 70 | } |
bosko001 | 1:e1bde4e82763 | 71 | |
bosko001 | 1:e1bde4e82763 | 72 | Thread udpBroadcast_thread; |
bosko001 | 1:e1bde4e82763 | 73 | void udpBroadcast_fun( void ) |
bosko001 | 1:e1bde4e82763 | 74 | { |
bosko001 | 1:e1bde4e82763 | 75 | while(true) |
bosko001 | 1:e1bde4e82763 | 76 | { |
bosko001 | 1:e1bde4e82763 | 77 | sendUdp( ps_eab, (char *)"test broadcast", 14); |
bosko001 | 1:e1bde4e82763 | 78 | wait(5); |
bosko001 | 1:e1bde4e82763 | 79 | } |
bosko001 | 1:e1bde4e82763 | 80 | } |
bosko001 | 0:fe8cd064ea08 | 81 | |
bosko001 | 0:fe8cd064ea08 | 82 | int main(void) |
bosko001 | 0:fe8cd064ea08 | 83 | { |
bosko001 | 1:e1bde4e82763 | 84 | |
bosko001 | 1:e1bde4e82763 | 85 | printf("\n\r -------- POCETAK ------------- \n\r"); |
bosko001 | 1:e1bde4e82763 | 86 | |
bosko001 | 1:e1bde4e82763 | 87 | ethernet_setup( &s_net ); |
bosko001 | 0:fe8cd064ea08 | 88 | |
bosko001 | 1:e1bde4e82763 | 89 | UARTSerial _scom(PTC17, PTC16, 38400); |
bosko001 | 1:e1bde4e82763 | 90 | pscom = &_scom; |
bosko001 | 1:e1bde4e82763 | 91 | |
bosko001 | 1:e1bde4e82763 | 92 | thread_scomrx.start( scomrx_fun ); |
bosko001 | 0:fe8cd064ea08 | 93 | |
bosko001 | 0:fe8cd064ea08 | 94 | /* |
bosko001 | 0:fe8cd064ea08 | 95 | TCPSocket socket; |
bosko001 | 0:fe8cd064ea08 | 96 | socket.open(ð); |
bosko001 | 0:fe8cd064ea08 | 97 | socket.connect( |
bosko001 | 0:fe8cd064ea08 | 98 | */ |
bosko001 | 0:fe8cd064ea08 | 99 | |
bosko001 | 0:fe8cd064ea08 | 100 | if( (err = udpSocket.open( ð )) != NSAPI_ERROR_OK ) { printf(" greska otvaranja soketa %d \n\r", err); return 1; } |
bosko001 | 1:e1bde4e82763 | 101 | udpSocket.bind(ps_ear->port); |
bosko001 | 1:e1bde4e82763 | 102 | |
bosko001 | 1:e1bde4e82763 | 103 | int ret; |
bosko001 | 1:e1bde4e82763 | 104 | |
bosko001 | 1:e1bde4e82763 | 105 | char inbuff[1500]; |
bosko001 | 1:e1bde4e82763 | 106 | |
bosko001 | 1:e1bde4e82763 | 107 | |
bosko001 | 1:e1bde4e82763 | 108 | udpBroadcast_thread.start(udpBroadcast_fun); |
bosko001 | 1:e1bde4e82763 | 109 | |
bosko001 | 0:fe8cd064ea08 | 110 | while(1) |
bosko001 | 0:fe8cd064ea08 | 111 | { |
bosko001 | 0:fe8cd064ea08 | 112 | printf("cekam prijem \n\r"); |
bosko001 | 0:fe8cd064ea08 | 113 | |
bosko001 | 0:fe8cd064ea08 | 114 | SocketAddress sockAddr; |
bosko001 | 1:e1bde4e82763 | 115 | |
bosko001 | 0:fe8cd064ea08 | 116 | int n = udpSocket.recvfrom(&sockAddr, &inbuff, sizeof(inbuff)); |
bosko001 | 0:fe8cd064ea08 | 117 | printf(" primljeno = %d bajtova ",n); |
bosko001 | 0:fe8cd064ea08 | 118 | |
bosko001 | 1:e1bde4e82763 | 119 | pscom->write( (const uint8_t*) inbuff, n ); |
bosko001 | 0:fe8cd064ea08 | 120 | } |
bosko001 | 0:fe8cd064ea08 | 121 | } |
bosko001 | 0:fe8cd064ea08 | 122 | |
bosko001 | 1:e1bde4e82763 | 123 | |
bosko001 | 1:e1bde4e82763 | 124 | void ethernet_setup( struct S_net *ps_net ) |
bosko001 | 1:e1bde4e82763 | 125 | { |
bosko001 | 1:e1bde4e82763 | 126 | // if( (err = eth.disconnect()) != NSAPI_ERROR_OK ) { printf(" greska diskonekcije %d \n\r", err); return 1; } |
bosko001 | 1:e1bde4e82763 | 127 | // ((NetworkInterface *)ð)->set_network("192.168.1.11","255.255.255.0","192.168.1.1"); |
bosko001 | 1:e1bde4e82763 | 128 | |
bosko001 | 1:e1bde4e82763 | 129 | 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); } |
bosko001 | 1:e1bde4e82763 | 130 | |
bosko001 | 1:e1bde4e82763 | 131 | |
bosko001 | 1:e1bde4e82763 | 132 | if( (err = ps_net->p_eth->connect()) != NSAPI_ERROR_OK ) { printf(" greska konekcije %d \n\r", err);} |
bosko001 | 1:e1bde4e82763 | 133 | |
bosko001 | 1:e1bde4e82763 | 134 | |
bosko001 | 1:e1bde4e82763 | 135 | |
bosko001 | 1:e1bde4e82763 | 136 | printf( "MAC adresa: %s\n\r", ps_net->p_eth->get_mac_address() ); |
bosko001 | 1:e1bde4e82763 | 137 | printf( "IP adresa: %s\n\r", ps_net->p_eth->get_ip_address() ); |
bosko001 | 1:e1bde4e82763 | 138 | printf( "net mask: %s\n\r", ps_net->p_eth->get_netmask() ); |
bosko001 | 1:e1bde4e82763 | 139 | printf( "GW adresa: %s\n\r", ps_net->p_eth->get_gateway() ); |
bosko001 | 1:e1bde4e82763 | 140 | } |
bosko001 | 1:e1bde4e82763 | 141 | |
bosko001 | 1:e1bde4e82763 | 142 | |
bosko001 | 1:e1bde4e82763 | 143 | |
bosko001 | 0:fe8cd064ea08 | 144 | // |
bosko001 | 0:fe8cd064ea08 | 145 | //enum nsapi_error { |
bosko001 | 0:fe8cd064ea08 | 146 | // NSAPI_ERROR_OK = 0, /*!< no error */ |
bosko001 | 0:fe8cd064ea08 | 147 | // NSAPI_ERROR_WOULD_BLOCK = -3001, /*!< no data is not available but call is non-blocking */ |
bosko001 | 0:fe8cd064ea08 | 148 | // NSAPI_ERROR_UNSUPPORTED = -3002, /*!< unsupported functionality */ |
bosko001 | 0:fe8cd064ea08 | 149 | // NSAPI_ERROR_PARAMETER = -3003, /*!< invalid configuration */ |
bosko001 | 0:fe8cd064ea08 | 150 | // NSAPI_ERROR_NO_CONNECTION = -3004, /*!< not connected to a network */ |
bosko001 | 0:fe8cd064ea08 | 151 | // NSAPI_ERROR_NO_SOCKET = -3005, /*!< socket not available for use */ |
bosko001 | 0:fe8cd064ea08 | 152 | // NSAPI_ERROR_NO_ADDRESS = -3006, /*!< IP address is not known */ |
bosko001 | 0:fe8cd064ea08 | 153 | // NSAPI_ERROR_NO_MEMORY = -3007, /*!< memory resource not available */ |
bosko001 | 0:fe8cd064ea08 | 154 | // NSAPI_ERROR_NO_SSID = -3008, /*!< ssid not found */ |
bosko001 | 0:fe8cd064ea08 | 155 | // NSAPI_ERROR_DNS_FAILURE = -3009, /*!< DNS failed to complete successfully */ |
bosko001 | 0:fe8cd064ea08 | 156 | // NSAPI_ERROR_DHCP_FAILURE = -3010, /*!< DHCP failed to complete successfully */ |
bosko001 | 0:fe8cd064ea08 | 157 | // NSAPI_ERROR_AUTH_FAILURE = -3011, /*!< connection to access point failed */ |
bosko001 | 0:fe8cd064ea08 | 158 | // NSAPI_ERROR_DEVICE_ERROR = -3012, /*!< failure interfacing with the network processor */ |
bosko001 | 0:fe8cd064ea08 | 159 | // NSAPI_ERROR_IN_PROGRESS = -3013, /*!< operation (eg connect) in progress */ |
bosko001 | 0:fe8cd064ea08 | 160 | // NSAPI_ERROR_ALREADY = -3014, /*!< operation (eg connect) already in progress */ |
bosko001 | 0:fe8cd064ea08 | 161 | // NSAPI_ERROR_IS_CONNECTED = -3015, /*!< socket is already connected */ |
bosko001 | 0:fe8cd064ea08 | 162 | // NSAPI_ERROR_CONNECTION_LOST = -3016, /*!< connection lost */ |
bosko001 | 0:fe8cd064ea08 | 163 | // NSAPI_ERROR_CONNECTION_TIMEOUT = -3017, /*!< connection timed out */ |
bosko001 | 0:fe8cd064ea08 | 164 | //}; |