RPC Over UDP Command format is same as serial RPC (not HTTP RPC). Please find attached Test Program(processing & ActionScript3) at end of this file.
Dependencies: EthernetNetIf mbed
main.cpp@1:918ffa99c345, 2010-11-16 (annotated)
- Committer:
- xshige
- Date:
- Tue Nov 16 14:11:31 2010 +0000
- Revision:
- 1:918ffa99c345
- Parent:
- 0:797742779818
2010/11/16 Fixed the attached test program(processing) to run it in both Windows and Linux.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
xshige | 0:797742779818 | 1 | /* |
xshige | 0:797742779818 | 2 | * |
xshige | 0:797742779818 | 3 | * RPC Over UDP |
xshige | 0:797742779818 | 4 | * |
xshige | 0:797742779818 | 5 | * Command format is same as serial RPC (not HTTP RPC) |
xshige | 0:797742779818 | 6 | * |
xshige | 0:797742779818 | 7 | * (example) |
xshige | 0:797742779818 | 8 | * So you can control mbed by sending the followng command (over UDP) |
xshige | 0:797742779818 | 9 | * /DigitalOut/new LED1 myled |
xshige | 0:797742779818 | 10 | * /myled/write 1 |
xshige | 0:797742779818 | 11 | * /AnalogIn/new p20 ain |
xshige | 0:797742779818 | 12 | * /ain/read |
xshige | 0:797742779818 | 13 | * |
xshige | 0:797742779818 | 14 | * Please find attached Test Programs(processing & ActionScript3) at end of this file as comment "#if 0" |
xshige | 0:797742779818 | 15 | * |
xshige | 0:797742779818 | 16 | */ |
xshige | 0:797742779818 | 17 | |
xshige | 1:918ffa99c345 | 18 | // 2010/11/16 |
xshige | 1:918ffa99c345 | 19 | // Fixed test program(processing) |
xshige | 1:918ffa99c345 | 20 | // to run it in both windows and linux. |
xshige | 1:918ffa99c345 | 21 | |
xshige | 0:797742779818 | 22 | // 2010/11/15 |
xshige | 0:797742779818 | 23 | // written by: xshige |
xshige | 0:797742779818 | 24 | |
xshige | 0:797742779818 | 25 | |
xshige | 0:797742779818 | 26 | #define DEBUG |
xshige | 0:797742779818 | 27 | |
xshige | 0:797742779818 | 28 | // Send Port (RPC output) |
xshige | 0:797742779818 | 29 | #define OUTPUT_PORT 8001 |
xshige | 0:797742779818 | 30 | // Receive Port (RPC input) |
xshige | 0:797742779818 | 31 | #define INPUT_PORT 8000 |
xshige | 0:797742779818 | 32 | |
xshige | 0:797742779818 | 33 | #define DEBUG |
xshige | 0:797742779818 | 34 | |
xshige | 0:797742779818 | 35 | // please comment out if you want two way unicast |
xshige | 0:797742779818 | 36 | #define SENDBACK_UNICAST |
xshige | 0:797742779818 | 37 | |
xshige | 0:797742779818 | 38 | #define DHCP |
xshige | 0:797742779818 | 39 | |
xshige | 0:797742779818 | 40 | #include "mbed.h" |
xshige | 0:797742779818 | 41 | #include "EthernetNetIf.h" |
xshige | 0:797742779818 | 42 | #include "UDPSocket.h" |
xshige | 0:797742779818 | 43 | |
xshige | 0:797742779818 | 44 | #include "rpc.h" |
xshige | 0:797742779818 | 45 | Serial pc(USBTX, USBRX); |
xshige | 0:797742779818 | 46 | |
xshige | 0:797742779818 | 47 | #ifdef DHCP |
xshige | 0:797742779818 | 48 | EthernetNetIf eth; |
xshige | 0:797742779818 | 49 | #else |
xshige | 0:797742779818 | 50 | EthernetNetIf eth( |
xshige | 0:797742779818 | 51 | IpAddr(192,168,0,25), //IP Address |
xshige | 0:797742779818 | 52 | IpAddr(255,255,255,0), //Network Mask |
xshige | 0:797742779818 | 53 | IpAddr(192,168,0,1), //Gateway |
xshige | 0:797742779818 | 54 | IpAddr(192,168,0,1) //DNS |
xshige | 0:797742779818 | 55 | ); |
xshige | 0:797742779818 | 56 | #endif |
xshige | 0:797742779818 | 57 | |
xshige | 0:797742779818 | 58 | UDPSocket udpRec; |
xshige | 0:797742779818 | 59 | UDPSocket udpSend; |
xshige | 0:797742779818 | 60 | |
xshige | 0:797742779818 | 61 | |
xshige | 0:797742779818 | 62 | // mulitcast UDP |
xshige | 0:797742779818 | 63 | #ifdef SENDBACK_UNICAST |
xshige | 0:797742779818 | 64 | Host recHost(IpAddr(239, 255, 0, 1), INPUT_PORT, NULL); // Receive Port (RPC input) |
xshige | 0:797742779818 | 65 | // multicast IP |
xshige | 1:918ffa99c345 | 66 | // "224.0.0.1" works correctly. |
xshige | 1:918ffa99c345 | 67 | // "224.0.0.2" does not work(can Not receive) |
xshige | 0:797742779818 | 68 | // "239.255.0.0"-"239.255.255.255" (Site-Local Scope) maybe work |
xshige | 0:797742779818 | 69 | #else |
xshige | 0:797742779818 | 70 | // please change IP address to fit your enviroment |
xshige | 0:797742779818 | 71 | // unicast IUDP |
xshige | 0:797742779818 | 72 | Host sendHost(IpAddr(192, 168, 0, 7), OUTPUT_PORT, NULL); // Send Port (RPC output) |
xshige | 0:797742779818 | 73 | Host recHost(IpAddr(192, 168, 0, 7), INPUT_PORT, NULL); // Receive Port (RPC input) |
xshige | 1:918ffa99c345 | 74 | //Host sendHost(IpAddr(239, 255, 0, 1), OUTPUT_PORT, NULL); // Send Port (RPC output) |
xshige | 1:918ffa99c345 | 75 | //Host recHost(IpAddr(239, 255, 0, 1), INPUT_PORT, NULL); // Receive Port (RPC input) |
xshige | 0:797742779818 | 76 | #endif |
xshige | 0:797742779818 | 77 | |
xshige | 0:797742779818 | 78 | // receive commands, and send back the responses |
xshige | 0:797742779818 | 79 | char inbuf[512], outbuf[512]; |
xshige | 0:797742779818 | 80 | |
xshige | 0:797742779818 | 81 | void onUDPSocketEvent(UDPSocketEvent e) |
xshige | 0:797742779818 | 82 | { |
xshige | 0:797742779818 | 83 | |
xshige | 0:797742779818 | 84 | switch(e) |
xshige | 0:797742779818 | 85 | { |
xshige | 0:797742779818 | 86 | case UDPSOCKET_READABLE: //The only event for now |
xshige | 0:797742779818 | 87 | |
xshige | 0:797742779818 | 88 | Host host; |
xshige | 0:797742779818 | 89 | while( int len = udpRec.recvfrom( inbuf, 512, &host ) ) |
xshige | 0:797742779818 | 90 | { |
xshige | 0:797742779818 | 91 | if( len <= 0 ) |
xshige | 0:797742779818 | 92 | break; |
xshige | 0:797742779818 | 93 | inbuf[len]=0; |
xshige | 0:797742779818 | 94 | #ifdef DEBUG |
xshige | 0:797742779818 | 95 | printf("\r\nFrom %d.%d.%d.%d: %s\r\n", |
xshige | 0:797742779818 | 96 | host.getIp()[0], host.getIp()[1], host.getIp()[2], host.getIp()[3], inbuf); |
xshige | 0:797742779818 | 97 | #endif |
xshige | 0:797742779818 | 98 | |
xshige | 0:797742779818 | 99 | #ifdef SENDBACK_UNICAST |
xshige | 0:797742779818 | 100 | Host sendHost(IpAddr( |
xshige | 0:797742779818 | 101 | host.getIp()[0], host.getIp()[1], host.getIp()[2], host.getIp()[3]), |
xshige | 0:797742779818 | 102 | OUTPUT_PORT, NULL); // Send Port (RPC output) |
xshige | 0:797742779818 | 103 | #endif |
xshige | 0:797742779818 | 104 | |
xshige | 0:797742779818 | 105 | udpSend.bind(sendHost); |
xshige | 0:797742779818 | 106 | |
xshige | 0:797742779818 | 107 | #ifdef DEBUG |
xshige | 0:797742779818 | 108 | printf("debug1:%d %s\r\n",len,inbuf); |
xshige | 0:797742779818 | 109 | #endif |
xshige | 0:797742779818 | 110 | if (len>0) { |
xshige | 0:797742779818 | 111 | rpc(inbuf, outbuf); |
xshige | 0:797742779818 | 112 | udpSend.sendto( outbuf, strlen(outbuf), &sendHost); |
xshige | 0:797742779818 | 113 | } |
xshige | 0:797742779818 | 114 | #ifdef DEBUG |
xshige | 0:797742779818 | 115 | printf("debug2:%s\r\n", outbuf); |
xshige | 0:797742779818 | 116 | #endif |
xshige | 0:797742779818 | 117 | } |
xshige | 0:797742779818 | 118 | break; |
xshige | 0:797742779818 | 119 | } |
xshige | 0:797742779818 | 120 | } |
xshige | 0:797742779818 | 121 | |
xshige | 0:797742779818 | 122 | int main() { |
xshige | 0:797742779818 | 123 | // make debug port Fast |
xshige | 0:797742779818 | 124 | Serial pc(USBTX, USBRX); |
xshige | 0:797742779818 | 125 | // pc.baud(9600); |
xshige | 0:797742779818 | 126 | pc.baud(115200); |
xshige | 0:797742779818 | 127 | // pc.baud(230400); |
xshige | 0:797742779818 | 128 | |
xshige | 0:797742779818 | 129 | // setup the classes that can be created dynamically |
xshige | 0:797742779818 | 130 | Base::add_rpc_class<AnalogIn>(); |
xshige | 0:797742779818 | 131 | Base::add_rpc_class<AnalogOut>(); |
xshige | 0:797742779818 | 132 | Base::add_rpc_class<DigitalIn>(); |
xshige | 0:797742779818 | 133 | Base::add_rpc_class<DigitalOut>(); |
xshige | 0:797742779818 | 134 | Base::add_rpc_class<DigitalInOut>(); |
xshige | 0:797742779818 | 135 | Base::add_rpc_class<PwmOut>(); |
xshige | 0:797742779818 | 136 | Base::add_rpc_class<Timer>(); |
xshige | 0:797742779818 | 137 | Base::add_rpc_class<SPI>(); |
xshige | 0:797742779818 | 138 | Base::add_rpc_class<BusOut>(); |
xshige | 0:797742779818 | 139 | Base::add_rpc_class<BusIn>(); |
xshige | 0:797742779818 | 140 | Base::add_rpc_class<BusInOut>(); |
xshige | 0:797742779818 | 141 | Base::add_rpc_class<Serial>(); |
xshige | 0:797742779818 | 142 | |
xshige | 0:797742779818 | 143 | // setup network |
xshige | 0:797742779818 | 144 | printf("Setting up...\r\n"); |
xshige | 0:797742779818 | 145 | EthernetErr ethErr = eth.setup(); |
xshige | 0:797742779818 | 146 | if(ethErr) { |
xshige | 0:797742779818 | 147 | printf("Error %d in setup.\r\n", ethErr); |
xshige | 0:797742779818 | 148 | return -1; |
xshige | 0:797742779818 | 149 | } |
xshige | 0:797742779818 | 150 | |
xshige | 0:797742779818 | 151 | printf("UDP RPC Setup OK\r\n"); |
xshige | 0:797742779818 | 152 | |
xshige | 0:797742779818 | 153 | udpRec.setOnEvent(&onUDPSocketEvent); |
xshige | 0:797742779818 | 154 | |
xshige | 0:797742779818 | 155 | udpRec.bind(recHost); |
xshige | 0:797742779818 | 156 | |
xshige | 0:797742779818 | 157 | |
xshige | 0:797742779818 | 158 | while(1) { |
xshige | 0:797742779818 | 159 | Net::poll(); |
xshige | 0:797742779818 | 160 | #if 0 |
xshige | 0:797742779818 | 161 | // debug code |
xshige | 0:797742779818 | 162 | Host host; |
xshige | 0:797742779818 | 163 | int len = udpRec.recvfrom( inbuf, 512, &host ); |
xshige | 0:797742779818 | 164 | printf("debug:%d %s\r\n",len,inbuf); |
xshige | 0:797742779818 | 165 | len=1; |
xshige | 0:797742779818 | 166 | if (len>0) { |
xshige | 0:797742779818 | 167 | rpc(inbuf, outbuf); |
xshige | 0:797742779818 | 168 | strcpy(outbuf,"RPC UDP Test"); |
xshige | 0:797742779818 | 169 | udpSend.sendto( outbuf, strlen(outbuf), &sendHost ); |
xshige | 0:797742779818 | 170 | memset(inbuf,0,512); |
xshige | 0:797742779818 | 171 | } |
xshige | 0:797742779818 | 172 | printf("debug:%s\r\n", outbuf); |
xshige | 0:797742779818 | 173 | memset(outbuf,0,512); |
xshige | 0:797742779818 | 174 | wait(1); |
xshige | 0:797742779818 | 175 | #endif |
xshige | 0:797742779818 | 176 | } |
xshige | 0:797742779818 | 177 | |
xshige | 0:797742779818 | 178 | } |
xshige | 0:797742779818 | 179 | |
xshige | 0:797742779818 | 180 | #if 0 |
xshige | 0:797742779818 | 181 | //------------------------------------------------------------------------ |
xshige | 0:797742779818 | 182 | // Test Program (written in processing) |
xshige | 0:797742779818 | 183 | |
xshige | 0:797742779818 | 184 | // Important Note: |
xshige | 0:797742779818 | 185 | // this program can NOT work in windows enviroment (only Linux enviroment) |
xshige | 1:918ffa99c345 | 186 | // |
xshige | 1:918ffa99c345 | 187 | // 2010/11/16: |
xshige | 1:918ffa99c345 | 188 | // Above bug fixed. Now it can work in both windows and linux. |
xshige | 1:918ffa99c345 | 189 | // Note: Please set your host IP. |
xshige | 0:797742779818 | 190 | |
xshige | 0:797742779818 | 191 | /** |
xshige | 0:797742779818 | 192 | * RCP over UDP (by mbed) |
xshige | 0:797742779818 | 193 | * Test Program |
xshige | 0:797742779818 | 194 | * |
xshige | 0:797742779818 | 195 | * you can get UDP library from: |
xshige | 0:797742779818 | 196 | * http://ubaa.net/shared/processing/udp/ |
xshige | 0:797742779818 | 197 | * |
xshige | 0:797742779818 | 198 | */ |
xshige | 0:797742779818 | 199 | |
xshige | 0:797742779818 | 200 | // import UDP library |
xshige | 0:797742779818 | 201 | import hypermedia.net.*; |
xshige | 0:797742779818 | 202 | |
xshige | 0:797742779818 | 203 | String remoteIP = "239.255.0.1"; // multicast IP |
xshige | 1:918ffa99c345 | 204 | String localIP = "192.168.0.7"; // please set your host IP (2010/11/16) |
xshige | 0:797742779818 | 205 | int INPUT_PORT = 8000; |
xshige | 0:797742779818 | 206 | int OUTPUT_PORT = 8001; |
xshige | 0:797742779818 | 207 | |
xshige | 0:797742779818 | 208 | UDP udp; // define the UDP object |
xshige | 0:797742779818 | 209 | |
xshige | 0:797742779818 | 210 | /** |
xshige | 0:797742779818 | 211 | * init |
xshige | 0:797742779818 | 212 | */ |
xshige | 0:797742779818 | 213 | void setup() { |
xshige | 0:797742779818 | 214 | // create a new datagram connection on port OUTPUT_PORT |
xshige | 0:797742779818 | 215 | // and wait for incomming message |
xshige | 1:918ffa99c345 | 216 | udp = new UDP( this, OUTPUT_PORT, localIP ); // (2010/11/16) |
xshige | 0:797742779818 | 217 | // udp.log( true ); // <-- printout the connection activity |
xshige | 0:797742779818 | 218 | udp.listen( true ); |
xshige | 0:797742779818 | 219 | } |
xshige | 0:797742779818 | 220 | |
xshige | 0:797742779818 | 221 | //process events |
xshige | 0:797742779818 | 222 | void draw() {;} |
xshige | 0:797742779818 | 223 | |
xshige | 0:797742779818 | 224 | /* |
xshige | 0:797742779818 | 225 | * on key pressed event: |
xshige | 0:797742779818 | 226 | * pressing a key sends predefined command |
xshige | 0:797742779818 | 227 | */ |
xshige | 0:797742779818 | 228 | void keyPressed() { |
xshige | 0:797742779818 | 229 | String cmd=""; |
xshige | 0:797742779818 | 230 | if (key == '1') cmd="/"; |
xshige | 0:797742779818 | 231 | // |
xshige | 0:797742779818 | 232 | if (key == '2') cmd="/AnalogIn/new p20 ain"; |
xshige | 0:797742779818 | 233 | if (key == '3') cmd="/ain/read"; |
xshige | 0:797742779818 | 234 | // |
xshige | 0:797742779818 | 235 | if (key == '4') cmd="/AnalogOut/new p18 aout"; |
xshige | 0:797742779818 | 236 | if (key == '5') cmd="/aout/"; |
xshige | 0:797742779818 | 237 | if (key == '6') cmd="/aout/write_u16 4123"; |
xshige | 0:797742779818 | 238 | // if (key == '4') cmd="/BusIn/new bin p23 p22 p21"; |
xshige | 0:797742779818 | 239 | // if (key == '5') cmd="/bin/read"; |
xshige | 0:797742779818 | 240 | // |
xshige | 0:797742779818 | 241 | if (key == '8') cmd="/DigitalOut/new LED4 myled4"; |
xshige | 0:797742779818 | 242 | if (key == '9') cmd="/myled4/write 1"; |
xshige | 0:797742779818 | 243 | if (key == '0') cmd="/myled4/write 0"; |
xshige | 0:797742779818 | 244 | |
xshige | 0:797742779818 | 245 | // send the command |
xshige | 0:797742779818 | 246 | udp.send( cmd, remoteIP, INPUT_PORT ); |
xshige | 0:797742779818 | 247 | |
xshige | 0:797742779818 | 248 | } |
xshige | 0:797742779818 | 249 | |
xshige | 0:797742779818 | 250 | /* |
xshige | 0:797742779818 | 251 | * To perform any action on datagram reception |
xshige | 0:797742779818 | 252 | */ |
xshige | 0:797742779818 | 253 | |
xshige | 0:797742779818 | 254 | void receive( byte[] data, String ip, int port ) { |
xshige | 0:797742779818 | 255 | |
xshige | 0:797742779818 | 256 | data = subset(data, 0, data.length); |
xshige | 0:797742779818 | 257 | String message = new String( data ); |
xshige | 0:797742779818 | 258 | |
xshige | 0:797742779818 | 259 | // print the result |
xshige | 0:797742779818 | 260 | println( "received: \""+message+"\" from "+ip+" on port "+port ); |
xshige | 0:797742779818 | 261 | } |
xshige | 0:797742779818 | 262 | //---------------------------------------------------------------------------- |
xshige | 0:797742779818 | 263 | #endif |
xshige | 0:797742779818 | 264 | |
xshige | 0:797742779818 | 265 | #if 0 |
xshige | 0:797742779818 | 266 | //------------------------------------------------------------------------ |
xshige | 0:797742779818 | 267 | // Test Program (written in ActionScript3) |
xshige | 0:797742779818 | 268 | |
xshige | 0:797742779818 | 269 | // Important Note: |
xshige | 0:797742779818 | 270 | // this program can work in both windows and Linux enviroment. |
xshige | 0:797742779818 | 271 | |
xshige | 0:797742779818 | 272 | // DatagramSocketExample.as |
xshige | 0:797742779818 | 273 | // |
xshige | 0:797742779818 | 274 | // This program is comming from: |
xshige | 0:797742779818 | 275 | // http://help.adobe.com/en_US/FlashPlatform/beta/reference/actionscript/3/flash/net/DatagramSocket.html |
xshige | 0:797742779818 | 276 | // |
xshige | 0:797742779818 | 277 | // I changed some parameters(IP address, port# etc ) for testing mbed HTTP over UDP |
xshige | 0:797742779818 | 278 | // You can send command over UDP by changing "message:" field |
xshige | 0:797742779818 | 279 | // |
xshige | 0:797742779818 | 280 | // You must build it as AIR application to utilize UDP capability. |
xshige | 0:797742779818 | 281 | // localIP should be changed to fit your enviroment. |
xshige | 0:797742779818 | 282 | // |
xshige | 0:797742779818 | 283 | package |
xshige | 0:797742779818 | 284 | { |
xshige | 0:797742779818 | 285 | import flash.display.Sprite; |
xshige | 0:797742779818 | 286 | import flash.events.DatagramSocketDataEvent; |
xshige | 0:797742779818 | 287 | import flash.events.Event; |
xshige | 0:797742779818 | 288 | import flash.events.MouseEvent; |
xshige | 0:797742779818 | 289 | import flash.events.TimerEvent; |
xshige | 0:797742779818 | 290 | import flash.net.DatagramSocket; |
xshige | 0:797742779818 | 291 | import flash.text.TextField; |
xshige | 0:797742779818 | 292 | import flash.text.TextFieldAutoSize; |
xshige | 0:797742779818 | 293 | import flash.text.TextFieldType; |
xshige | 0:797742779818 | 294 | import flash.utils.ByteArray; |
xshige | 0:797742779818 | 295 | import flash.utils.Timer; |
xshige | 0:797742779818 | 296 | |
xshige | 0:797742779818 | 297 | public class DatagramSocketExample extends Sprite |
xshige | 0:797742779818 | 298 | { |
xshige | 0:797742779818 | 299 | private var datagramSocket:DatagramSocket = new DatagramSocket();; |
xshige | 0:797742779818 | 300 | |
xshige | 0:797742779818 | 301 | private var localIP:TextField; |
xshige | 0:797742779818 | 302 | private var localPort:TextField; |
xshige | 0:797742779818 | 303 | private var logField:TextField; |
xshige | 0:797742779818 | 304 | private var targetIP:TextField; |
xshige | 0:797742779818 | 305 | private var targetPort:TextField; |
xshige | 0:797742779818 | 306 | private var message:TextField; |
xshige | 0:797742779818 | 307 | |
xshige | 0:797742779818 | 308 | public function DatagramSocketExample() |
xshige | 0:797742779818 | 309 | { |
xshige | 0:797742779818 | 310 | setupUI(); |
xshige | 0:797742779818 | 311 | } |
xshige | 0:797742779818 | 312 | |
xshige | 0:797742779818 | 313 | private function bind( event:Event ):void |
xshige | 0:797742779818 | 314 | { |
xshige | 0:797742779818 | 315 | if( datagramSocket.bound ) |
xshige | 0:797742779818 | 316 | { |
xshige | 0:797742779818 | 317 | datagramSocket.close(); |
xshige | 0:797742779818 | 318 | datagramSocket = new DatagramSocket(); |
xshige | 0:797742779818 | 319 | |
xshige | 0:797742779818 | 320 | } |
xshige | 0:797742779818 | 321 | datagramSocket.bind( parseInt( localPort.text ), localIP.text ); |
xshige | 0:797742779818 | 322 | datagramSocket.addEventListener( DatagramSocketDataEvent.DATA, dataReceived ); |
xshige | 0:797742779818 | 323 | datagramSocket.receive(); |
xshige | 0:797742779818 | 324 | log( "Bound to: " + datagramSocket.localAddress + ":" + datagramSocket.localPort ); |
xshige | 0:797742779818 | 325 | } |
xshige | 0:797742779818 | 326 | |
xshige | 0:797742779818 | 327 | private function dataReceived( event:DatagramSocketDataEvent ):void |
xshige | 0:797742779818 | 328 | { |
xshige | 0:797742779818 | 329 | //Read the data from the datagram |
xshige | 0:797742779818 | 330 | log("Received from " + event.srcAddress + ":" + event.srcPort + "> " + |
xshige | 0:797742779818 | 331 | event.data.readUTFBytes( event.data.bytesAvailable ) ); |
xshige | 0:797742779818 | 332 | } |
xshige | 0:797742779818 | 333 | |
xshige | 0:797742779818 | 334 | private function send( event:Event ):void |
xshige | 0:797742779818 | 335 | { |
xshige | 0:797742779818 | 336 | //Create a message in a ByteArray |
xshige | 0:797742779818 | 337 | var data:ByteArray = new ByteArray(); |
xshige | 0:797742779818 | 338 | data.writeUTFBytes( message.text ); |
xshige | 0:797742779818 | 339 | |
xshige | 0:797742779818 | 340 | //Send a datagram to the target |
xshige | 0:797742779818 | 341 | try |
xshige | 0:797742779818 | 342 | { |
xshige | 0:797742779818 | 343 | datagramSocket.send( data, 0, 0, targetIP.text, parseInt( targetPort.text )); |
xshige | 0:797742779818 | 344 | log( "Sent message to " + targetIP.text + ":" + targetPort.text ); |
xshige | 0:797742779818 | 345 | } |
xshige | 0:797742779818 | 346 | catch ( error:Error ) |
xshige | 0:797742779818 | 347 | { |
xshige | 0:797742779818 | 348 | log( error.message ); |
xshige | 0:797742779818 | 349 | } |
xshige | 0:797742779818 | 350 | } |
xshige | 0:797742779818 | 351 | |
xshige | 0:797742779818 | 352 | private function log( text:String ):void |
xshige | 0:797742779818 | 353 | { |
xshige | 0:797742779818 | 354 | logField.appendText( text + "\n" ); |
xshige | 0:797742779818 | 355 | logField.scrollV = logField.maxScrollV; |
xshige | 0:797742779818 | 356 | trace( text ); |
xshige | 0:797742779818 | 357 | } |
xshige | 0:797742779818 | 358 | private function setupUI():void |
xshige | 0:797742779818 | 359 | { |
xshige | 0:797742779818 | 360 | // targetIP = createTextField( 10, 10, "Target IP:", "192.168.0.1" ); |
xshige | 0:797742779818 | 361 | // targetPort = createTextField( 10, 35, "Target port:", "8989" ); |
xshige | 0:797742779818 | 362 | |
xshige | 0:797742779818 | 363 | targetIP = createTextField( 10, 10, "Target IP:", "239.255.0.1" ); |
xshige | 0:797742779818 | 364 | targetPort = createTextField( 10, 35, "Target port:", "8000" ); |
xshige | 1:918ffa99c345 | 365 | |
xshige | 0:797742779818 | 366 | message = createTextField( 10, 60, "Message:", "/AnalogIn/new p20 ain" ); |
xshige | 0:797742779818 | 367 | // localIP = createTextField( 10, 85, "Local IP", "0.0.0.0"); |
xshige | 0:797742779818 | 368 | // localPort = createTextField( 10, 110, "Local port:", "0" ); |
xshige | 0:797742779818 | 369 | |
xshige | 1:918ffa99c345 | 370 | localIP = createTextField( 10, 85, "Local IP", "192.168.0.7"); // set your host IP |
xshige | 1:918ffa99c345 | 371 | localPort = createTextField( 10, 110, "Local port:", "8001" ); |
xshige | 0:797742779818 | 372 | |
xshige | 0:797742779818 | 373 | createTextButton( 250, 135, "Bind", bind ); |
xshige | 0:797742779818 | 374 | createTextButton( 300, 135, "Send", send ); |
xshige | 0:797742779818 | 375 | logField = createTextField( 10, 160, "Log:", "", false, 200 ) |
xshige | 0:797742779818 | 376 | |
xshige | 0:797742779818 | 377 | this.stage.nativeWindow.activate(); |
xshige | 0:797742779818 | 378 | } |
xshige | 0:797742779818 | 379 | |
xshige | 0:797742779818 | 380 | private function createTextField( x:int, y:int, label:String, defaultValue:String = '', editable:Boolean = true, height:int = 20 ):TextField |
xshige | 0:797742779818 | 381 | { |
xshige | 0:797742779818 | 382 | var labelField:TextField = new TextField(); |
xshige | 0:797742779818 | 383 | labelField.text = label; |
xshige | 0:797742779818 | 384 | labelField.type = TextFieldType.DYNAMIC; |
xshige | 0:797742779818 | 385 | labelField.width = 180; |
xshige | 0:797742779818 | 386 | labelField.x = x; |
xshige | 0:797742779818 | 387 | labelField.y = y; |
xshige | 0:797742779818 | 388 | |
xshige | 0:797742779818 | 389 | var input:TextField = new TextField(); |
xshige | 0:797742779818 | 390 | input.text = defaultValue; |
xshige | 0:797742779818 | 391 | input.type = TextFieldType.INPUT; |
xshige | 0:797742779818 | 392 | input.border = editable; |
xshige | 0:797742779818 | 393 | input.selectable = editable; |
xshige | 0:797742779818 | 394 | input.width = 280; |
xshige | 0:797742779818 | 395 | input.height = height; |
xshige | 0:797742779818 | 396 | input.x = x + labelField.width; |
xshige | 0:797742779818 | 397 | input.y = y; |
xshige | 0:797742779818 | 398 | |
xshige | 0:797742779818 | 399 | this.addChild( labelField ); |
xshige | 0:797742779818 | 400 | this.addChild( input ); |
xshige | 0:797742779818 | 401 | |
xshige | 0:797742779818 | 402 | return input; |
xshige | 0:797742779818 | 403 | } |
xshige | 0:797742779818 | 404 | |
xshige | 0:797742779818 | 405 | private function createTextButton( x:int, y:int, label:String, clickHandler:Function ):TextField |
xshige | 0:797742779818 | 406 | { |
xshige | 0:797742779818 | 407 | var button:TextField = new TextField(); |
xshige | 0:797742779818 | 408 | button.htmlText = "<u><b>" + label + "</b></u>"; |
xshige | 0:797742779818 | 409 | button.type = TextFieldType.DYNAMIC; |
xshige | 0:797742779818 | 410 | button.selectable = false; |
xshige | 0:797742779818 | 411 | button.width = 180; |
xshige | 0:797742779818 | 412 | button.x = x; |
xshige | 0:797742779818 | 413 | button.y = y; |
xshige | 0:797742779818 | 414 | button.addEventListener( MouseEvent.CLICK, clickHandler ); |
xshige | 0:797742779818 | 415 | |
xshige | 0:797742779818 | 416 | this.addChild( button ); |
xshige | 0:797742779818 | 417 | return button; |
xshige | 0:797742779818 | 418 | |
xshige | 0:797742779818 | 419 | } |
xshige | 0:797742779818 | 420 | } |
xshige | 0:797742779818 | 421 | } |
xshige | 0:797742779818 | 422 | //------------------------------------------------------------------------ |
xshige | 0:797742779818 | 423 | #endif |