Vorlage für Projekt
Dependencies: EthernetInterface mbed-rtos
Fork of Telemetrie_eth_h2m by
Telemetry.h@8:2cd23c180dfe, 2015-12-04 (annotated)
- Committer:
- HMFK03LST1
- Date:
- Fri Dec 04 15:54:30 2015 +0000
- Revision:
- 8:2cd23c180dfe
- Parent:
- 7:a269ac8c3259
- Child:
- 9:4e882da5a890
new demo Program
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
HMFK03LST1 | 0:c0179f2ad295 | 1 | |
HMFK03LST1 | 0:c0179f2ad295 | 2 | #ifndef TELEMETRY |
HMFK03LST1 | 0:c0179f2ad295 | 3 | #define TELEMETRY |
HMFK03LST1 | 0:c0179f2ad295 | 4 | |
HMFK03LST1 | 1:2df3958877f2 | 5 | //#define DEBUG |
HMFK03LST1 | 0:c0179f2ad295 | 6 | |
HMFK03LST1 | 0:c0179f2ad295 | 7 | #include "mbed.h" |
HMFK03LST1 | 0:c0179f2ad295 | 8 | #include "EthernetInterface.h" |
HMFK03LST1 | 3:94a735c744ff | 9 | |
HMFK03LST1 | 3:94a735c744ff | 10 | /** Ethernet Interface for send/receive Datastructs over udp |
HMFK03LST1 | 3:94a735c744ff | 11 | * |
HMFK03LST1 | 3:94a735c744ff | 12 | * By Sebastian Donner |
HMFK03LST1 | 3:94a735c744ff | 13 | * |
HMFK03LST1 | 3:94a735c744ff | 14 | * Permission is hereby granted, free of charge, to any person |
HMFK03LST1 | 3:94a735c744ff | 15 | * obtaining a copy of this software and associated documentation |
HMFK03LST1 | 3:94a735c744ff | 16 | * files (the "Software"), to deal in the Software without |
HMFK03LST1 | 3:94a735c744ff | 17 | * restriction, including without limitation the rights to use, |
HMFK03LST1 | 3:94a735c744ff | 18 | * copy, modify, merge, publish, distribute, sublicense, and/or sell |
HMFK03LST1 | 3:94a735c744ff | 19 | * copies of the Software, and to permit persons to whom the |
HMFK03LST1 | 3:94a735c744ff | 20 | * Software is furnished to do so, subject to the following |
HMFK03LST1 | 3:94a735c744ff | 21 | * conditions: |
HMFK03LST1 | 3:94a735c744ff | 22 | * |
HMFK03LST1 | 3:94a735c744ff | 23 | * The above copyright notice and this permission notice shall be |
HMFK03LST1 | 3:94a735c744ff | 24 | * included in all copies or substantial portions of the Software. |
HMFK03LST1 | 3:94a735c744ff | 25 | * |
HMFK03LST1 | 3:94a735c744ff | 26 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
HMFK03LST1 | 3:94a735c744ff | 27 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
HMFK03LST1 | 3:94a735c744ff | 28 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
HMFK03LST1 | 3:94a735c744ff | 29 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
HMFK03LST1 | 3:94a735c744ff | 30 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
HMFK03LST1 | 3:94a735c744ff | 31 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
HMFK03LST1 | 3:94a735c744ff | 32 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
HMFK03LST1 | 3:94a735c744ff | 33 | * OTHER DEALINGS IN THE SOFTWARE. |
HMFK03LST1 | 3:94a735c744ff | 34 | * |
HMFK03LST1 | 3:94a735c744ff | 35 | * @code |
HMFK03LST1 | 6:abadad863420 | 36 | * #include "mbed.h" |
HMFK03LST1 | 6:abadad863420 | 37 | * #include "Telemetry.h" |
HMFK03LST1 | 6:abadad863420 | 38 | * |
HMFK03LST1 | 8:2cd23c180dfe | 39 | * Telemetry Network(p21,p22); |
HMFK03LST1 | 8:2cd23c180dfe | 40 | |
HMFK03LST1 | 8:2cd23c180dfe | 41 | * Serial debug(USBTX, USBRX); |
HMFK03LST1 | 8:2cd23c180dfe | 42 | |
HMFK03LST1 | 8:2cd23c180dfe | 43 | |
HMFK03LST1 | 8:2cd23c180dfe | 44 | * uint8_t buffer[255]; |
HMFK03LST1 | 6:abadad863420 | 45 | * int SELF_PORT = 8889; |
HMFK03LST1 | 8:2cd23c180dfe | 46 | |
HMFK03LST1 | 8:2cd23c180dfe | 47 | * typedef struct struct_test |
HMFK03LST1 | 8:2cd23c180dfe | 48 | * { |
HMFK03LST1 | 8:2cd23c180dfe | 49 | * struct_test() |
HMFK03LST1 | 8:2cd23c180dfe | 50 | * :length(9), ID(0) |
HMFK03LST1 | 8:2cd23c180dfe | 51 | * {} |
HMFK03LST1 | 8:2cd23c180dfe | 52 | * uint8_t length ;//1 |
HMFK03LST1 | 8:2cd23c180dfe | 53 | * uint8_t ID ;//2 |
HMFK03LST1 | 8:2cd23c180dfe | 54 | * uint8_t flag_bit0 : 1 ;//3.0 |
HMFK03LST1 | 8:2cd23c180dfe | 55 | * uint8_t flag_bit1 : 1 ;//3.1 |
HMFK03LST1 | 8:2cd23c180dfe | 56 | * uint8_t flagbyte_2 ;//4 |
HMFK03LST1 | 8:2cd23c180dfe | 57 | * uint16_t word ;//6 |
HMFK03LST1 | 8:2cd23c180dfe | 58 | * int16_t counter ;//8 |
HMFK03LST1 | 8:2cd23c180dfe | 59 | * uint8_t cs ;//9 |
HMFK03LST1 | 8:2cd23c180dfe | 60 | * } Tstruct_test; |
HMFK03LST1 | 6:abadad863420 | 61 | * |
HMFK03LST1 | 6:abadad863420 | 62 | * |
HMFK03LST1 | 6:abadad863420 | 63 | * struct_test test; |
HMFK03LST1 | 6:abadad863420 | 64 | * |
HMFK03LST1 | 8:2cd23c180dfe | 65 | * uint8_t* struct_id[5]; |
HMFK03LST1 | 8:2cd23c180dfe | 66 | * |
HMFK03LST1 | 8:2cd23c180dfe | 67 | * main() |
HMFK03LST1 | 3:94a735c744ff | 68 | * { |
HMFK03LST1 | 8:2cd23c180dfe | 69 | * debug.baud(115200); |
HMFK03LST1 | 6:abadad863420 | 70 | * Network.InitEthernetConnection(); |
HMFK03LST1 | 8:2cd23c180dfe | 71 | * Network.ConnectSocket_UDP_Client(); |
HMFK03LST1 | 8:2cd23c180dfe | 72 | * Network.ConnectSocket_UDP_Server(SELF_PORT); |
HMFK03LST1 | 8:2cd23c180dfe | 73 | * |
HMFK03LST1 | 8:2cd23c180dfe | 74 | * struct_id[0]=&test.length; |
HMFK03LST1 | 6:abadad863420 | 75 | * |
HMFK03LST1 | 8:2cd23c180dfe | 76 | * debug.printf("%s",Network.ip_self); |
HMFK03LST1 | 8:2cd23c180dfe | 77 | * while(1) |
HMFK03LST1 | 8:2cd23c180dfe | 78 | *{ |
HMFK03LST1 | 8:2cd23c180dfe | 79 | * Network.Rec_Data_UDP_Server((char*)buffer, 255); |
HMFK03LST1 | 8:2cd23c180dfe | 80 | * if (Network.Rec_Struct(buffer)) // receive and CS check |
HMFK03LST1 | 8:2cd23c180dfe | 81 | * { |
HMFK03LST1 | 8:2cd23c180dfe | 82 | * uint8_t* b_pointer; |
HMFK03LST1 | 8:2cd23c180dfe | 83 | * |
HMFK03LST1 | 8:2cd23c180dfe | 84 | * b_pointer = struct_id[buffer[1]]; // Pointer to received Struct |
HMFK03LST1 | 6:abadad863420 | 85 | * |
HMFK03LST1 | 8:2cd23c180dfe | 86 | * for(int i = 0; i < (buffer[0]); i++) // Copy buffer to Struct |
HMFK03LST1 | 8:2cd23c180dfe | 87 | * { |
HMFK03LST1 | 8:2cd23c180dfe | 88 | * *b_pointer = buffer[i]; |
HMFK03LST1 | 8:2cd23c180dfe | 89 | * b_pointer++; |
HMFK03LST1 | 8:2cd23c180dfe | 90 | * } |
HMFK03LST1 | 8:2cd23c180dfe | 91 | * |
HMFK03LST1 | 8:2cd23c180dfe | 92 | * b_pointer = struct_id[buffer[1]]; // Pointer to received Struct |
HMFK03LST1 | 8:2cd23c180dfe | 93 | * |
HMFK03LST1 | 8:2cd23c180dfe | 94 | * Network.Send_Struct_UDP_Client(Network.input_Server, b_pointer); // Send received Struct back to sender |
HMFK03LST1 | 8:2cd23c180dfe | 95 | * } |
HMFK03LST1 | 8:2cd23c180dfe | 96 | * |
HMFK03LST1 | 8:2cd23c180dfe | 97 | * wait(1); |
HMFK03LST1 | 8:2cd23c180dfe | 98 | * } |
HMFK03LST1 | 8:2cd23c180dfe | 99 | *} |
HMFK03LST1 | 3:94a735c744ff | 100 | * @endcode |
HMFK03LST1 | 6:abadad863420 | 101 | */ |
HMFK03LST1 | 3:94a735c744ff | 102 | |
HMFK03LST1 | 3:94a735c744ff | 103 | |
HMFK03LST1 | 0:c0179f2ad295 | 104 | |
HMFK03LST1 | 0:c0179f2ad295 | 105 | class Telemetry |
HMFK03LST1 | 0:c0179f2ad295 | 106 | { |
HMFK03LST1 | 0:c0179f2ad295 | 107 | public: |
HMFK03LST1 | 3:94a735c744ff | 108 | //! Create Ethernet Interface |
HMFK03LST1 | 7:a269ac8c3259 | 109 | Telemetry(PinName orange, PinName green); |
HMFK03LST1 | 7:a269ac8c3259 | 110 | |
HMFK03LST1 | 7:a269ac8c3259 | 111 | DigitalOut orange; |
HMFK03LST1 | 7:a269ac8c3259 | 112 | DigitalOut green; |
HMFK03LST1 | 7:a269ac8c3259 | 113 | |
HMFK03LST1 | 3:94a735c744ff | 114 | //! IP Address of mbed Interface |
HMFK03LST1 | 2:e19b937a29c1 | 115 | char* ip_self; |
HMFK03LST1 | 3:94a735c744ff | 116 | |
HMFK03LST1 | 3:94a735c744ff | 117 | //! IP Addess and Port of databank Server |
HMFK03LST1 | 2:e19b937a29c1 | 118 | Endpoint db_server; |
HMFK03LST1 | 3:94a735c744ff | 119 | |
HMFK03LST1 | 5:a96e4e59c710 | 120 | //! IP Addess and Port of control Server |
HMFK03LST1 | 2:e19b937a29c1 | 121 | Endpoint controller; |
HMFK03LST1 | 3:94a735c744ff | 122 | |
HMFK03LST1 | 7:a269ac8c3259 | 123 | //! IP Addess and Port of MSG on Server Port |
HMFK03LST1 | 7:a269ac8c3259 | 124 | Endpoint input_Server; |
HMFK03LST1 | 7:a269ac8c3259 | 125 | |
HMFK03LST1 | 7:a269ac8c3259 | 126 | //! IP Addess and Port of MSG on Client Port |
HMFK03LST1 | 7:a269ac8c3259 | 127 | Endpoint input_Client; |
HMFK03LST1 | 3:94a735c744ff | 128 | |
HMFK03LST1 | 2:e19b937a29c1 | 129 | |
HMFK03LST1 | 0:c0179f2ad295 | 130 | #ifdef DEBUG |
HMFK03LST1 | 0:c0179f2ad295 | 131 | void InitUSBSerialConnection(); |
HMFK03LST1 | 0:c0179f2ad295 | 132 | #endif |
HMFK03LST1 | 3:94a735c744ff | 133 | |
HMFK03LST1 | 3:94a735c744ff | 134 | //! init with DHCP |
HMFK03LST1 | 0:c0179f2ad295 | 135 | bool InitEthernetConnection(); |
HMFK03LST1 | 3:94a735c744ff | 136 | |
HMFK03LST1 | 3:94a735c744ff | 137 | //! init with Static IP |
HMFK03LST1 | 0:c0179f2ad295 | 138 | bool InitEthernetConnection(const char* IPAdress, const char* SubNetMask, const char* GateWay); |
HMFK03LST1 | 3:94a735c744ff | 139 | |
HMFK03LST1 | 3:94a735c744ff | 140 | |
HMFK03LST1 | 3:94a735c744ff | 141 | //! Close Connection |
HMFK03LST1 | 0:c0179f2ad295 | 142 | void CloseEthernetConnection(); |
HMFK03LST1 | 3:94a735c744ff | 143 | |
HMFK03LST1 | 3:94a735c744ff | 144 | |
HMFK03LST1 | 3:94a735c744ff | 145 | //! Connect Port TCP |
HMFK03LST1 | 7:a269ac8c3259 | 146 | void ConnectSocket_TCP(Endpoint Host); |
HMFK03LST1 | 3:94a735c744ff | 147 | |
HMFK03LST1 | 7:a269ac8c3259 | 148 | //! Connect Port for UDP Client |
HMFK03LST1 | 7:a269ac8c3259 | 149 | void ConnectSocket_UDP_Client(); |
HMFK03LST1 | 3:94a735c744ff | 150 | |
HMFK03LST1 | 7:a269ac8c3259 | 151 | //! Connect Port for UDP Server |
HMFK03LST1 | 7:a269ac8c3259 | 152 | void ConnectSocket_UDP_Server(int Port); |
HMFK03LST1 | 3:94a735c744ff | 153 | |
HMFK03LST1 | 3:94a735c744ff | 154 | |
HMFK03LST1 | 3:94a735c744ff | 155 | //! Close Port TCP |
HMFK03LST1 | 7:a269ac8c3259 | 156 | void CloseSocket_TCP(); |
HMFK03LST1 | 0:c0179f2ad295 | 157 | |
HMFK03LST1 | 7:a269ac8c3259 | 158 | //! Close Port UDP Client |
HMFK03LST1 | 7:a269ac8c3259 | 159 | void CloseSocket_UDP_Client(); |
HMFK03LST1 | 3:94a735c744ff | 160 | |
HMFK03LST1 | 3:94a735c744ff | 161 | //! Close Port UDP send |
HMFK03LST1 | 7:a269ac8c3259 | 162 | void CloseSocket_UDP_Server(); |
HMFK03LST1 | 3:94a735c744ff | 163 | |
HMFK03LST1 | 3:94a735c744ff | 164 | |
HMFK03LST1 | 3:94a735c744ff | 165 | //! Struct Check Sum calc |
HMFK03LST1 | 6:abadad863420 | 166 | uint8_t do_cs(uint8_t *buffer); |
HMFK03LST1 | 7:a269ac8c3259 | 167 | |
HMFK03LST1 | 3:94a735c744ff | 168 | |
HMFK03LST1 | 7:a269ac8c3259 | 169 | //! Read UDP Packet on Client Port |
HMFK03LST1 | 7:a269ac8c3259 | 170 | int Rec_Data_UDP_Client(char *buffer, int size); |
HMFK03LST1 | 7:a269ac8c3259 | 171 | |
HMFK03LST1 | 7:a269ac8c3259 | 172 | //! Read UDP Packet on Server Port |
HMFK03LST1 | 7:a269ac8c3259 | 173 | int Rec_Data_UDP_Server(char *buffer, int size); |
HMFK03LST1 | 2:e19b937a29c1 | 174 | |
HMFK03LST1 | 7:a269ac8c3259 | 175 | //! Check Packet of containing Struct |
HMFK03LST1 | 7:a269ac8c3259 | 176 | bool Rec_Struct(uint8_t *buffer); |
HMFK03LST1 | 3:94a735c744ff | 177 | |
HMFK03LST1 | 3:94a735c744ff | 178 | //! Read TCP Packet |
HMFK03LST1 | 3:94a735c744ff | 179 | int Rec_Data_TCP(char *buffer, int size); |
HMFK03LST1 | 3:94a735c744ff | 180 | |
HMFK03LST1 | 3:94a735c744ff | 181 | |
HMFK03LST1 | 7:a269ac8c3259 | 182 | //! Send UDP Packet as Client |
HMFK03LST1 | 7:a269ac8c3259 | 183 | void Send_Data_UDP_Client(Endpoint Server, char* Daten, int size); |
HMFK03LST1 | 7:a269ac8c3259 | 184 | |
HMFK03LST1 | 7:a269ac8c3259 | 185 | //! Send UDP Packet as Server |
HMFK03LST1 | 7:a269ac8c3259 | 186 | void Send_Data_UDP_Server(Endpoint Client, char* Daten, int size); |
HMFK03LST1 | 7:a269ac8c3259 | 187 | |
HMFK03LST1 | 7:a269ac8c3259 | 188 | //! Send Struct as UDP Client |
HMFK03LST1 | 7:a269ac8c3259 | 189 | void Send_Struct_UDP_Client(Endpoint Server, uint8_t* Daten); |
HMFK03LST1 | 3:94a735c744ff | 190 | |
HMFK03LST1 | 7:a269ac8c3259 | 191 | //! Send Struct as UDP Server |
HMFK03LST1 | 7:a269ac8c3259 | 192 | void Send_Struct_UDP_Server(Endpoint Client, uint8_t* Daten); |
HMFK03LST1 | 7:a269ac8c3259 | 193 | |
HMFK03LST1 | 7:a269ac8c3259 | 194 | |
HMFK03LST1 | 3:94a735c744ff | 195 | //! Send TCP Packet |
HMFK03LST1 | 3:94a735c744ff | 196 | void Send_Data_TCP(char* Host, char* Daten); |
HMFK03LST1 | 2:e19b937a29c1 | 197 | |
HMFK03LST1 | 0:c0179f2ad295 | 198 | |
HMFK03LST1 | 3:94a735c744ff | 199 | private: |
HMFK03LST1 | 3:94a735c744ff | 200 | bool InitSucceed; |
HMFK03LST1 | 2:e19b937a29c1 | 201 | |
HMFK03LST1 | 0:c0179f2ad295 | 202 | }; |
HMFK03LST1 | 0:c0179f2ad295 | 203 | |
HMFK03LST1 | 0:c0179f2ad295 | 204 | #endif |