-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from Galvant/dev
Pull from upstream
- Loading branch information
Showing
7 changed files
with
310 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
from instruments.abstract_instruments.wrapperabc import WrapperABC | ||
from instruments.abstract_instruments.instrument import Instrument | ||
from instruments.abstract_instruments.multimeter import Multimeter | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
python/src/instruments/abstract_instruments/serialwrapper.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
#!/usr/bin/python | ||
# -*- coding: utf-8 -*- | ||
## | ||
# socketwrapper.py: Wraps sockets into a filelike object. | ||
## | ||
# © 2013 Steven Casagrande ([email protected]). | ||
# | ||
# This file is a part of the GPIBUSB adapter project. | ||
# Licensed under the AGPL version 3. | ||
## | ||
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU Affero General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU Affero General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU Affero General Public License | ||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
## | ||
## | ||
|
||
## IMPORTS ##################################################################### | ||
|
||
import io | ||
import serial | ||
|
||
import numpy as np | ||
|
||
from instruments.abstract_instruments import WrapperABC | ||
|
||
## CLASSES ##################################################################### | ||
|
||
class SerialWrapper(io.IOBase, WrapperABC): | ||
""" | ||
Wraps a pyserial Serial object to add a few properties as well as | ||
handling of termination characters. | ||
""" | ||
|
||
def __init__(self, conn): | ||
if isinstance(conn, serial.Serial): | ||
self._conn = conn | ||
self._terminator = '\n' | ||
else: | ||
raise TypeError('SerialWrapper must wrap a serial.Serial object.') | ||
|
||
def __repr__(self): | ||
return "<SerialWrapper object at 0x{:X} "\ | ||
"connected to {}>".format(id(self), self._conn.port) | ||
|
||
## PROPERTIES ## | ||
|
||
@property | ||
def address(self): | ||
return self._conn.port | ||
@address.setter | ||
def address(self, newval): | ||
# TODO: Input checking on Serial port newval | ||
# TODO: Add port changing capability to serialmanager | ||
# self._conn.port = newval | ||
raise NotImplementedError | ||
|
||
@property | ||
def terminator(self): | ||
return self._terminator | ||
@terminator.setter | ||
def terminator(self, newval): | ||
if not isinstance(newval, str): | ||
raise TypeError('Terminator for SerialWrapper must be specified ' | ||
'as a single character string.') | ||
if len(newval) > 1: | ||
raise ValueError('Terminator for SerialWrapper must only be 1 ' | ||
'character long.') | ||
self._terminator = newval | ||
|
||
## FILE-LIKE METHODS ## | ||
|
||
def close(self): | ||
try: | ||
self._conn.shutdown() | ||
finally: | ||
self._conn.close() | ||
|
||
def read(self, size): | ||
if (size >= 0): | ||
return self._conn.recv(size) | ||
elif (size == -1): | ||
result = np.bytearray() | ||
c = 0 | ||
while c != self._terminator: | ||
c = self._file.read(1) | ||
result += c | ||
return bytes(result) | ||
else: | ||
raise ValueError('Must read a positive value of characters.') | ||
|
||
def write(self, string): | ||
self._conn.sendall(string + self._terminator) | ||
|
||
def seek(self, offset): | ||
return NotImplemented | ||
|
||
def tell(self): | ||
return NotImplemented |
Oops, something went wrong.