Permalink
Browse files

Fixes #17 adds socket support (#19)

* Fixes #17 adds socket support

* better check for socket URL

* remove baudrate arg from create_serial_connection
  • Loading branch information...
iiSeymour authored and rob-smallshire committed Jan 22, 2017
1 parent 6fe7415 commit b9c2bd946be6364bb709818c6fb06e5c27394524
Showing with 25 additions and 11 deletions.
  1. +1 −1 .travis.yml
  2. +1 −0 CREDITS.rst
  3. +5 −5 serial_asyncio/__init__.py
  4. +18 −5 test/test_asyncio.py
View
@@ -12,4 +12,4 @@ install:
- pip install -e .
script:
- python test/test_asyncio.py loop://
- python test/test_asyncio.py
View
@@ -15,6 +15,7 @@ Contributors
- David Ko
- Nicolas Di Pietro
- jabdoa2
- Chris Seymour
- ... not all names may be listed here, see also ``git log`` or online history_
View
@@ -294,28 +294,28 @@ def _ensure_writer(self):
def _remove_writer(self):
if self._has_writer:
self._loop.remove_writer(self._serial.fd)
self._loop.remove_writer(self._serial.fileno())
self._has_writer = False
else:
def _ensure_reader(self):
if (not self._has_reader) and (not self._closing):
self._loop.add_reader(self._serial.fd, self._read_ready)
self._loop.add_reader(self._serial.fileno(), self._read_ready)
self._has_reader = True
def _remove_reader(self):
if self._has_reader:
self._loop.remove_reader(self._serial.fd)
self._loop.remove_reader(self._serial.fileno())
self._has_reader = False
def _ensure_writer(self):
if (not self._has_writer) and (not self._closing):
self._loop.add_writer(self._serial.fd, self._write_ready)
self._loop.add_writer(self._serial.fileno(), self._write_ready)
self._has_writer = True
def _remove_writer(self):
if self._has_writer:
self._loop.remove_writer(self._serial.fd)
self._loop.remove_writer(self._serial.fileno())
self._has_writer = False
def _set_write_buffer_limits(self, high=None, low=None):
View
@@ -21,8 +21,12 @@
import serial_asyncio
HOST = '127.0.0.1'
_PORT = 8888
# on which port should the tests be performed:
PORT = '/dev/ttyUSB0'
PORT = 'socket://%s:%s' % (HOST, _PORT)
@unittest.skipIf(os.name != 'posix', "asyncio not supported on platform")
class Test_asyncio(unittest.TestCase):
@@ -40,15 +44,20 @@ def test_asyncio(self):
received = []
actions = []
class Input(asyncio.Protocol):
def connection_made(self, transport):
self.transport = transport
def data_received(self, data):
self.transport.write(data)
class Output(asyncio.Protocol):
def connection_made(self, transport):
self.transport = transport
actions.append('open')
transport.serial.rts = False
transport.write(TEXT)
def data_received(self, data):
#~ print('data received', repr(data))
received.append(data)
if b'\n' in data:
self.transport.close()
@@ -65,8 +74,12 @@ def resume_writing(self):
actions.append('resume')
print(self.transport.get_write_buffer_size())
coro = serial_asyncio.create_serial_connection(self.loop, Output, PORT, baudrate=115200)
self.loop.run_until_complete(coro)
if PORT.startswith('socket://'):
coro = self.loop.create_server(Input, HOST, _PORT)
self.loop.run_until_complete(coro)
client = serial_asyncio.create_serial_connection(self.loop, Output, PORT)
self.loop.run_until_complete(client)
self.loop.run_forever()
self.assertEqual(b''.join(received), TEXT)
self.assertEqual(actions, ['open', 'close'])

0 comments on commit b9c2bd9

Please sign in to comment.