本文整理汇总了Python中pyb.UART.readinto方法的典型用法代码示例。如果您正苦于以下问题:Python UART.readinto方法的具体用法?Python UART.readinto怎么用?Python UART.readinto使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pyb.UART
的用法示例。
在下文中一共展示了UART.readinto方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: JYMCU
# 需要导入模块: from pyb import UART [as 别名]
# 或者: from pyb.UART import readinto [as 别名]
class JYMCU(object):
"""JY-MCU Bluetooth serial device driver. This is simply a light UART wrapper
with addition AT command methods to customize the device."""
def __init__( self, uart, baudrate ):
""" uart = uart #1-6, baudrate must match what is set on the JY-MCU.
Needs to be a #1-C. """
self._uart = UART(uart, baudrate)
def __del__( self ) : self._uart.deinit()
def any( self ) : return self._uart.any()
def write( self, astring ) : return self._uart.write(astring)
def writechar( self, achar ) : self._uart.writechar(achar)
def read( self, num = None ) : return self._uart.read(num)
def readline( self ) : return self._uart.readline()
def readchar( self ) : return self._uart.readchar()
def readall( self ) : return self._uart.readall()
def readinto( self, buf, count = None ) : return self._uart.readinto(buf, count)
def _cmd( self, cmd ) :
""" Send AT command, wait a bit then return result string. """
self._uart.write("AT+" + cmd)
udelay(500)
return self.readline()
def baudrate( self, rate ) :
""" Set the baud rate. Needs to be #1-C. """
return self._cmd("BAUD" + str(rate))
def name( self, name ) :
""" Set the name to show up on the connecting device. """
return self._cmd("NAME" + name)
def pin( self, pin ) :
""" Set the given 4 digit numeric pin. """
return self._cmd("PIN" + str(pin))
def version( self ) : return self._cmd("VERSION")
def setrepl( self ) : repl_uart(self._uart)
示例2: __init__
# 需要导入模块: from pyb import UART [as 别名]
# 或者: from pyb.UART import readinto [as 别名]
#.........这里部分代码省略.........
rep = {}
rep['Valid Frames'] = self.validSbusFrame
rep['Lost Frames'] = self.lostSbusFrame
rep['Resync Events'] = self.resyncEvent
return rep
def decode_frame(self):
# TODO: DoubleCheck if it has to be removed
for i in range(0, self.SBUS_NUM_CHANNELS - 2):
self.sbusChannels[i] = 0
# counters initialization
byte_in_sbus = 1
bit_in_sbus = 0
ch = 0
bit_in_channel = 0
for i in range(0, 175): # TODO Generalization
if self.sbusFrame[byte_in_sbus] & (1 << bit_in_sbus):
self.sbusChannels[ch] |= (1 << bit_in_channel)
bit_in_sbus += 1
bit_in_channel += 1
if bit_in_sbus == 8:
bit_in_sbus = 0
byte_in_sbus += 1
if bit_in_channel == 11:
bit_in_channel = 0
ch += 1
# Decode Digitals Channels
# Digital Channel 1
if self.sbusFrame[self.SBUS_FRAME_LEN - 2] & (1 << 0):
self.sbusChannels[self.SBUS_NUM_CHAN - 2] = 1
else:
self.sbusChannels[self.SBUS_NUM_CHAN - 2] = 0
# Digital Channel 2
if self.sbusFrame[self.SBUS_FRAME_LEN - 2] & (1 << 1):
self.sbusChannels[self.SBUS_NUM_CHAN - 1] = 1
else:
self.sbusChannels[self.SBUS_NUM_CHAN - 1] = 0
# Failsafe
self.failSafeStatus = self.SBUS_SIGNAL_OK
if self.sbusFrame[self.SBUS_FRAME_LEN - 2] & (1 << 2):
self.failSafeStatus = self.SBUS_SIGNAL_LOST
if self.sbusFrame[self.SBUS_FRAME_LEN - 2] & (1 << 3):
self.failSafeStatus = self.SBUS_SIGNAL_FAILSAFE
def get_sync(self):
if self.sbus.any() > 0:
if self.startByteFound:
if self.frameIndex == (self.SBUS_FRAME_LEN - 1):
self.sbus.readinto(self.sbusBuff, 1) # end of frame byte
if self.sbusBuff[0] == 0: # TODO: Change to use constant var value
self.startByteFound = False
self.isSync = True
self.frameIndex = 0
else:
self.sbus.readinto(self.sbusBuff, 1) # keep reading 1 byte until the end of frame
self.frameIndex += 1
else:
self.frameIndex = 0
self.sbus.readinto(self.sbusBuff, 1) # read 1 byte
if self.sbusBuff[0] == 15: # TODO: Change to use constant var value
self.startByteFound = True
self.frameIndex += 1
def get_new_data(self):
"""
This function must be called periodically according to the specific SBUS implementation in order to update
the channels values.
For FrSky the period is 300us.
"""
if self.isSync:
if self.sbus.any() >= self.SBUS_FRAME_LEN:
self.sbus.readinto(self.sbusFrame, self.SBUS_FRAME_LEN) # read the whole frame
if (self.sbusFrame[0] == 15 and self.sbusFrame[
self.SBUS_FRAME_LEN - 1] == 0): # TODO: Change to use constant var value
self.validSbusFrame += 1
self.outOfSyncCounter = 0
self.decode_frame()
else:
self.lostSbusFrame += 1
self.outOfSyncCounter += 1
if self.outOfSyncCounter > self.OUT_OF_SYNC_THD:
self.isSync = False
self.resyncEvent += 1
else:
self.get_sync()
示例3: __init__
# 需要导入模块: from pyb import UART [as 别名]
# 或者: from pyb.UART import readinto [as 别名]
#.........这里部分代码省略.........
def get_failsafe_status(self):
return self.failSafeStatus
def get_rx_report(self):
rep = {}
rep['Valid Frames'] = self.validSbusFrame
rep['Lost Frames'] = self.lostSbusFrame
rep['Resync Events'] = self.resyncEvent
return rep
def decode_frame(self):
# TODO: DoubleCheck if it has to be removed
for i in range(0, self.SBUS_NUM_CHANNELS - 2):
self.sbusChannels[i] = 0
# counters initialization
byte_in_sbus = 1
bit_in_sbus = 0
ch = 0
bit_in_channel = 0
for i in range(0, 175): # TODO Generalization
if self.sbusFrame[byte_in_sbus] & (1 << bit_in_sbus):
self.sbusChannels[ch] |= (1 << bit_in_channel)
bit_in_sbus += 1
bit_in_channel += 1
if bit_in_sbus == 8:
bit_in_sbus = 0
byte_in_sbus += 1
if bit_in_channel == 11:
bit_in_channel = 0
ch += 1
# Decode Digitals Channels
# Digital Channel 1
if self.sbusFrame[self.SBUS_FRAME_LEN - 2] & (1 << 0):
self.sbusChannels[self.SBUS_NUM_CHAN - 2] = 1
else:
self.sbusChannels[self.SBUS_NUM_CHAN - 2] = 0
# Digital Channel 2
if self.sbusFrame[self.SBUS_FRAME_LEN - 2] & (1 << 1):
self.sbusChannels[self.SBUS_NUM_CHAN - 1] = 1
else:
self.sbusChannels[self.SBUS_NUM_CHAN - 1] = 0
# Failsafe
self.failSafeStatus = self.SBUS_SIGNAL_OK
if self.sbusFrame[self.SBUS_FRAME_LEN - 2] & (1 << 2):
self.failSafeStatus = self.SBUS_SIGNAL_LOST
if self.sbusFrame[self.SBUS_FRAME_LEN - 2] & (1 << 3):
self.failSafeStatus = self.SBUS_SIGNAL_FAILSAFE
def get_sync(self):
if self.sbus.any() > 0:
if self.startByteFound:
if self.frameIndex == (self.SBUS_FRAME_LEN - 1):
self.sbus.readinto(self.sbusBuff, 1) # end of frame byte
if self.sbusBuff[0] == 0: # TODO: Change to use constant var value
self.startByteFound = False
self.isSync = True
self.frameIndex = 0
else:
self.sbus.readinto(self.sbusBuff, 1) # keep reading 1 byte until the end of frame
self.frameIndex += 1
else:
self.frameIndex = 0
self.sbus.readinto(self.sbusBuff, 1) # read 1 byte
if self.sbusBuff[0] == 15: # TODO: Change to use constant var value
self.startByteFound = True
self.frameIndex += 1
def get_new_data(self):
if self.isSync:
if self.sbus.any() >= self.SBUS_FRAME_LEN:
self.sbus.readinto(self.sbusFrame, self.SBUS_FRAME_LEN) # read the whole frame
if (self.sbusFrame[0] == 15 and self.sbusFrame[
self.SBUS_FRAME_LEN - 1] == 0): # TODO: Change to use constant var value
self.validSbusFrame += 1
self.outOfSyncCounter = 0
self.decode_frame()
else:
self.lostSbusFrame += 1
self.outOfSyncCounter += 1
if self.outOfSyncCounter > self.OUT_OF_SYNC_THD:
self.isSync = False
self.resyncEvent += 1
else:
self.get_sync()
示例4: UART
# 需要导入模块: from pyb import UART [as 别名]
# 或者: from pyb.UART import readinto [as 别名]
uart0 = UART(0, 1000000, pins=uart_pins[0][0])
print(uart0)
uart1 = UART(1, 1000000, pins=uart_pins[1][0])
print(uart1)
print(uart0.write(b'123456') == 6)
print(uart1.read() == b'123456')
print(uart1.write(b'123') == 3)
print(uart0.read(1) == b'1')
print(uart0.read(2) == b'23')
print(uart0.read() == b'')
uart0.write(b'123')
buf = bytearray(3)
print(uart1.readinto(buf, 1) == 1)
print(buf)
print(uart1.readinto(buf) == 2)
print(buf)
# try initializing without the id
uart0 = UART(baudrate=1000000, pins=uart_pins[0][0])
uart0.write(b'1234567890')
pyb.delay(2) # because of the fifo interrupt levels
print(uart1.any() == 10)
print(uart1.readline() == b'1234567890')
print(uart1.any() == 0)
uart0.write(b'1234567890')
print(uart1.readall() == b'1234567890')