def ReadChar(fd): # read a single character back from the serial line qtydata = wiringpi.serialDataAvail(fd) logging.info("Amount of data: %s bytes" % qtydata) response = 0 if qtydata > 0: logging.debug("Reading data back %d" % qtydata) response = wiringpi.serialGetchar(fd) return response
def ReadInt(fd): # read a single character back from the serial line qtydata = wiringpi2.serialDataAvail(fd) # print ("Amount of data: %s bytes" % qtydata) # Added for debug purposes response = 0 if qtydata > 0: # print ("Reading data back %d" % qtydata) #Added for Debug purposes response = wiringpi2.serialGetchar(fd) return response
def ReadText(fd): # read the data back from the serial line and return it as a string to the calling function qtydata = wiringpi2.serialDataAvail(fd) # print ("Amount of data: %d bytes" % qtydata) # Added for debug purposes response = "" while qtydata > 0: # while there is data to be read, read it back # print ("Reading data back %d" % qtydata) #Added for Debug purposes response = response + chr(wiringpi2.serialGetchar(fd)) qtydata = qtydata - 1 return response
def read(self, buffer_len): """ read buffer_len size bytes from opened port. If there is no data available then the calling thread will be hang-up until data is available. If no data longer than 10 seconds, throw "read timeout exception". """ buffer = BytesIO() while len(buffer.getvalue()) < buffer_len: char = wiringpi.serialGetchar(self.serial_fd) if char == -1: raise "read timeout!" buffer.write(char.to_bytes(1, byteorder='big')) return buffer.getvalue()
def read_serial(): while running: try: if wpi.serialDataAvail(fd) != -1 : ch = wpi.serialGetchar(fd) if ch== -1 : continue ch = chr(ch) if ch == '\r': ch = '\n' print(ch, end='') except: pass print("bye.")
def ReadData(fd): # read the data back from the serial line and return it as a string to the calling function qtydata = wiringpi.serialDataAvail(fd) logging.info("Amount of data: %d bytes" % qtydata) response = [] while qtydata > 0: # while there is data to be read, read it back logging.debug("Reading data back byte:%d" % qtydata) # This used to have hex to convert the data to a hex string response.append(wiringpi.serialGetchar(fd)) qtydata = qtydata - 1 logging.info("Data Packet received: %s" % response) logging.debug("Size of data packet received %d" % len(response)) return response
def read_char(fd): # Reads chars from the serial device until the parity is correct. # Returns the first valid char received. while True: # read next byte b = serialGetchar(fd) if b >= 0: # check even parity p = True m = 0x80 while m: if (m & b): p = not p m = m >> 1 if p: return chr(b & 0x7f)
def _serial(send=None): wiringpi.wiringPiSetup() serial = wiringpi.serialOpen('/dev/ttyAMA0', 9600) if send != None: wiringpi.serialPuts(serial, send) wiringpi.serialPutchar(serial, 3) wiringpi.serialClose(serial) else: char = "" asciinum = -1 while (True): asciinum = wiringpi.serialGetchar(serial) if asciinum != -1 and asciinum != 3: char += chr(asciinum) elif asciinum == 3: break wiringpi.serialClose(serial) return char
def read_ATR(serial): wiringpi.pinMode(TX, INPUT) wiringpi.pullUpDnControl(TX, PUD_UP) first = wiringpi.millis() data_str = '' while (wiringpi.millis() - first) < 100: if wiringpi.serialDataAvail(serial) > 0: first = wiringpi.millis() data = wiringpi.serialGetchar(serial) if (data < 16): data *= 16 data_str += ('{:02x}'.format(data)) + ' ' wiringpi.pullUpDnControl(TX, PUD_OFF) wiringpi.pinModeAlt(TX, 4) data_str = data_str.lstrip('00') return data_str
def read_response(serial, ser, length): wiringpi.pinMode(TX, INPUT) wiringpi.pullUpDnControl(TX, PUD_UP) first = wiringpi.millis() wiringpi.delay(50) ## Save all the data from RX excluding bounced data data_list = [] num_ser = wiringpi.serialDataAvail(serial) while (wiringpi.millis() - first) < 100: if wiringpi.serialDataAvail(serial) > 0: first = wiringpi.millis() data = wiringpi.serialGetchar(serial) if (wiringpi.serialDataAvail(serial) < (num_ser - length)): data_list.append('{:02x}'.format(data)) wiringpi.pullUpDnControl(TX, PUD_OFF) wiringpi.pinModeAlt(TX, 4) return data_list
#!/usr/bin/env python import wiringpi as wpi import time serial = wpi.serialOpen('/dev/ttyS0', 115200) while True: input_str = raw_input('Serial Input> ') wpi.serialPuts(serial, input_str) time.sleep(0.1) output_str = 'Serial Output> ' while wpi.serialDataAvail(serial): output_str += chr(wpi.serialGetchar(serial)) print output_str wpi.serialClose(serial)
def read(self): while wiringpi.serialDataAvail(self.serial) > 0: value = (wiringpi.serialGetchar(self.serial)) if value > 0: print("{}".format(chr(value)), end='', flush=True)
serial_tty = "/dev/ttyS0" serial_speed = 9600 serial = pi.serialOpen(serial_tty, serial_speed) def dm_to_deg(data): deg = int(float(data) / 100) min = float(data) - deg * 100 return (round(deg + min / 60.0, 6)) while (True): gps_line = '' while (True): buf = chr(pi.serialGetchar(serial)) if (buf == '\r'): break elif (buf != '\n'): gps_line = gps_line + buf gps_data = gps_line.split(",") if (gps_data[0] == '$GPRMC'): lat = gps_data[3] long = gps_data[5] if (lat != "" and long != ""): lat_deg = dm_to_deg(lat) long_deg = dm_to_deg(long) print("Latitude:", lat_deg, " Longitude:", long_deg) else: print("Cannot catch satellite.")