本文整理汇总了Python中pyb.UART.readchar方法的典型用法代码示例。如果您正苦于以下问题:Python UART.readchar方法的具体用法?Python UART.readchar怎么用?Python UART.readchar使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pyb.UART
的用法示例。
在下文中一共展示了UART.readchar方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from pyb import UART [as 别名]
# 或者: from pyb.UART import readchar [as 别名]
class UART_Port:
"""Implements a port which can send or receive commands with a bioloid
device using the pyboard UART class. This particular class takes
advantage of some features which are only available on the STM32F4xx processors.
"""
def __init__(self, uart_num, baud):
self.uart = UART(uart_num)
self.baud = 0
self.set_baud(baud)
base_str = 'USART{}'.format(uart_num)
if not hasattr(stm, base_str):
base_str = 'UART{}'.format(uart_num)
self.uart_base = getattr(stm, base_str)
# Set HDSEL (bit 3) in CR3 - which puts the UART in half-duplex
# mode. This connects Rx to Tx internally, and only enables the
# transmitter when there is data to send.
stm.mem16[self.uart_base + stm.USART_CR3] |= (1 << 3)
def any(self):
return self.uart.any()
def read_byte(self):
"""Reads a byte from the bus.
This function will return None if no character was read within the
designated timeout (set when we call self.uart.init).
"""
byte = self.uart.readchar()
if byte >= 0:
return byte
def set_baud(self, baud):
"""Sets the baud rate.
Note, the pyb.UART class doesn't have a method for setting the baud
rate, so we need to reinitialize the uart object.
"""
if self.baud != baud:
self.baud = baud
# The max Return Delay Time is 254 * 2 usec = 508 usec. The default
# is 500 usec. So using a timeout of 2 ensures that we wait for
# at least 1 msec before considering a timeout.
self.uart.init(baudrate=baud, timeout=2)
def write_packet(self, packet_data):
"""Writes an entire packet to the serial port."""
_write_packet(self.uart_base, packet_data, len(packet_data))
示例2: JYMCU
# 需要导入模块: from pyb import UART [as 别名]
# 或者: from pyb.UART import readchar [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)
示例3: UART
# 需要导入模块: from pyb import UART [as 别名]
# 或者: from pyb.UART import readchar [as 别名]
from pyb import UART
uart = UART(6, 115200) # init with given baudrate
cmdStr = ""
for i in range(1000):
uart.write("hello ")
pyb.delay(100)
if uart.any() > 0:
for i in range(uart.any()):
ch = uart.readchar()
if ch == 0x0d:
print("Command is:", cmdStr)
cmdStr = ""
continue
if ch == 0x0a:
continue
cmdStr += chr(ch)
print (chr(ch))
示例4: SensorQueue
# 需要导入模块: from pyb import UART [as 别名]
# 或者: from pyb.UART import readchar [as 别名]
indicator = pyb.LED(4)
increase = True
sensor_queue = SensorQueue(tmp36,
mcp9808,
accel,
gps
)
command_pool = CommandPool(servo1, motor_a)
communicator = Communicator(sensor_queue, command_pool)
while True:
if new_data:
while uart.any():
gps.update(chr(uart.readchar()))
new_data = False
communicator.write_packet()
communicator.read_command()
if increase:
indicator.intensity(indicator.intensity() + 5)
else:
indicator.intensity(indicator.intensity() - 5)
if indicator.intensity() <= 0:
increase = True
elif indicator.intensity() >= 255:
increase = False
示例5: UART
# 需要导入模块: from pyb import UART [as 别名]
# 或者: from pyb.UART import readchar [as 别名]
xbee_uart = UART(2, 9600)
Razor_IMU = IMU.Razor(3, 57600)
# Razor_IMU.set_angle_output()
Razor_IMU.set_all_calibrated_output()
# while True:
# a,b,c = Razor_IMU.get_one_frame()
# print(a,b,c)
# Countdown Timer
start = pyb.millis()
backup_timer = 5400000
# Don't do anything until GPS is found
while gps_uart.any() >= 0:
my_gps.update(chr(gps_uart.readchar()))
print("No GPS signal!!!\n")
print(my_gps.latitude)
if my_gps.latitude[0] != 0:
init_lat = convert_latitude(my_gps.latitude)
init_long = convert_longitude(my_gps.longitude)
initial_point = (init_lat, init_long)
print("Initial Point: {}".format(initial_point))
break
# initial_point = (40.870242, -119.106354)
# while True:
# print('not landed yet')
# if pyb.elapsed_millis(start) >= backup_timer: #This is 90 minutes
# break
示例6: UART
# 需要导入模块: from pyb import UART [as 别名]
# 或者: from pyb.UART import readchar [as 别名]
from pyb import UART
# Setup the connection to your GPS here
# This example uses UART 3 with RX on pin Y10
# Baudrate is 9600bps, with the standard 8 bits, 1 stop bit, no parity
uart = UART(3, 9600)
# Basic UART --> terminal printer, use to test your GPS module
while True:
if uart.any():
print(chr(uart.readchar()), end='')
示例7: port
# 需要导入模块: from pyb import UART [as 别名]
# 或者: from pyb.UART import readchar [as 别名]
class Receiver:
# Allowed System Field values
_DSM2_1024_22MS = 0x01
_DSM2_2048_11MS = 0x12
_DSMS_2048_22MS = 0xa2
_DSMX_2048_11MS = 0xb2
_SYSTEM_FIELD_VALUES = (_DSM2_1024_22MS, _DSM2_2048_11MS, _DSMS_2048_22MS, _DSMX_2048_11MS)
# Channel formats depending on system field value
_MASK_1024_CHANID = 0xFC00 # when 11ms
_MASK_1024_SXPOS = 0x03FF # when 11ms
_MASK_2048_CHANID = 0x7800 # when 22ms
_MASK_2048_SXPOS = 0x07FF # when 22ms
# Serial configuration
_uart = None
_uart_port = 0 # Which UART port to use?
_uart_speed = 0 # Which UART speed to use?
# Serial buffer and frame data
_system_field = None
_frame = [0] * 16 # Assumption: frames are received correctly, no need of intermediate buffer and controls
_channels = [0] * 20 # Up-to 20 channels can be used by SPM4648
_debug = False
# ########################################################################
# ### Properties
# ########################################################################
@property
def port(self):
return self._uart_port
@property
def speed(self):
return self._uart_speed
@property
def frame(self):
return self._frame
@property
def channels(self):
return self._channels
@property
def system_field(self):
return self._system_field
# ########################################################################
# ### Constructor and destructor
# ########################################################################
def __init__(self, port, speed, debug=False):
self._debug = debug
self._uart_port = port
self._uart_speed = speed
self._uart = UART(self._uart_port, self._uart_speed)
# ########################################################################
# ### Functions
# ########################################################################
def read_serial(self):
# Lire un frame
if self._uart.any():
index = 0
while index < 16:
self._frame[index] = self._uart.readchar()
index += 1
self._decode_frame()
return True
else:
return False
def _decode_frame(self):
# Verify the system field (_channels[2])
if self._frame[1] in self._SYSTEM_FIELD_VALUES:
self._system_field = self._frame[1]
if self._frame[1] == self._DSM2_1024_22MS:
for i in range(1, 7):
data = self._frame[i * 2] * 256 + self._frame[(i * 2) + 1]
channel = (data & self._MASK_1024_CHANID) >> 10
value = data & self._MASK_1024_SXPOS
self._channels[channel] = value
else:
for i in range(1, 7):
data = self._frame[i * 2] * 256 + self._frame[(i * 2) + 1]
channel = (data & self._MASK_2048_CHANID) >> 11
value = data & self._MASK_2048_SXPOS
self._channels[channel] = value
else:
pass # Invalid system field value -> Do nothing
if self._debug:
self.debug()
def debug(self):
if not self._debug:
return
print("RX OUT: ", end="")
#.........这里部分代码省略.........
示例8: UART
# 需要导入模块: from pyb import UART [as 别名]
# 或者: from pyb.UART import readchar [as 别名]
# micropyGPS Sentence Test
# When properly connected to working GPS module,
# will print the names of the sentences it receives
# If you are having issues receiving sentences, use UART_test.py to ensure
# your UART is hooked up and configured correctly
from pyb import UART
from micropyGPS import MicropyGPS
# Setup the connection to your GPS here
# This example uses UART 3 with RX on pin Y10
# Baudrate is 9600bps, with the standard 8 bits, 1 stop bit, no parity
uart = UART(3, 9600)
# Instatntiate the micropyGPS object
my_gps = MicropyGPS()
# Continuous Tests for characters available in the UART buffer, any characters are feed into the GPS
# object. When enough char are feed to represent a whole, valid sentence, stat is set as the name of the
# sentence and printed
while True:
if uart.any():
stat = my_gps.update(chr(uart.readchar())) # Note the conversion to to chr, UART outputs ints normally
if stat:
print(stat)
stat = None
示例9: UART
# 需要导入模块: from pyb import UART [as 别名]
# 或者: from pyb.UART import readchar [as 别名]
from pyb import UART
from micropyGPS import MicropyGPS
uart = UART(3, 9600)
my_gps = MicropyGPS()
# Reads 300 sentences and reports how many were parsed and if any failed the CRC check
sentence_count = 0
while True:
if uart.any():
stat = my_gps.update(chr(uart.readchar()))
if stat:
print(stat)
stat = None
sentence_count += 1
if sentence_count == 300:
break;
print('Sentences Found:', my_gps.clean_sentences)
print('Sentences Parsed:', my_gps.parsed_sentences)
print('CRC_Fails:', my_gps.crc_fails)
示例10: sleep
# 需要导入模块: from pyb import UART [as 别名]
# 或者: from pyb.UART import readchar [as 别名]
sleep(0.4)
blue.toggle()
sleep(0.4)
#X second loop, get data every half second and write to backup.csv, and also transmit to ground station
for tag in range(1,61):
green.off()
temp = bmp180.temperature
pres = bmp180.pressure
alt = bmp180.altitude
#if there is gps data to be read
while uart.any():
my_sentence = chr(uart.readchar())
for x in my_sentence:
my_gps.update(x)
latitude = my_gps.latitude_string()
longitude = my_gps.longitude_string()
timestamp = my_gps.timestamp
#alt2 = my_gps.altitude
#open backup.csv to write data to, write to it, then close it
backup = open('/sd/backup.csv', 'a')
backup.write('{},{},{},{},{},{},{}\n'.format(tag,timestamp,temp,pres,alt,latitude,longitude))
backup.close()
data = str(tag) + ',' + str(temp) + ',' + str(pres) + ',' + str(alt) + ',' + str(latitude) + ',' + str(longitude) #concatenate data with commas