wiki:epics/streamdevice/sim

テスト用スクリプト

stream device の試験をするときに、切り分けるためにソフトウェアで tcp server があると便利。 Echo Server のサンプルをベースにした書きなぐりのスクリプトですが、誰かの役に立つかもしれないので掲載しておく。

コマンドや応答を適当に書き換えていただければ使えると思います。

import SocketServer
import string
import sys
import time
import signal
import random

HOST = ''
PORT = 9999

class EchoHandler(SocketServer.BaseRequestHandler):
    def handle(self):
        print time.ctime(time.time()), "  Connected from ", self.client_address

        flag = 1
        fid = self.request.makefile()
        while flag:
            receivedData = fid.readline()
            if not receivedData: break
            comd = string.strip(receivedData)
            ret = string.upper(string.strip(comd))
            if ret=='QUIT':
                flag = 0
            elif ret=='CURRENT?':
                buf = self.creRetStr() + "\r\n"
                self.request.send(buf)
            elif ret=='*IDN?':
                buf = "Simulator\r\n"
                self.request.send(buf)
            else:
                ret = ret + '\r\n'
                self.request.send(ret)
        fid.close()
        self.request.close()
        print time.ctime(time.time()), "  Disconnected (from ", self.client_address, ")"

    def creRetStr(self):
        random.seed()
        val = (random.random()-0.5)*1000
        #buf = "A=%8.4f, B=%8.4f, C=%8.4f"%(val,val,val2*2.345)
        if val>0:
          buf = "%8.4f"%val
        else:
          buf = "ERROR:%f"%val

        return buf
# end of class EchoHandler

def signal_handler(signal, frame):
    sys.exit(0)

# __ main __
if __name__ == '__main__':
    signal.signal(signal.SIGINT, signal_handler)
    SocketServer.ForkingTCPServer.allow_reuse_address = True
    srv = SocketServer.ForkingTCPServer((HOST,PORT), EchoHandler)
    print "StreamDevice Simulator started."
    srv.serve_forever()
Last modified 10 years ago Last modified on 08/26/14 13:45:20