|
3 | 3 |
|
4 | 4 | # if sys.platform == "linux": |
5 | 5 | import socket |
| 6 | +import select |
6 | 7 |
|
7 | 8 | # else: |
8 | 9 | import serial |
@@ -64,6 +65,14 @@ def __init__( |
64 | 65 | self.address = address |
65 | 66 | self.serial_speed = serial_speed |
66 | 67 | self.__log = log |
| 68 | + |
| 69 | + self.__serial = None |
| 70 | + self.__socket = None |
| 71 | + self.__num_chs = 0 |
| 72 | + self.__api_mode = 1 |
| 73 | + self.__sample_rate = None |
| 74 | + self.__chs = [None] * 8 |
| 75 | + self.__log = False |
67 | 76 |
|
68 | 77 | # Setup socket in function of com_mode argument |
69 | 78 | self.__setupSocket() |
@@ -284,6 +293,9 @@ def read(self, convert=True, matrix=False): |
284 | 293 | & 0xFFFFFF |
285 | 294 | ) |
286 | 295 | byte_it += 3 |
| 296 | + if convert: |
| 297 | + f.mv[index] = ((f.a[index]) * (3.3*2) / (pow(2, 24) - 1))*1000 |
| 298 | + f.mv[index] = round(f.mv[index], 3) |
287 | 299 |
|
288 | 300 | # If it's an AI channel |
289 | 301 | else: |
@@ -631,12 +643,22 @@ def __recv(self, nrOfBytes, waitall_flag=True): |
631 | 643 | """ |
632 | 644 | Receive data |
633 | 645 | """ |
634 | | - result = None |
| 646 | + result = b"" |
635 | 647 | if self.__socket: |
636 | | - if waitall_flag: |
637 | | - result = self.__socket.recv(nrOfBytes, socket.MSG_WAITALL) |
638 | | - else: |
639 | | - result = self.__socket.recv(nrOfBytes) |
| 648 | + # We have to use a select here with a single socket because we can't apply a timeout in any other way |
| 649 | + ready = select.select([self.__socket], [], [], 5) # 10 seconds timeout |
| 650 | + if ready[0]: |
| 651 | + if waitall_flag: |
| 652 | + remaining = nrOfBytes |
| 653 | + while remaining > 0: |
| 654 | + ready = select.select([self.__socket], [], [], 10) |
| 655 | + if not ready[0]: |
| 656 | + raise ContactingDeviceError() |
| 657 | + temp = self.__socket.recv(remaining) |
| 658 | + result += temp |
| 659 | + remaining = nrOfBytes - len(result) |
| 660 | + else: |
| 661 | + result = self.__socket.recv(nrOfBytes) |
640 | 662 | elif self.__serial: |
641 | 663 | result = self.__serial.read(nrOfBytes) |
642 | 664 | else: |
|
0 commit comments