RPC Over TCP(telnet) Command format is same as serial RPC (not HTTP RPC). Please find attached Test Program(Processing and ActionScript3) at end of this file. Same functionality of Serial RPC via Serproxy.

Dependencies:   EthernetNetIf mbed

Committer:
xshige
Date:
Fri Nov 19 17:19:03 2010 +0000
Revision:
0:a0cd4d9690f0

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
xshige 0:a0cd4d9690f0 1 /*
xshige 0:a0cd4d9690f0 2 *
xshige 0:a0cd4d9690f0 3 * RPC over TCP (RPC over telnet)
xshige 0:a0cd4d9690f0 4 * (Same functionality of Serial RPC via Serproxy )
xshige 0:a0cd4d9690f0 5 *
xshige 0:a0cd4d9690f0 6 * Command format is same as serial RPC (not HTTP RPC)
xshige 0:a0cd4d9690f0 7 *
xshige 0:a0cd4d9690f0 8 * (example)
xshige 0:a0cd4d9690f0 9 * So you can control mbed by sending the followng command (over UDP)
xshige 0:a0cd4d9690f0 10 * /DigitalOut/new LED1 myled
xshige 0:a0cd4d9690f0 11 * /myled/write 1
xshige 0:a0cd4d9690f0 12 * /AnalogIn/new p20 ain
xshige 0:a0cd4d9690f0 13 * /ain/read
xshige 0:a0cd4d9690f0 14 *
xshige 0:a0cd4d9690f0 15 * Please find attached Test Programs(processing & ActionScript3) at end of this file as comment "#if 0"
xshige 0:a0cd4d9690f0 16 *
xshige 0:a0cd4d9690f0 17 * Serproxy(Serial Proxy) source&binary can be downloaded from:
xshige 0:a0cd4d9690f0 18 * http://cote.cc/blog/serialproxy-v014-can-use-com-ports-above-9
xshige 0:a0cd4d9690f0 19 * http://cote.cc/sites/default/files/serproxy-0.1.4.win32.zip
xshige 0:a0cd4d9690f0 20 * http://cote.cc/sites/default/files/serproxy-0.1.4.src_.zip
xshige 0:a0cd4d9690f0 21 *
xshige 0:a0cd4d9690f0 22 */
xshige 0:a0cd4d9690f0 23
xshige 0:a0cd4d9690f0 24 // 2010/11/20
xshige 0:a0cd4d9690f0 25 // written by: xshige
xshige 0:a0cd4d9690f0 26
xshige 0:a0cd4d9690f0 27
xshige 0:a0cd4d9690f0 28 // please change port# if you need
xshige 0:a0cd4d9690f0 29 #define TCP_LISTENING_PORT 5335 // telnet port
xshige 0:a0cd4d9690f0 30
xshige 0:a0cd4d9690f0 31 #define DEBUG
xshige 0:a0cd4d9690f0 32 //#define DHCP
xshige 0:a0cd4d9690f0 33 //#define LIVE_LED
xshige 0:a0cd4d9690f0 34
xshige 0:a0cd4d9690f0 35 #include "mbed.h"
xshige 0:a0cd4d9690f0 36 #include "EthernetNetIf.h"
xshige 0:a0cd4d9690f0 37 #include "TCPSocket.h"
xshige 0:a0cd4d9690f0 38 #include "rpc.h"
xshige 0:a0cd4d9690f0 39
xshige 0:a0cd4d9690f0 40 #ifdef DHCP
xshige 0:a0cd4d9690f0 41 EthernetNetIf eth;
xshige 0:a0cd4d9690f0 42 #else
xshige 0:a0cd4d9690f0 43 // please set your mbed IP
xshige 0:a0cd4d9690f0 44 EthernetNetIf eth(
xshige 0:a0cd4d9690f0 45 IpAddr(192,168,0,25), //IP Address
xshige 0:a0cd4d9690f0 46 IpAddr(255,255,255,0), //Network Mask
xshige 0:a0cd4d9690f0 47 IpAddr(192,168,0,1), //Gateway
xshige 0:a0cd4d9690f0 48 IpAddr(192,168,0,1) //DNS
xshige 0:a0cd4d9690f0 49 );
xshige 0:a0cd4d9690f0 50 #endif
xshige 0:a0cd4d9690f0 51
xshige 0:a0cd4d9690f0 52 #ifdef LIVE_LED
xshige 0:a0cd4d9690f0 53 DigitalOut led4(LED4, "led4");
xshige 0:a0cd4d9690f0 54 #endif
xshige 0:a0cd4d9690f0 55
xshige 0:a0cd4d9690f0 56
xshige 0:a0cd4d9690f0 57 // receive commands, and send back the responses
xshige 0:a0cd4d9690f0 58 char inbuf[512], outbuf[512];
xshige 0:a0cd4d9690f0 59
xshige 0:a0cd4d9690f0 60 TCPSocket ListeningSock;
xshige 0:a0cd4d9690f0 61 TCPSocket* pConnectedSock; // for ConnectedSock
xshige 0:a0cd4d9690f0 62 Host client;
xshige 0:a0cd4d9690f0 63 TCPSocketErr err;
xshige 0:a0cd4d9690f0 64
xshige 0:a0cd4d9690f0 65 void onConnectedTCPSocketEvent(TCPSocketEvent e)
xshige 0:a0cd4d9690f0 66 {
xshige 0:a0cd4d9690f0 67 switch(e)
xshige 0:a0cd4d9690f0 68 {
xshige 0:a0cd4d9690f0 69 case TCPSOCKET_CONNECTED:
xshige 0:a0cd4d9690f0 70 printf("TCP Socket Connected\r\n");
xshige 0:a0cd4d9690f0 71 break;
xshige 0:a0cd4d9690f0 72 case TCPSOCKET_WRITEABLE:
xshige 0:a0cd4d9690f0 73 //Can now write some data...
xshige 0:a0cd4d9690f0 74 // printf("TCP Socket Writable\r\n");
xshige 0:a0cd4d9690f0 75 break;
xshige 0:a0cd4d9690f0 76 case TCPSOCKET_READABLE:
xshige 0:a0cd4d9690f0 77 //Can now read dome data...
xshige 0:a0cd4d9690f0 78 // printf("TCP Socket Readable\r\n");
xshige 0:a0cd4d9690f0 79 //-------------------------------------
xshige 0:a0cd4d9690f0 80 // Read in any available data into the buffer
xshige 0:a0cd4d9690f0 81 int len = pConnectedSock->recv(inbuf, 512);
xshige 0:a0cd4d9690f0 82 inbuf[len] = 0;
xshige 0:a0cd4d9690f0 83 #ifdef DEBUG
xshige 0:a0cd4d9690f0 84 printf("\nReceived: %s", inbuf);
xshige 0:a0cd4d9690f0 85 #endif
xshige 0:a0cd4d9690f0 86 // do RPC
xshige 0:a0cd4d9690f0 87 if (len>0) {
xshige 0:a0cd4d9690f0 88 rpc(inbuf, outbuf);
xshige 0:a0cd4d9690f0 89 outbuf[strlen(outbuf)]=0;
xshige 0:a0cd4d9690f0 90 // send result of RPC
xshige 0:a0cd4d9690f0 91 pConnectedSock->send(outbuf, strlen(outbuf));
xshige 0:a0cd4d9690f0 92 #ifdef DEBUG
xshige 0:a0cd4d9690f0 93 printf("Sent:%s\n",outbuf);
xshige 0:a0cd4d9690f0 94 #endif
xshige 0:a0cd4d9690f0 95 }
xshige 0:a0cd4d9690f0 96 break;
xshige 0:a0cd4d9690f0 97 //----------------------------------------
xshige 0:a0cd4d9690f0 98 case TCPSOCKET_CONTIMEOUT:
xshige 0:a0cd4d9690f0 99 printf("TCP Socket Timeout\r\n");
xshige 0:a0cd4d9690f0 100 break;
xshige 0:a0cd4d9690f0 101 case TCPSOCKET_CONRST:
xshige 0:a0cd4d9690f0 102 printf("TCP Socket CONRST\r\n");
xshige 0:a0cd4d9690f0 103 break;
xshige 0:a0cd4d9690f0 104 case TCPSOCKET_CONABRT:
xshige 0:a0cd4d9690f0 105 printf("TCP Socket CONABRT\r\n");
xshige 0:a0cd4d9690f0 106 break;
xshige 0:a0cd4d9690f0 107 case TCPSOCKET_ERROR:
xshige 0:a0cd4d9690f0 108 printf("TCP Socket Error\r\n");
xshige 0:a0cd4d9690f0 109 break;
xshige 0:a0cd4d9690f0 110 case TCPSOCKET_DISCONNECTED:
xshige 0:a0cd4d9690f0 111 printf("TCP Socket Disconnected\r\n");
xshige 0:a0cd4d9690f0 112 // pConnectedSock->close();
xshige 0:a0cd4d9690f0 113 break;
xshige 0:a0cd4d9690f0 114 default:
xshige 0:a0cd4d9690f0 115 printf("DEFAULT\r\n");
xshige 0:a0cd4d9690f0 116 }
xshige 0:a0cd4d9690f0 117 }
xshige 0:a0cd4d9690f0 118
xshige 0:a0cd4d9690f0 119
xshige 0:a0cd4d9690f0 120 void onListeningTCPSocketEvent(TCPSocketEvent e)
xshige 0:a0cd4d9690f0 121 {
xshige 0:a0cd4d9690f0 122 switch(e)
xshige 0:a0cd4d9690f0 123 {
xshige 0:a0cd4d9690f0 124 case TCPSOCKET_ACCEPT:
xshige 0:a0cd4d9690f0 125 printf("Listening: TCP Socket Accepted\r\n");
xshige 0:a0cd4d9690f0 126 // Accepts connection from client and gets connected socket.
xshige 0:a0cd4d9690f0 127 err=ListeningSock.accept(&client, &pConnectedSock);
xshige 0:a0cd4d9690f0 128 if (err) {
xshige 0:a0cd4d9690f0 129 printf("onListeningTcpSocketEvent : Could not accept connection.\r\n");
xshige 0:a0cd4d9690f0 130 return; //Error in accept, discard connection
xshige 0:a0cd4d9690f0 131 }
xshige 0:a0cd4d9690f0 132 // Setup the new socket events
xshige 0:a0cd4d9690f0 133 pConnectedSock->setOnEvent(&onConnectedTCPSocketEvent);
xshige 0:a0cd4d9690f0 134 // We can find out from where the connection is coming by looking at the
xshige 0:a0cd4d9690f0 135 // Host parameter of the accept() method
xshige 0:a0cd4d9690f0 136 IpAddr clientIp = client.getIp();
xshige 0:a0cd4d9690f0 137 printf("Listening: Incoming TCP connection from %d.%d.%d.%d\r\n",
xshige 0:a0cd4d9690f0 138 clientIp[0], clientIp[1], clientIp[2], clientIp[3]);
xshige 0:a0cd4d9690f0 139 break;
xshige 0:a0cd4d9690f0 140 // the following cases will not happen
xshige 0:a0cd4d9690f0 141 case TCPSOCKET_CONNECTED:
xshige 0:a0cd4d9690f0 142 printf("Listening: TCP Socket Connected\r\n");
xshige 0:a0cd4d9690f0 143 break;
xshige 0:a0cd4d9690f0 144 case TCPSOCKET_WRITEABLE:
xshige 0:a0cd4d9690f0 145 printf("Listening: TCP Socket Writable\r\n");
xshige 0:a0cd4d9690f0 146 break;
xshige 0:a0cd4d9690f0 147 case TCPSOCKET_READABLE:
xshige 0:a0cd4d9690f0 148 printf("Listening: TCP Socket Readable\r\n");
xshige 0:a0cd4d9690f0 149 break;
xshige 0:a0cd4d9690f0 150 case TCPSOCKET_CONTIMEOUT:
xshige 0:a0cd4d9690f0 151 printf("Listening: TCP Socket Timeout\r\n");
xshige 0:a0cd4d9690f0 152 break;
xshige 0:a0cd4d9690f0 153 case TCPSOCKET_CONRST:
xshige 0:a0cd4d9690f0 154 printf("Listening: TCP Socket CONRST\r\n");
xshige 0:a0cd4d9690f0 155 break;
xshige 0:a0cd4d9690f0 156 case TCPSOCKET_CONABRT:
xshige 0:a0cd4d9690f0 157 printf("Listening: TCP Socket CONABRT\r\n");
xshige 0:a0cd4d9690f0 158 break;
xshige 0:a0cd4d9690f0 159 case TCPSOCKET_ERROR:
xshige 0:a0cd4d9690f0 160 printf("Listening: TCP Socket Error\r\n");
xshige 0:a0cd4d9690f0 161 break;
xshige 0:a0cd4d9690f0 162 case TCPSOCKET_DISCONNECTED:
xshige 0:a0cd4d9690f0 163 //Close socket...
xshige 0:a0cd4d9690f0 164 printf("Listening: TCP Socket Disconnected\r\n");
xshige 0:a0cd4d9690f0 165 // keep socket for next connection //ListeningSock.close();
xshige 0:a0cd4d9690f0 166 break;
xshige 0:a0cd4d9690f0 167 default:
xshige 0:a0cd4d9690f0 168 printf("DEFAULT\r\n");
xshige 0:a0cd4d9690f0 169 };
xshige 0:a0cd4d9690f0 170 }
xshige 0:a0cd4d9690f0 171
xshige 0:a0cd4d9690f0 172
xshige 0:a0cd4d9690f0 173 int main() {
xshige 0:a0cd4d9690f0 174
xshige 0:a0cd4d9690f0 175 // make debug port Fast
xshige 0:a0cd4d9690f0 176 Serial pc(USBTX, USBRX);
xshige 0:a0cd4d9690f0 177 // pc.baud(9600);
xshige 0:a0cd4d9690f0 178 pc.baud(115200);
xshige 0:a0cd4d9690f0 179 // pc.baud(230400);
xshige 0:a0cd4d9690f0 180
xshige 0:a0cd4d9690f0 181
xshige 0:a0cd4d9690f0 182 // setup the classes that can be created dynamically
xshige 0:a0cd4d9690f0 183 Base::add_rpc_class<AnalogIn>();
xshige 0:a0cd4d9690f0 184 Base::add_rpc_class<AnalogOut>();
xshige 0:a0cd4d9690f0 185 Base::add_rpc_class<DigitalIn>();
xshige 0:a0cd4d9690f0 186 Base::add_rpc_class<DigitalOut>();
xshige 0:a0cd4d9690f0 187 Base::add_rpc_class<DigitalInOut>();
xshige 0:a0cd4d9690f0 188 Base::add_rpc_class<PwmOut>();
xshige 0:a0cd4d9690f0 189 Base::add_rpc_class<Timer>();
xshige 0:a0cd4d9690f0 190 Base::add_rpc_class<SPI>();
xshige 0:a0cd4d9690f0 191 Base::add_rpc_class<BusOut>();
xshige 0:a0cd4d9690f0 192 Base::add_rpc_class<BusIn>();
xshige 0:a0cd4d9690f0 193 Base::add_rpc_class<BusInOut>();
xshige 0:a0cd4d9690f0 194 Base::add_rpc_class<Serial>();
xshige 0:a0cd4d9690f0 195
xshige 0:a0cd4d9690f0 196 // setup network
xshige 0:a0cd4d9690f0 197 printf("\r\n");
xshige 0:a0cd4d9690f0 198 printf("Setting up...\r\n");
xshige 0:a0cd4d9690f0 199 EthernetErr ethErr = eth.setup();
xshige 0:a0cd4d9690f0 200 if(ethErr)
xshige 0:a0cd4d9690f0 201 {
xshige 0:a0cd4d9690f0 202 printf("Error %d in setup.\r\n", ethErr);
xshige 0:a0cd4d9690f0 203 return -1;
xshige 0:a0cd4d9690f0 204 }
xshige 0:a0cd4d9690f0 205 printf("RPC over TCP(telnet) Setup OK\r\n");
xshige 0:a0cd4d9690f0 206
xshige 0:a0cd4d9690f0 207 IpAddr ip = eth.getIp();
xshige 0:a0cd4d9690f0 208 printf("mbed IP Address is %d.%d.%d.%d:%d\r\n", ip[0], ip[1], ip[2], ip[3],(TCP_LISTENING_PORT));
xshige 0:a0cd4d9690f0 209
xshige 0:a0cd4d9690f0 210 // Set the callbacks for Listening
xshige 0:a0cd4d9690f0 211 ListeningSock.setOnEvent(&onListeningTCPSocketEvent);
xshige 0:a0cd4d9690f0 212
xshige 0:a0cd4d9690f0 213 // bind and listen on TCP
xshige 0:a0cd4d9690f0 214 err=ListeningSock.bind(Host(IpAddr(), TCP_LISTENING_PORT));
xshige 0:a0cd4d9690f0 215 printf("Binding..\r\n");
xshige 0:a0cd4d9690f0 216 if(err)
xshige 0:a0cd4d9690f0 217 {
xshige 0:a0cd4d9690f0 218 //Deal with that error...
xshige 0:a0cd4d9690f0 219 printf("Binding Error\n");
xshige 0:a0cd4d9690f0 220 }
xshige 0:a0cd4d9690f0 221
xshige 0:a0cd4d9690f0 222 err=ListeningSock.listen(); // Starts listening
xshige 0:a0cd4d9690f0 223 printf("Listening...\r\n");
xshige 0:a0cd4d9690f0 224 if(err)
xshige 0:a0cd4d9690f0 225 {
xshige 0:a0cd4d9690f0 226 printf("Listening Error\r\n");
xshige 0:a0cd4d9690f0 227 }
xshige 0:a0cd4d9690f0 228
xshige 0:a0cd4d9690f0 229 #ifdef LIVE_LED
xshige 0:a0cd4d9690f0 230 Timer tmr;
xshige 0:a0cd4d9690f0 231 tmr.start();
xshige 0:a0cd4d9690f0 232 #endif
xshige 0:a0cd4d9690f0 233
xshige 0:a0cd4d9690f0 234
xshige 0:a0cd4d9690f0 235 while(true)
xshige 0:a0cd4d9690f0 236 {
xshige 0:a0cd4d9690f0 237 Net::poll();
xshige 0:a0cd4d9690f0 238
xshige 0:a0cd4d9690f0 239 #ifdef LIVE_LED
xshige 0:a0cd4d9690f0 240 if(tmr.read() > 0.2) // sec
xshige 0:a0cd4d9690f0 241 {
xshige 0:a0cd4d9690f0 242 led4=!led4; //Show that we are alive
xshige 0:a0cd4d9690f0 243 tmr.reset();
xshige 0:a0cd4d9690f0 244 }
xshige 0:a0cd4d9690f0 245 #endif
xshige 0:a0cd4d9690f0 246 }
xshige 0:a0cd4d9690f0 247 }
xshige 0:a0cd4d9690f0 248 #if 0
xshige 0:a0cd4d9690f0 249 //------------------------------------------------------------------------
xshige 0:a0cd4d9690f0 250 // Test Program (written in processing)
xshige 0:a0cd4d9690f0 251
xshige 0:a0cd4d9690f0 252 /*
xshige 0:a0cd4d9690f0 253 *
xshige 0:a0cd4d9690f0 254 * RPC over TCP(telnet) or
xshige 0:a0cd4d9690f0 255 * Serial RPC (via Serproxy)
xshige 0:a0cd4d9690f0 256 * Test Program
xshige 0:a0cd4d9690f0 257 *
xshige 0:a0cd4d9690f0 258 */
xshige 0:a0cd4d9690f0 259
xshige 0:a0cd4d9690f0 260 import processing.net.*;
xshige 0:a0cd4d9690f0 261
xshige 0:a0cd4d9690f0 262 // please setup remoteIP for yor enviroment
xshige 0:a0cd4d9690f0 263 String remoteIP = "192.168.0.25"; // server IP
xshige 0:a0cd4d9690f0 264 //String remoteIP = "127.0.0.1"; // (Serproxy case)
xshige 0:a0cd4d9690f0 265 int TELNET_PORT = 5335;
xshige 0:a0cd4d9690f0 266
xshige 0:a0cd4d9690f0 267 Client myClient;
xshige 0:a0cd4d9690f0 268 String dataIn;
xshige 0:a0cd4d9690f0 269
xshige 0:a0cd4d9690f0 270 void setup() {
xshige 0:a0cd4d9690f0 271 size(200, 200);
xshige 0:a0cd4d9690f0 272 myClient = new Client(this, remoteIP, TELNET_PORT);
xshige 0:a0cd4d9690f0 273 }
xshige 0:a0cd4d9690f0 274
xshige 0:a0cd4d9690f0 275 void draw() {} // Empty draw keeps the program running
xshige 0:a0cd4d9690f0 276
xshige 0:a0cd4d9690f0 277 /*
xshige 0:a0cd4d9690f0 278 * on key pressed event:
xshige 0:a0cd4d9690f0 279 * pressing a key sends predefined command
xshige 0:a0cd4d9690f0 280 */
xshige 0:a0cd4d9690f0 281 void keyPressed() {
xshige 0:a0cd4d9690f0 282 String cmd="";
xshige 0:a0cd4d9690f0 283 // Real Serial RPC needs terminator("\r\n")
xshige 0:a0cd4d9690f0 284 if (key == '1') cmd="/";
xshige 0:a0cd4d9690f0 285 //
xshige 0:a0cd4d9690f0 286 if (key == '2') cmd="/AnalogIn/new p20 ain";
xshige 0:a0cd4d9690f0 287 if (key == '3') cmd="/ain/read";
xshige 0:a0cd4d9690f0 288 //
xshige 0:a0cd4d9690f0 289 if (key == '4') cmd="/AnalogOut/new p18 aout";
xshige 0:a0cd4d9690f0 290 if (key == '5') cmd="/aout/";
xshige 0:a0cd4d9690f0 291 if (key == '6') cmd="/aout/write_u16 4123";
xshige 0:a0cd4d9690f0 292 // if (key == '4') cmd="/BusIn/new bin p23 p22 p21";
xshige 0:a0cd4d9690f0 293 // if (key == '5') cmd="/bin/read";
xshige 0:a0cd4d9690f0 294 //
xshige 0:a0cd4d9690f0 295 if (key == '8') cmd="/DigitalOut/new LED4 myled4";
xshige 0:a0cd4d9690f0 296 if (key == '9') cmd="/myled4/write 1";
xshige 0:a0cd4d9690f0 297 if (key == '0') cmd="/myled4/write 0";
xshige 0:a0cd4d9690f0 298
xshige 0:a0cd4d9690f0 299 // send the command
xshige 0:a0cd4d9690f0 300 // *** Serial RPC needs terminator("\n") **
xshige 0:a0cd4d9690f0 301 myClient.write(cmd+"\n");
xshige 0:a0cd4d9690f0 302
xshige 0:a0cd4d9690f0 303 }
xshige 0:a0cd4d9690f0 304
xshige 0:a0cd4d9690f0 305 // ClientEvent message is generated when the server
xshige 0:a0cd4d9690f0 306 // sends data to an existing client.
xshige 0:a0cd4d9690f0 307 void clientEvent(Client someClient) {
xshige 0:a0cd4d9690f0 308 if (someClient.available()>0) {
xshige 0:a0cd4d9690f0 309 dataIn = someClient.readString();
xshige 0:a0cd4d9690f0 310 //println(dataIn);
xshige 0:a0cd4d9690f0 311 print(dataIn);
xshige 0:a0cd4d9690f0 312 }
xshige 0:a0cd4d9690f0 313
xshige 0:a0cd4d9690f0 314 }
xshige 0:a0cd4d9690f0 315 #endif
xshige 0:a0cd4d9690f0 316 //------------------------------------------------------------------------
xshige 0:a0cd4d9690f0 317 //------------------------------------------------------------------------
xshige 0:a0cd4d9690f0 318 #if 0
xshige 0:a0cd4d9690f0 319 // TelnetClient.as
xshige 0:a0cd4d9690f0 320
xshige 0:a0cd4d9690f0 321 /*
xshige 0:a0cd4d9690f0 322 *
xshige 0:a0cd4d9690f0 323 * RPC over TCP(telnet) or
xshige 0:a0cd4d9690f0 324 * Serial RPC (via Serproxy)
xshige 0:a0cd4d9690f0 325 * Test Program
xshige 0:a0cd4d9690f0 326 *
xshige 0:a0cd4d9690f0 327 */
xshige 0:a0cd4d9690f0 328
xshige 0:a0cd4d9690f0 329 package {
xshige 0:a0cd4d9690f0 330 import flash.display.Sprite;
xshige 0:a0cd4d9690f0 331 import flash.events.Event;
xshige 0:a0cd4d9690f0 332 import flash.events.IOErrorEvent;
xshige 0:a0cd4d9690f0 333 import flash.events.KeyboardEvent;
xshige 0:a0cd4d9690f0 334 import flash.events.ProgressEvent;
xshige 0:a0cd4d9690f0 335 import flash.events.SecurityErrorEvent;
xshige 0:a0cd4d9690f0 336 import flash.net.Socket;
xshige 0:a0cd4d9690f0 337 import flash.text.*;
xshige 0:a0cd4d9690f0 338 import flash.ui.Keyboard;
xshige 0:a0cd4d9690f0 339
xshige 0:a0cd4d9690f0 340 public class TelnetClient extends Sprite
xshige 0:a0cd4d9690f0 341 {
xshige 0:a0cd4d9690f0 342 // please set your mbed IP
xshige 0:a0cd4d9690f0 343 private var targetIP:String = "192.168.0.25";
xshige 0:a0cd4d9690f0 344 //private var targetIP:String = "127.0.0.1";
xshige 0:a0cd4d9690f0 345 //
xshige 0:a0cd4d9690f0 346 private var targetPort:String = "5335";
xshige 0:a0cd4d9690f0 347 //private var targetPort:String = "5334";
xshige 0:a0cd4d9690f0 348
xshige 0:a0cd4d9690f0 349 private var socket:Socket;
xshige 0:a0cd4d9690f0 350 private var textView:TextField;
xshige 0:a0cd4d9690f0 351 private var textBoard:TextField;
xshige 0:a0cd4d9690f0 352
xshige 0:a0cd4d9690f0 353 public function TelnetClient()
xshige 0:a0cd4d9690f0 354 {
xshige 0:a0cd4d9690f0 355 // setup text field
xshige 0:a0cd4d9690f0 356 textView = addTextField( 5, 10, 640, 340+190 );
xshige 0:a0cd4d9690f0 357 textBoard = addTextField( 5, 340+200, 640, 20 ); // input field
xshige 0:a0cd4d9690f0 358
xshige 0:a0cd4d9690f0 359 // Create a new Socket object and assign event listeners.
xshige 0:a0cd4d9690f0 360 socket = new Socket();
xshige 0:a0cd4d9690f0 361
xshige 0:a0cd4d9690f0 362 socket.addEventListener( Event.CONNECT, onConnect );
xshige 0:a0cd4d9690f0 363 socket.addEventListener( Event.CLOSE, onClose );
xshige 0:a0cd4d9690f0 364 socket.addEventListener( SecurityErrorEvent.SECURITY_ERROR, onSecurtyError );
xshige 0:a0cd4d9690f0 365 socket.addEventListener( IOErrorEvent.IO_ERROR,onIOError );
xshige 0:a0cd4d9690f0 366 socket.addEventListener(ProgressEvent.SOCKET_DATA, onRead );
xshige 0:a0cd4d9690f0 367 addEventListener( KeyboardEvent.KEY_DOWN, onWriteBoard );
xshige 0:a0cd4d9690f0 368
xshige 0:a0cd4d9690f0 369 // Attempt to connect to remote socket server.
xshige 0:a0cd4d9690f0 370 try {
xshige 0:a0cd4d9690f0 371 writeView("trying to connect to " + targetIP + ":" + targetPort +'\n');
xshige 0:a0cd4d9690f0 372 socket.connect(targetIP, parseInt(targetPort));
xshige 0:a0cd4d9690f0 373 } catch (error:Error) {
xshige 0:a0cd4d9690f0 374 /*
xshige 0:a0cd4d9690f0 375 Unable to connect to remote server, display error
xshige 0:a0cd4d9690f0 376 message and close connection.
xshige 0:a0cd4d9690f0 377 */
xshige 0:a0cd4d9690f0 378 writeView(error.message+'\n');
xshige 0:a0cd4d9690f0 379 socket.close();
xshige 0:a0cd4d9690f0 380 }
xshige 0:a0cd4d9690f0 381
xshige 0:a0cd4d9690f0 382 }
xshige 0:a0cd4d9690f0 383
xshige 0:a0cd4d9690f0 384 public function onConnect( event:Event ):void
xshige 0:a0cd4d9690f0 385 {
xshige 0:a0cd4d9690f0 386 writeView( "server connected\n" );
xshige 0:a0cd4d9690f0 387 }
xshige 0:a0cd4d9690f0 388
xshige 0:a0cd4d9690f0 389 public function onClose( event:Event ):void
xshige 0:a0cd4d9690f0 390 {
xshige 0:a0cd4d9690f0 391 writeView( "server close\n" );
xshige 0:a0cd4d9690f0 392 }
xshige 0:a0cd4d9690f0 393
xshige 0:a0cd4d9690f0 394 public function onSecurtyError( ev:Event ):void
xshige 0:a0cd4d9690f0 395 {
xshige 0:a0cd4d9690f0 396 writeView( "Security error.\n" );
xshige 0:a0cd4d9690f0 397 }
xshige 0:a0cd4d9690f0 398
xshige 0:a0cd4d9690f0 399 public function onIOError( ev:Event ):void
xshige 0:a0cd4d9690f0 400 {
xshige 0:a0cd4d9690f0 401 writeView( "IOError.\n" );
xshige 0:a0cd4d9690f0 402 }
xshige 0:a0cd4d9690f0 403
xshige 0:a0cd4d9690f0 404 public function onRead( event:Event ):void
xshige 0:a0cd4d9690f0 405 {
xshige 0:a0cd4d9690f0 406 writeView( socket.readUTFBytes(socket.bytesAvailable ) );
xshige 0:a0cd4d9690f0 407 }
xshige 0:a0cd4d9690f0 408
xshige 0:a0cd4d9690f0 409 public function onWriteBoard( event:KeyboardEvent ):void
xshige 0:a0cd4d9690f0 410 {
xshige 0:a0cd4d9690f0 411 if( !socket.connected ) return;
xshige 0:a0cd4d9690f0 412 if( event.keyCode == Keyboard.ENTER )
xshige 0:a0cd4d9690f0 413 {
xshige 0:a0cd4d9690f0 414 // Shortcut Keys for testing RPC
xshige 0:a0cd4d9690f0 415 if (textBoard.text == "1") textBoard.text = "/";
xshige 0:a0cd4d9690f0 416 if (textBoard.text == "2") textBoard.text = "/AnalogIn/new p20 ain";
xshige 0:a0cd4d9690f0 417 if (textBoard.text == "3") textBoard.text = "/ain/read";
xshige 0:a0cd4d9690f0 418 //
xshige 0:a0cd4d9690f0 419 //if (textBoard.text == "4") textBoard.text = "/AnalogOut/new p18 aout";
xshige 0:a0cd4d9690f0 420 //if (textBoard.text == "5") textBoard.text = "/aout/";
xshige 0:a0cd4d9690f0 421 //if (textBoard.text == "6") textBoard.text = "/aout/write_u16 4123";
xshige 0:a0cd4d9690f0 422 //
xshige 0:a0cd4d9690f0 423 if (textBoard.text == "4") textBoard.text = "/BusOut/new b0 LED1 LED2 LED3 LED4";
xshige 0:a0cd4d9690f0 424 if (textBoard.text == "5") textBoard.text = "/bo/write 13";
xshige 0:a0cd4d9690f0 425 //
xshige 0:a0cd4d9690f0 426 if (textBoard.text == "8") textBoard.text = "/DigitalOut/new LED4 myled4";
xshige 0:a0cd4d9690f0 427 if (textBoard.text == "9") textBoard.text = "/myled4/write 1";
xshige 0:a0cd4d9690f0 428 if (textBoard.text == "0") textBoard.text = "/myled4/write 0";
xshige 0:a0cd4d9690f0 429 //
xshige 0:a0cd4d9690f0 430 socket.writeUTFBytes( textBoard.text + '\n' );
xshige 0:a0cd4d9690f0 431 socket.flush();
xshige 0:a0cd4d9690f0 432 textBoard.text = '';
xshige 0:a0cd4d9690f0 433 }
xshige 0:a0cd4d9690f0 434 }
xshige 0:a0cd4d9690f0 435
xshige 0:a0cd4d9690f0 436 public function writeView( str:String ):void
xshige 0:a0cd4d9690f0 437 {
xshige 0:a0cd4d9690f0 438 if( textView.text == '' )
xshige 0:a0cd4d9690f0 439 {
xshige 0:a0cd4d9690f0 440 textView.text = str;
xshige 0:a0cd4d9690f0 441 }
xshige 0:a0cd4d9690f0 442 else {
xshige 0:a0cd4d9690f0 443 // textView.text = textView.text + '\n' + str;
xshige 0:a0cd4d9690f0 444 // textView.text = textView.text + str;
xshige 0:a0cd4d9690f0 445 textView.appendText(str+'\n'); // serial RPC via serproxy
xshige 0:a0cd4d9690f0 446 }
xshige 0:a0cd4d9690f0 447 //
xshige 0:a0cd4d9690f0 448 textView.scrollV++; // make scroll
xshige 0:a0cd4d9690f0 449 }
xshige 0:a0cd4d9690f0 450
xshige 0:a0cd4d9690f0 451 private function addTextField( x:int, y:int, w:uint,h:uint ):TextField
xshige 0:a0cd4d9690f0 452 {
xshige 0:a0cd4d9690f0 453 var textField:TextField = new TextField();
xshige 0:a0cd4d9690f0 454 addChild( textField );
xshige 0:a0cd4d9690f0 455 textField.x = x;
xshige 0:a0cd4d9690f0 456 textField.y = y;
xshige 0:a0cd4d9690f0 457 textField.width = w;
xshige 0:a0cd4d9690f0 458 textField.height = h;
xshige 0:a0cd4d9690f0 459 textField.text = "";
xshige 0:a0cd4d9690f0 460 textField.selectable = true;
xshige 0:a0cd4d9690f0 461 textField.border = true;
xshige 0:a0cd4d9690f0 462 textField.type = TextFieldType.INPUT;
xshige 0:a0cd4d9690f0 463 textField.wordWrap = true;
xshige 0:a0cd4d9690f0 464 return textField;
xshige 0:a0cd4d9690f0 465 }
xshige 0:a0cd4d9690f0 466
xshige 0:a0cd4d9690f0 467 }
xshige 0:a0cd4d9690f0 468 }
xshige 0:a0cd4d9690f0 469 #endif
xshige 0:a0cd4d9690f0 470 //------------------------------------------------------------------------