class WIFI: """docstring for wifi""" def __init__(self, uart, baudrate=115200): """ uart = uart #1-6, baudrate must match what is set on the ESP8266. """ self._uart = UART(uart, baudrate) def write(self, aMsg): self._uart.write(aMsg) res = self._uart.readall() if res: print(res.decode("utf-8")) def read(self): return self._uart.readall().decode("utf-8") def _cmd(self, cmd): """ Send AT command, wait a bit then return results. """ self._uart.write("AT+" + cmd + "\r\n") udelay(500) return self.read() @property def IP(self): return self._cmd("CIFSR") @property def networks(self): return self._cmd("CWLAP") @property def baudrate(self): return self._cmd("CIOBAUD?") @baudrate.setter def baudrate(self, value): return self._cmd("CIOBAUD=" + str(value)) @property def mode(self): return self._cmd("CWMODE?") @mode.setter def mode(self, value): self._cmd("CWMODE=" + str(value)) def connect(self, ssid, password=""): """ Connect to the given network ssid with the given password """ constr = "CWJAP=\"" + ssid + "\",\"" + password + "\"" return self._cmd(constr) def disconnect(self): return self._cmd("CWQAP") def reset(self): return self._cmd("RST")
class WIFI: """docstring for wifi""" def __init__(self, uart, baudrate = 115200): """ uart = uart #1-6, baudrate must match what is set on the ESP8266. """ self._uart = UART(uart, baudrate) def write( self, aMsg ) : self._uart.write(aMsg) res = self._uart.readall() if res: print(res.decode("utf-8")) def read( self ) : return self._uart.readall().decode("utf-8") def _cmd( self, cmd ) : """ Send AT command, wait a bit then return results. """ self._uart.write("AT+" + cmd + "\r\n") udelay(500) return self.read() @property def IP(self): return self._cmd("CIFSR") @property def networks( self ) : return self._cmd("CWLAP") @property def baudrate(self): return self._cmd("CIOBAUD?") @baudrate.setter def baudrate(self, value): return self._cmd("CIOBAUD=" + str(value)) @property def mode(self): return self._cmd("CWMODE?") @mode.setter def mode(self, value): self._cmd("CWMODE=" + str(value)) def connect( self, ssid, password = "" ) : """ Connect to the given network ssid with the given password """ constr = "CWJAP=\"" + ssid + "\",\"" + password + "\"" return self._cmd(constr) def disconnect( self ) : return self._cmd("CWQAP") def reset( self ) : return self._cmd("RST")
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)
class ESP8266(object): def __init__(self): self.uart = UART(6, 115200) def write(self, command): self.uart.write(command) count = 5 while count >= 0: if self.uart.any(): print(self.uart.readall().decode('utf-8')) time.sleep(0.1) count -= 1
class ESP8266(object): def __init__(self): self.uart = UART(6, 115200) def write(self, command): self.uart.write(command) count = 5 while count >= 0: if self.uart.any(): print(self.uart.readall().decode('utf-8')) time.sleep(0.1) count-=1
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)
class BLE: BLE_NONE = 0 BLE_SHIELD = 1 def command(self, cmd): if self.type == self.BLE_SHIELD: self.uart.write(cmd) self.uart.write("\r\n") r = self.uart.read(9) if r[0] != 82: raise OSError("Response corrupted!") if r[1] == 49: raise OSError("Command failed!") if r[1] == 50: raise OSError("Parse error!") if r[1] == 51: raise OSError("Unknown command!") if r[1] == 52: raise OSError("Too few args!") if r[1] == 53: raise OSError("Too many args!") if r[1] == 54: raise OSError("Unknown variable or option!") if r[1] == 55: raise OSError("Invalid argument!") if r[1] == 56: raise OSError("Timeout!") if r[1] == 57: raise OSError("Security mismatch!") if r[1] != 48: raise OSError("Response corrupted!") for i in range(2, 6): if r[i] < 48 or 57 < r[i]: raise OSError("Response corrupted!") if r[7] != 13 or r[8] != 10: raise OSError("Response corrupted!") l=((r[2]-48)*10000)+\ ((r[3]-48)*1000)+\ ((r[4]-48)*100)+\ ((r[5]-48)*10)+\ ((r[6]-48)*1) if not l: return None if l == 1 or l == 2: raise OSError("Response corrupted!") response = self.uart.read(l - 2) if self.uart.readchar() != 13: raise OSError("Response corrupted!") if self.uart.readchar() != 10: raise OSError("Response corrupted!") return response def deinit(self): if self.type == self.BLE_SHIELD: self.uart.deinit() self.rst = None self.uart = None self.type = self.BLE_NONE def init(self, type=BLE_SHIELD): self.deinit() if type == self.BLE_SHIELD: self.rst = Pin("P7", Pin.OUT_OD, Pin.PULL_NONE) self.uart = UART(3, 115200, timeout_char=1000) self.type = self.BLE_SHIELD self.rst.low() sleep(100) self.rst.high() sleep(100) self.uart.write("set sy c m machine\r\nsave\r\nreboot\r\n") sleep(1000) self.uart.readall() # clear def uart(self): if self.type == self.BLE_SHIELD: return self.uart def type(self): if self.type == self.BLE_SHIELD: return self.BLE_SHIELD def __init__(self): self.rst = None self.uart = None self.type = self.BLE_NONE
class BLE: BLE_NONE=0 BLE_SHIELD=1 def command(self, cmd): if self.type==self.BLE_SHIELD: self.uart.write(cmd) self.uart.write("\r\n") r=self.uart.read(9) if r[0]!=82: raise OSError("Response corrupted!") if r[1]==49: raise OSError("Command failed!") if r[1]==50: raise OSError("Parse error!") if r[1]==51: raise OSError("Unknown command!") if r[1]==52: raise OSError("Too few args!") if r[1]==53: raise OSError("Too many args!") if r[1]==54: raise OSError("Unknown variable or option!") if r[1]==55: raise OSError("Invalid argument!") if r[1]==56: raise OSError("Timeout!") if r[1]==57: raise OSError("Security mismatch!") if r[1]!=48: raise OSError("Response corrupted!") for i in range(2,6): if r[i]<48 or 57<r[i]: raise OSError("Response corrupted!") if r[7]!=13 or r[8]!=10: raise OSError("Response corrupted!") l=((r[2]-48)*10000)+\ ((r[3]-48)*1000)+\ ((r[4]-48)*100)+\ ((r[5]-48)*10)+\ ((r[6]-48)*1) if not l: return None if l==1 or l==2: raise OSError("Response corrupted!") response=self.uart.read(l-2) if self.uart.readchar()!=13: raise OSError("Response corrupted!") if self.uart.readchar()!=10: raise OSError("Response corrupted!") return response def deinit(self): if self.type==self.BLE_SHIELD: self.uart.deinit() self.rst=None self.uart=None self.type=self.BLE_NONE def init(self, type=BLE_SHIELD): self.deinit() if type==self.BLE_SHIELD: self.rst=Pin("P7",Pin.OUT_OD,Pin.PULL_NONE) self.uart=UART(3,115200,timeout_char=1000) self.type=self.BLE_SHIELD self.rst.low() sleep(100) self.rst.high() sleep(100) self.uart.write("set sy c m machine\r\nsave\r\nreboot\r\n") sleep(1000) self.uart.readall() # clear def uart(self): if self.type==self.BLE_SHIELD: return self.uart def type(self): if self.type==self.BLE_SHIELD: return self.BLE_SHIELD def __init__(self): self.rst=None self.uart=None self.type=self.BLE_NONE
return ''.join(chr(x) for x in out_buf) uart.write(org_out_buf(0x01, 0x01, 0x21, 0x34)) while (True): clock.tick() # Track elapsed milliseconds between snapshots(). img = sensor.snapshot() # Take a picture and return the image. #print(clock.fps()) # Note: Your OpenMV Cam runs about half as fast while # connected to your computer. The FPS should increase once disconnected. img.binary([high_threshold]) orient = find_orient(img) uart.write(org_out_buf(0x01, 0x00, orient[2], orient[1])) if (uart.any()): led.on() in_buf = uart.readall() if check_in_buf(in_buf) == False: continue print(' '.join(hex(x) for x in in_buf[1:6])) in_buf2 = in_buf[8:] if check_in_buf(in_buf2) == False: continue print(' '.join(hex(x) for x in in_buf2[1:6])) while (True): clock.tick() img = sensor.snapshot() #print(clock.fps()) img.binary([high_threshold]) orient = find_orient(img) print(orient)
global leds len_ = len(leds) for i in range(0, len_): if i != num_: leds[i].off() else: leds[i].on() while True: u2.init(2400, bits=8, parity=None, stop=1) pyb.delay(80) Quality = 'DATA NULL' if (u2.any() > 0): u2.deinit() _dataRead = u2.readall() #R代表截取数据的起始位 R = _dataRead.find(b'\xaa') #R>-1代表存在起始位,长度大于起始位位置+2 if R > -1 and len(_dataRead) > (R + 2): P = _dataRead[R + 1] L = _dataRead[R + 2] #把串口收到的十六进制数据转换成十进制 SHI = P * 256 + L SHUCHU = SHI / G * A if (SHUCHU < 35): Quality = 'Excellente' print('环境质量:优', 'PM2.5=', SHUCHU) count_ = 1 elif (35 < SHUCHU < 75): Quality = 'Good'
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') # tx only mode uart0 = UART(0, 1000000, pins=('GP12', None)) print(uart0.write(b'123456') == 6) print(uart1.read() == b'123456') print(uart1.write(b'123') == 3) print(uart0.read() == b'') # rx only mode uart0 = UART(0, 1000000, pins=(None, 'GP13')) print(uart0.write(b'123456') == 6) print(uart1.read() == b'') print(uart1.write(b'123') == 3) print(uart0.read() == b'123')
uart0.write(b'123') buf = bytearray(3) print(uart1.readinto(buf, 1) == 1) print(buf) print(uart1.readinto(buf) == 2) print(buf) 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') # tx only mode uart0 = UART(0, 1000000, pins=('GP12', None)) print(uart0.write(b'123456') == 6) print(uart1.read() == b'123456') print(uart1.write(b'123') == 3) print(uart0.read() == b'') # rx only mode uart0 = UART(0, 1000000, pins=(None, 'GP13')) print(uart0.write(b'123456') == 6) print(uart1.read() == b'') print(uart1.write(b'123') == 3) print(uart0.read() == b'123')
import pyb from pyb import UART from pyb import Pin from ubinascii import hexlify from ubinascii import * ulan = UART(6, 9600)#定义连接网口的串口 K=1 jia=0 jie1=0 he=0 js=0#设置寄存变量 #*******************************主程序********************************** print('while') while (K>0): _dataRead=ulan.readall()#读取客户端数据 if _dataRead!=None:#判断客户端是否传来数据 print(_dataRead) js=js+1#计数判断执行命令标志 if(js==1): jia=_dataRead.decode('utf-8')#数据转换 jia=int(jia)#数据转换 print(jia) if(js==2): jia1=_dataRead.decode('utf-8') jia1=int(jia1) print(jia1) if(js==2): he=jia+jia1 js=0 ulan.write(str(jia)+'+'+str(jia1)+'='+str(he)+'\r\n')#计算结果返回给客户端
sensor.reset() # Initialize the camera sensor. sensor.set_pixformat(sensor.RGB565) # or sensor.GRAYSCALE sensor.set_framesize(sensor.VGA) # or sensor.QQVGA (or others) pyb.LED(RED_LED_PIN).on() sensor.skip_frames(time = 2000) # Give the user time to get ready. pyb.LED(RED_LED_PIN).off() pyb.LED(BLUE_LED_PIN).off() # print("You're on camera!") count = 0 while True: if (uart.any()): data = uart.readall() pyb.LED(BLUE_LED_PIN).on() sensor.snapshot().save("%d.jpg"%pyb.rng()) # or "example.bmp" (or others) print("Take Photo %d"%(count)) pyb.LED(BLUE_LED_PIN).off() count = count + 1 else: # 啥事儿不干 sensor.snapshot() time.sleep(100)
# UART Control # # This example shows how to use the serial port on your OpenMV Cam. Attach pin # P4 to the serial input of a serial LCD screen to see "Hello World!" printed # on the serial LCD display. import time from pyb import UART # Always pass UART 3 for the UART number for your OpenMV Cam. # The second argument is the UART baud rate. For a more advanced UART control # example see the BLE-Shield driver. uart = UART(3, 9600) # while(True): #uart.write("\n") uart.write("AT+RST\r\n") #uart.write("\n") #time.sleep(10) s = "s" while (True): if (uart.any() > 0): s = uart.readall() print(s) print(s.decode('utf-8')) #字节转字符串