Commits

Takayuki KONDO committed ebb227c

serial recv/send サンプル追加(WIP)

  • Participants
  • Parent commits b0243ca

Comments (0)

Files changed (2)

File py/serial/recv.py

+#!/usr/bin/env python
+
+from serial import Serial
+import sys
+
+class DataRecv:
+    com = None
+    def __init__(self,baudrate=115200,bytesize=8,parity='N',stopbits=1):
+        self.com = Serial(
+            port=13,
+            baudrate=baudrate,
+            bytesize=bytesize,
+            parity=parity,
+            stopbits=stopbits,
+            timeout=None,
+            )
+
+    def readandwrite(self, length):
+        msg = self.com.read(length)
+        return len(msg), msg
+
+    def write(self, msg):
+        size = self.com.write(msg)
+        return size, msg
+
+    def close(self):
+        self.com.close()
+
+def usage():
+    print "Usage: recv.py"
+
+if __name__ == '__main__':
+    if len(sys.argv) <= 1:
+        usage()
+        exit()
+
+    count = int(sys.argv[1])
+    com = DataRecv()
+
+    for n in range(count):
+        size , msg = com.readandwrite();
+        print size, msg
+    com.close();

File py/serial/send.py

+#!/usr/bin/env python
+from serial import Serial
+import sys
+
+class DataSend:
+    com = None
+    msg = ""
+    def __init__(self, msg="abc\r\n"):
+        self.com = Serial(
+            port=12,
+            baudrate=115200,
+            bytesize=8,
+            parity='N',
+            stopbits=1,
+            timeout=None,
+            )
+        self.msg = msg
+
+    def _write(self, msg):
+        size = self.com.write(msg)
+        return size, msg
+
+    def _read(self, length):
+        self.com.read(length)
+
+    def writeandread(self):
+        sendmsg = self.msg
+        recvmsg = ""
+        self._write():
+        recvmsg = self._read(len(self.msg)):
+        
+
+    def close(self):
+        self.com.close()
+
+def usage():
+    print "Usage: send.py count"
+
+
+if __name__ == '__main__':
+    if len(sys.argv) <= 1:
+        usage()
+        exit()
+
+    count = int(sys.argv[1])
+    com = DataSend('abcdef\r\n')
+
+    for n in range(count):
+        size , msg = com.write();
+        print "write:", size, msg
+    com.close();