connection.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #!/bin/python3
  2. import sys
  3. import time
  4. import math
  5. import socket
  6. import serial
  7. N_ROWS = 5
  8. N_COLS = 10
  9. class SerialConnection:
  10. def __init__(self):
  11. self._ser = None
  12. self.port = None
  13. def open(self, port = None):
  14. self.port = port
  15. if self.port == None:
  16. for port in ["/dev/ttyUSB{}".format(p) for p in range(4)]:
  17. try:
  18. self._ser = serial.Serial(port, 38400)
  19. print("connected to " + port)
  20. self.port = port
  21. break
  22. except serial.SerialException:
  23. pass
  24. if self.port == None:
  25. for port in ["COM{}".format(p) for p in range(3,20)]:
  26. try:
  27. self._ser = serial.Serial(port, 38400)
  28. print("connected to " + port)
  29. self.port = port
  30. break
  31. except serial.SerialException:
  32. pass
  33. else:
  34. try:
  35. self._ser = serial.Serial(self.port, 38400)
  36. print("connected to " + self.port)
  37. except serial.SerialException:
  38. pass
  39. if self._ser:
  40. self._ser.write(b'A' + b'\15' * N_ROWS*N_COLS)
  41. else:
  42. print("connection to port {} failed".format(port))
  43. def send(self, bytes):
  44. if self._ser:
  45. try:
  46. return self._ser.write(bytes)
  47. except serial.SerialException:
  48. self._ser = None
  49. if not self._ser:
  50. self.open()
  51. if not self._ser:
  52. time.sleep(30)
  53. def read(self, n=64):
  54. if self._ser:
  55. try:
  56. if self._ser.in_waiting:
  57. return self._ser.read(n)
  58. except serial.SerialException:
  59. self._ser = None
  60. if not self._ser:
  61. self.open()
  62. def isConnected(self):
  63. return self._ser != None
  64. def close(self):
  65. if self._ser:
  66. self._ser.close()
  67. class IPConnection:
  68. def __init__(self):
  69. self.oldPing = time.time()
  70. self._sock = None
  71. self.server_address = None
  72. self.client_address = None
  73. def open(self, port = 2020):
  74. self._sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  75. self.server_address = ('0.0.0.0', port)
  76. print('binding {} port {}'.format(*self.server_address))
  77. self._sock.bind(self.server_address)
  78. self._sock.settimeout(30)
  79. def send(self, bytes):
  80. if not client_address:
  81. try:
  82. print('waiting...')
  83. data, addr = self._sock.recvfrom(16)
  84. print('new connection from {}'.format(data, addr))
  85. client_address = addr
  86. except socket.timeout:
  87. pass
  88. else:
  89. self._sock.sendto(bytes, client_address)
  90. if time.time() - oldPing > 30:
  91. try:
  92. data, addr = self._sock.recvfrom(16)
  93. print('received {} from {}'.format(data, addr))
  94. client_address = addr
  95. oldPing = time.time()
  96. except socket.timeout:
  97. client_address = None
  98. def isConnected(self):
  99. return self.client_address != None
  100. def close(self):
  101. self._sock.close()