Last active
September 21, 2018 06:15
-
-
Save pszafer/af7ce3c48890fdbd786f291330651f97 to your computer and use it in GitHub Desktop.
TEST Espon connection with serial asyncio
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
import asyncio | |
import serial_asyncio | |
class Output(asyncio.Protocol): | |
def connection_made(self, transport): | |
self.transport = transport | |
print('port opened', transport) | |
transport.serial.rts = False | |
transport.write(b'PWR?\r') | |
def data_received(self, data): | |
print('data received', repr(data)) | |
if b'\r' in data: | |
self.transport.close() | |
def connection_lost(self, exc): | |
print('port closed') | |
asyncio.get_event_loop().stop() | |
def pause_writing(self): | |
print('pause writing') | |
print(self.transport.get_write_buffer_size()) | |
def resume_writing(self): | |
print(self.transport.get_write_buffer_size()) | |
print('resume writing') | |
loop = asyncio.get_event_loop() | |
coro = serial_asyncio.create_serial_connection(loop, Output, '/dev/ttyUSB1', baudrate=9600) | |
loop.run_until_complete(coro) | |
loop.run_forever() | |
loop.close() |
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
import asyncio | |
import serial_asyncio | |
class Output(asyncio.Protocol): | |
def connection_made(self, transport): | |
self.transport = transport | |
print('port opened', transport) | |
transport.serial.rts = False | |
transport.write(b'PWR?\n') | |
def data_received(self, data): | |
print('data received', repr(data)) | |
if b'\n' in data: | |
self.transport.close() | |
def connection_lost(self, exc): | |
print('port closed') | |
asyncio.get_event_loop().stop() | |
def pause_writing(self): | |
print('pause writing') | |
print(self.transport.get_write_buffer_size()) | |
def resume_writing(self): | |
print(self.transport.get_write_buffer_size()) | |
print('resume writing') | |
loop = asyncio.get_event_loop() | |
coro = serial_asyncio.create_serial_connection(loop, Output, '/dev/ttyUSB1', baudrate=9600) | |
loop.run_until_complete(coro) | |
loop.run_forever() | |
loop.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment