From 88e0aae16d765a9463c05faee8c0a7dd53cce597 Mon Sep 17 00:00:00 2001 From: Sebastien Bourdeauducq Date: Fri, 10 Apr 2015 01:22:03 +0800 Subject: [PATCH] coredevice: add comm_tcp --- artiq/coredevice/comm_tcp.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 artiq/coredevice/comm_tcp.py diff --git a/artiq/coredevice/comm_tcp.py b/artiq/coredevice/comm_tcp.py new file mode 100644 index 000000000..d2c9367e2 --- /dev/null +++ b/artiq/coredevice/comm_tcp.py @@ -0,0 +1,36 @@ +import logging +import socket + +from artiq.coredevice.comm_generic import CommGeneric +from artiq.language.db import * + + +logger = logging.getLogger(__name__) + + +class Comm(CommGeneric, AutoDB): + class DBKeys: + host = Argument() + port = Argument(1381) + + def open(self): + if hasattr(self, "socket"): + return + self.socket = socket.create_connection((self.host, self.port)) + logger.debug("connected to host %s on port %d", self.host, self.port) + + def close(self): + if not hasattr(self, "socket"): + return + self.socket.close() + del self.socket + logger.debug("disconnected") + + def read(self, length): + r = bytes() + while len(r) < length: + r += self.socket.recv(min(8192, length - len(r))) + return r + + def write(self, data): + self.socket.sendall(data)