mbed-os

Dependents:   cobaLCDJoyMotor_Thread odometry_omni_3roda_v3 odometry_omni_3roda_v1 odometry_omni_3roda_v2 ... more

Committer:
be_bryan
Date:
Mon Dec 11 17:54:04 2017 +0000
Revision:
0:b74591d5ab33
motor ++

Who changed what in which revision?

UserRevisionLine numberNew contents of line
be_bryan 0:b74591d5ab33 1 """
be_bryan 0:b74591d5ab33 2 mbed SDK
be_bryan 0:b74591d5ab33 3 Copyright (c) 2011-2013 ARM Limited
be_bryan 0:b74591d5ab33 4
be_bryan 0:b74591d5ab33 5 Licensed under the Apache License, Version 2.0 (the "License");
be_bryan 0:b74591d5ab33 6 you may not use this file except in compliance with the License.
be_bryan 0:b74591d5ab33 7 You may obtain a copy of the License at
be_bryan 0:b74591d5ab33 8
be_bryan 0:b74591d5ab33 9 http://www.apache.org/licenses/LICENSE-2.0
be_bryan 0:b74591d5ab33 10
be_bryan 0:b74591d5ab33 11 Unless required by applicable law or agreed to in writing, software
be_bryan 0:b74591d5ab33 12 distributed under the License is distributed on an "AS IS" BASIS,
be_bryan 0:b74591d5ab33 13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
be_bryan 0:b74591d5ab33 14 See the License for the specific language governing permissions and
be_bryan 0:b74591d5ab33 15 limitations under the License.
be_bryan 0:b74591d5ab33 16 """
be_bryan 0:b74591d5ab33 17
be_bryan 0:b74591d5ab33 18 import sys
be_bryan 0:b74591d5ab33 19 import socket
be_bryan 0:b74591d5ab33 20 from sys import stdout
be_bryan 0:b74591d5ab33 21 from SocketServer import BaseRequestHandler, TCPServer
be_bryan 0:b74591d5ab33 22
be_bryan 0:b74591d5ab33 23 class TCPEchoClient_Handler(BaseRequestHandler):
be_bryan 0:b74591d5ab33 24 def handle(self):
be_bryan 0:b74591d5ab33 25 """ One handle per connection
be_bryan 0:b74591d5ab33 26 """
be_bryan 0:b74591d5ab33 27 print "HOST: Connection received...",
be_bryan 0:b74591d5ab33 28 count = 1;
be_bryan 0:b74591d5ab33 29 while True:
be_bryan 0:b74591d5ab33 30 data = self.request.recv(1024)
be_bryan 0:b74591d5ab33 31 if not data: break
be_bryan 0:b74591d5ab33 32 self.request.sendall(data)
be_bryan 0:b74591d5ab33 33 if '{{end}}' in str(data):
be_bryan 0:b74591d5ab33 34 print
be_bryan 0:b74591d5ab33 35 print str(data)
be_bryan 0:b74591d5ab33 36 else:
be_bryan 0:b74591d5ab33 37 if not count % 10:
be_bryan 0:b74591d5ab33 38 sys.stdout.write('.')
be_bryan 0:b74591d5ab33 39 count += 1
be_bryan 0:b74591d5ab33 40 stdout.flush()
be_bryan 0:b74591d5ab33 41
be_bryan 0:b74591d5ab33 42 class TCPEchoClientTest():
be_bryan 0:b74591d5ab33 43 def send_server_ip_port(self, selftest, ip_address, port_no):
be_bryan 0:b74591d5ab33 44 """ Set up network host. Reset target and and send server IP via serial to Mbed
be_bryan 0:b74591d5ab33 45 """
be_bryan 0:b74591d5ab33 46 c = selftest.mbed.serial_readline() # 'TCPCllient waiting for server IP and port...'
be_bryan 0:b74591d5ab33 47 if c is None:
be_bryan 0:b74591d5ab33 48 self.print_result(selftest.RESULT_IO_SERIAL)
be_bryan 0:b74591d5ab33 49 return
be_bryan 0:b74591d5ab33 50
be_bryan 0:b74591d5ab33 51 selftest.notify(c.strip())
be_bryan 0:b74591d5ab33 52 selftest.notify("HOST: Sending server IP Address to target...")
be_bryan 0:b74591d5ab33 53
be_bryan 0:b74591d5ab33 54 connection_str = ip_address + ":" + str(port_no) + "\n"
be_bryan 0:b74591d5ab33 55 selftest.mbed.serial_write(connection_str)
be_bryan 0:b74591d5ab33 56 selftest.notify(connection_str)
be_bryan 0:b74591d5ab33 57
be_bryan 0:b74591d5ab33 58 # Two more strings about connection should be sent by MBED
be_bryan 0:b74591d5ab33 59 for i in range(0, 2):
be_bryan 0:b74591d5ab33 60 c = selftest.mbed.serial_readline()
be_bryan 0:b74591d5ab33 61 if c is None:
be_bryan 0:b74591d5ab33 62 selftest.print_result(self.RESULT_IO_SERIAL)
be_bryan 0:b74591d5ab33 63 return
be_bryan 0:b74591d5ab33 64 selftest.notify(c.strip())
be_bryan 0:b74591d5ab33 65
be_bryan 0:b74591d5ab33 66 def test(self, selftest):
be_bryan 0:b74591d5ab33 67 # We need to discover SERVEP_IP and set up SERVER_PORT
be_bryan 0:b74591d5ab33 68 # Note: Port 7 is Echo Protocol:
be_bryan 0:b74591d5ab33 69 #
be_bryan 0:b74591d5ab33 70 # Port number rationale:
be_bryan 0:b74591d5ab33 71 #
be_bryan 0:b74591d5ab33 72 # The Echo Protocol is a service in the Internet Protocol Suite defined
be_bryan 0:b74591d5ab33 73 # in RFC 862. It was originally proposed for testing and measurement
be_bryan 0:b74591d5ab33 74 # of round-trip times[citation needed] in IP networks.
be_bryan 0:b74591d5ab33 75 #
be_bryan 0:b74591d5ab33 76 # A host may connect to a server that supports the Echo Protocol using
be_bryan 0:b74591d5ab33 77 # the Transmission Control Protocol (TCP) or the User Datagram Protocol
be_bryan 0:b74591d5ab33 78 # (UDP) on the well-known port number 7. The server sends back an
be_bryan 0:b74591d5ab33 79 # identical copy of the data it received.
be_bryan 0:b74591d5ab33 80 SERVER_IP = str(socket.gethostbyname(socket.getfqdn()))
be_bryan 0:b74591d5ab33 81 SERVER_PORT = 7
be_bryan 0:b74591d5ab33 82
be_bryan 0:b74591d5ab33 83 # Returning none will suppress host test from printing success code
be_bryan 0:b74591d5ab33 84 server = TCPServer((SERVER_IP, SERVER_PORT), TCPEchoClient_Handler)
be_bryan 0:b74591d5ab33 85 print "HOST: Listening for TCP connections: " + SERVER_IP + ":" + str(SERVER_PORT)
be_bryan 0:b74591d5ab33 86 self.send_server_ip_port(selftest, SERVER_IP, SERVER_PORT)
be_bryan 0:b74591d5ab33 87 server.serve_forever()