Skip to content

Commit

Permalink
Fixes #17 adds socket support (#19)
Browse files Browse the repository at this point in the history
* 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 b9c2bd9
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ install:
- pip install -e .

script:
- python test/test_asyncio.py loop://
- python test/test_asyncio.py
1 change: 1 addition & 0 deletions CREDITS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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_


Expand Down
10 changes: 5 additions & 5 deletions serial_asyncio/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
23 changes: 18 additions & 5 deletions test/test_asyncio.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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()
Expand All @@ -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'])
Expand Down

0 comments on commit b9c2bd9

Please sign in to comment.