当前位置: 首页>>代码示例>>Python>>正文


Python array.fromstring方法代码示例

本文整理汇总了Python中array.array.fromstring方法的典型用法代码示例。如果您正苦于以下问题:Python array.fromstring方法的具体用法?Python array.fromstring怎么用?Python array.fromstring使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在array.array的用法示例。


在下文中一共展示了array.fromstring方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: wait_for_interrupts

# 需要导入模块: from array import array [as 别名]
# 或者: from array.array import fromstring [as 别名]
  def wait_for_interrupts(self, wait_time = 1):
    """wait_for_interrupts
    
    listen for interrupts for the specified amount of time

    Args:
      wait_time: the amount of time in seconds to wait for an interrupt

    Returns:
      True: Interrupts were detected
      False: No interrupts detected

    Raises:
      Nothing
    """
    timeout = time.time() + wait_time

    temp = Array ('B')
    while time.time() < timeout:
      response = self.dev.read_data(1)
      rsp = Array('B')
      rsp.fromstring(response)
      temp.extend(rsp)
      if 0xDC in rsp:
        if self.debug:
          print "Got a response"  
        break

    if not 0xDC in rsp:
      if self.debug:
        print "Response not found"  
      return False

    read_total = 9
    read_count = len(rsp)

    #print "read_count: %s" % str(rsp)
    while (time.time() < timeout) and (read_count < read_total):
      response = self.dev.read_data(read_total - read_count)
      temp  = Array('B')
      temp.fromstring(response)
      #print "temp: %s", str(temp)
      if (len(temp) > 0):
        rsp += temp
        read_count = len(rsp)

    #print "read_count: %s" % str(rsp)
   

    index  = rsp.index(0xDC) + 1

    read_data = Array('B')
    read_data.extend(rsp[index:])
    #print "read_data: " + str(rsp)

    self.interrupts = read_data[-4] << 24 | read_data[-3] << 16 | read_data[-2] << 8 | read_data[-1]
    
    if self.debug:
      print "interrupts: " + str(self.interrupts)
    return True
开发者ID:CospanDesign,项目名称:olympus,代码行数:62,代码来源:dionysus.py

示例2: read_dma_data

# 需要导入模块: from array import array [as 别名]
# 或者: from array.array import fromstring [as 别名]
 def read_dma_data(self, count):
     d = Array('B')
     self.write_command(CMD_DMA_READ, count, 0x00)
     print "Send Peripheral Read Command"
     data = Array('B')
     data.fromstring(os.read(self.f, count * 4))
     print "Data: %s" % str(data)
     print_32bit_hex_array(d)
开发者ID:CospanDesign,项目名称:nysa-tx1-pcie-platform,代码行数:10,代码来源:nysa_pcie.py

示例3: load_callback

# 需要导入模块: from array import array [as 别名]
# 或者: from array.array import fromstring [as 别名]
 def load_callback(self):
     filename = self.loader.get_filename()
     self.read_data_count.setText("0x00")
     if not os.path.exists(filename):
         self.f2m_button.setEnabled(False)
         return
     self.f2m_button.setEnabled(True)
     f = open(filename, "rb")
     d = Array('B')
     d.fromstring(f.read())
     f.close()
     self.write_data_count.setText(hex(len(d)))
开发者ID:CospanDesign,项目名称:nysa-gui,代码行数:14,代码来源:file_io.py

示例4: ping

# 需要导入模块: from array import array [as 别名]
# 或者: from array.array import fromstring [as 别名]
  def ping(self):
    """ping

    Pings the Olympus image

    Args:
      Nothing

    Returns:
      Nothing

    Raises:
      OlympusCommError
    """
    data = Array('B')
    data.extend([0XCD, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
    if self.debug:
      print "Sending ping...",
    self.dev.write_data(data)
    rsp = Array('B')
    temp = Array('B')

    timeout = time.time() + self.read_timeout

    while time.time() < timeout:
      response = self.dev.read_data(5)
      if self.debug:
        print ".",
      rsp = Array('B')
      rsp.fromstring(response)
      temp.extend(rsp)
      if 0xDC in rsp:
        if self.debug:
          print "Got a response"  
          print "Response: %s" % str(temp)
        break

    if not 0xDC in rsp:
      if self.debug:
        print "ID byte not found in response"  
        print "temp: " + str(temp)
      raise OlympusCommError("Ping response did not contain ID: %s" % str(temp))

    index  = rsp.index(0xDC) + 1

    read_data = Array('B')
    read_data.extend(rsp[index:])
    num = 3 - index
    read_data.fromstring(self.dev.read_data(num))
    if self.debug:
      print "Success!"
    return
开发者ID:CospanDesign,项目名称:olympus,代码行数:54,代码来源:dionysus.py

示例5: file_2_memory

# 需要导入模块: from array import array [as 别名]
# 或者: from array.array import fromstring [as 别名]
 def file_2_memory(self, filename, address, count = None):
     """
     Data should be in the form of a byte array
     """
     f = open(filename, 'rb')
     d = Array('B')
     d.fromstring(f.read())
     f.close()
     if count is None:
         count = len(d)
     mem_base = self.n.get_device_address(self.urn)
     print "Writing %d bytes down" % len(d[0:count])
     self.n.write_memory(mem_base + address, d[0:count])
开发者ID:CospanDesign,项目名称:nysa-gui,代码行数:15,代码来源:controller.py

示例6: ping

# 需要导入模块: from array import array [as 别名]
# 或者: from array.array import fromstring [as 别名]
	def ping(self):
		data = Array('B', '0000'.decode('hex'))
		print "writing"
		for a in data:
			print "Data: %02X" % (a)

		self.dev.write_data(data)
		print "reading"

		time.sleep(.1)
		response = self.dev.read_data(4)
		rsp = Array('B')
		rsp.fromstring(response)
		print "rsp: " + str(rsp)
开发者ID:cospan,项目名称:sycamore,代码行数:16,代码来源:sycamore.py

示例7: read_periph_data

# 需要导入模块: from array import array [as 别名]
# 或者: from array.array import fromstring [as 别名]
    def read_periph_data(self, address, count):
        d = Array('B')
        data_count = count
        if data_count == 0:
            data_count = 1
        d.extend(dword_to_array(IDWORD))
        d.extend(dword_to_array(0x00000002))
        d.extend(dword_to_array(data_count))
        d.extend(dword_to_array(address))

        self.write_command(CMD_PERIPHERAL_WRITE, len(d) / 4, 0x00)
        os.write(self.f, d)
        self.write_command(CMD_PERIPHERAL_READ, data_count, 0x00)
        print "Send Peripheral Read Command"
        data = Array('B')
        data.fromstring(os.read(self.f, data_count * 4))
        print "Data: %s" % str(data)
开发者ID:CospanDesign,项目名称:nysa-tx1-pcie-platform,代码行数:19,代码来源:nysa_pcie.py

示例8: _exchange

# 需要导入模块: from array import array [as 别名]
# 或者: from array.array import fromstring [as 别名]
 def _exchange(self, frequency, cs_cmd, out, readlen):
     """Perform a half-duplex transaction with the SPI slave"""
     if not self._ftdi:
         raise SpiIOError("FTDI controller not initialized")
     if len(out) > SpiController.PAYLOAD_MAX_LENGTH:
         raise SpiIOError("Output payload is too large")
     if readlen > SpiController.PAYLOAD_MAX_LENGTH:
         raise SpiIOError("Input payload is too large")
     if self._frequency != frequency:
         self._ftdi.set_frequency(frequency)
         # store the requested value, not the actual one (best effort)
         self._frequency = frequency
     write_cmd = struct.pack('<BH', Ftdi.WRITE_BYTES_NVE_MSB, len(out)-1)
     cmd = Array('B', cs_cmd)
     if PY3:
         cmd.frombytes(write_cmd)
     else:
         cmd.fromstring(write_cmd)
     cmd.extend(out)
     if readlen:
         read_cmd = struct.pack('<BH', Ftdi.READ_BYTES_NVE_MSB, readlen-1)
         if PY3:
             cmd.frombytes(read_cmd)
         else:
             cmd.fromstring(read_cmd)
         cmd.extend(self._immediate)
         if self._turbo:
             cmd.extend(self._cs_high)
             self._ftdi.write_data(cmd)
         else:
             self._ftdi.write_data(cmd)
             self._ftdi.write_data(self._cs_high)
         # USB read cycle may occur before the FTDI device has actually
         # sent the data, so try to read more than once if no data is
         # actually received
         data = self._ftdi.read_data_bytes(readlen, 4)
     else:
         if self._turbo:
             cmd.extend(self._cs_high)
             self._ftdi.write_data(cmd)
         else:
             self._ftdi.write_data(cmd)
             self._ftdi.write_data(self._cs_high)
         data = Array('B')
     return data
开发者ID:kery-chen,项目名称:pyftdi,代码行数:47,代码来源:spi.py

示例9: write_string

# 需要导入模块: from array import array [as 别名]
# 或者: from array.array import fromstring [as 别名]
  def write_string(self, string = ""):
    """write_string
    
    Writes a string of data over the UART

    Args:
      string: String to send

    Return:
      Nothing

    Raises:
      OlympusCommError
    """
    if self.debug:
      print "Writing a string"

    data = Array('B')
    data.fromstring(string)
    self.write_raw(data)
开发者ID:CospanDesign,项目名称:olympus,代码行数:22,代码来源:uart.py

示例10: test_rw_sector_1

# 需要导入模块: from array import array [as 别名]
# 或者: from array.array import fromstring [as 别名]
	def test_rw_sector_1(self):
		from hashlib import sha1
		buf = Array('I')
#		length = 65536
		length = len(self.flash)
		print "length: " + str(length)
		print "Build Sequence"
		for address in range (0, length):
			buf.append(address)
		print "Swap sequence"
		buf = buf.byteswap()
		#print "Erase flash from %08X to %08X" % (0, length)
		print "Erase all of the flash"
		self.flash.erase(0, len(self.flash))
		bufstr = buf.tostring()
		dout = Array('B')
		dout.fromstring(bufstr)
		self.flash.write(0, bufstr)
		print "Verify Flash"
		wmd = sha1()
		wmd.update(buf.tostring())
		refdigest = wmd.hexdigest()
		print "Read Flash"
		din = self.flash.read(0, length)
		print "Dump Flash"
		print hexdump(din.tostring())
		print "Verify Flash"
		rmd = sha1()
		rmd.update(din.tostring())
		newdigest = rmd.hexdigest()
		print "Reference: ", refdigest
		print "Retrieved: ", newdigest

		try:
			f = open("din.hex", "w")
			din.tofile(f)
			f.close()
		except IOError, err:
			print "Error writing to din file"
开发者ID:cospan,项目名称:sycamore,代码行数:41,代码来源:test_spi_flash.py

示例11: test_flashdevice_4_long_rw

# 需要导入模块: from array import array [as 别名]
# 或者: from array.array import fromstring [as 别名]
 def test_flashdevice_4_long_rw(self):
     """Long R/W test
     """
     # Max size to perform the test on
     size = 1<<20
     # Whether to test with random value, or contiguous values to ease debug
     randomize = True
     # Fill in the whole flash with a monotonic increasing value, that is
     # the current flash 32-bit address, then verify the sequence has been
     # properly read back
     from hashlib import sha1
     # limit the test to 1MiB to keep the test duration short, but performs
     # test at the end of the flash to verify that high addresses may be
     # reached
     length = min(len(self.flash), size)
     start = len(self.flash)-length
     print "Erase %s from flash @ 0x%06x(may take a while...)" % \
         (pretty_size(length), start)
     delta = time.time()
     self.flash.unlock()
     self.flash.erase(start, length, True)
     delta = time.time()-delta
     self._report_bw('Erased', length, delta)
     if str(self.flash).startswith('SST'):
         # SST25 flash devices are tremendously slow at writing (one or two
         # bytes per SPI request MAX...). So keep the test sequence short
         # enough
         length = 16<<10
     print "Build test sequence"
     if not randomize:
         buf = Array('I')
         back = Array('I')
         for address in range(0, length, 4):
             buf.append(address)
         # Expect to run on x86 or ARM (little endian), so swap the values
         # to ease debugging
         # A cleaner test would verify the host endianess, or use struct
         # module
         buf.byteswap()
         # Cannot use buf directly, as it's an I-array,
         # and SPI expects a B-array
     else:
         from random import seed
         seed(0)
         buf = Array('B')
         back = Array('B')
         buf.extend((randint(0, 255) for x in range(0, length)))
     bufstr = buf.tostring()
     print "Writing %s to flash (may take a while...)" % \
         pretty_size(len(bufstr))
     delta = time.time()
     self.flash.write(start, bufstr)
     delta = time.time()-delta
     length = len(bufstr)
     self._report_bw('Wrote', length, delta)
     wmd = sha1()
     wmd.update(buf.tostring())
     refdigest = wmd.hexdigest()
     print "Reading %s from flash" % pretty_size(length)
     delta = time.time()
     data = self.flash.read(start, length)
     delta = time.time()-delta
     self._report_bw('Read', length, delta)
     #print "Dump flash"
     #print hexdump(data.tostring())
     print "Verify flash"
     rmd = sha1()
     rmd.update(data.tostring())
     newdigest = rmd.hexdigest()
     print "Reference:", refdigest
     print "Retrieved:", newdigest
     if refdigest != newdigest:
         errcount = 0
         back.fromstring(data)
         for pos in xrange(len(buf)):
             if buf[pos] != data[pos]:
                 print 'Invalid byte @ offset 0x%06x: 0x%02x / 0x%02x' % \
                     (pos, buf[pos], back[pos])
                 errcount += 1
                 # Stop report after 16 errors
                 if errcount >= 32:
                     break
         raise AssertionError('Data comparison mismatch')
开发者ID:0xd3d0,项目名称:pyspiflash,代码行数:85,代码来源:serialflash.py

示例12: _write_bytes_raw

# 需要导入模块: from array import array [as 别名]
# 或者: from array.array import fromstring [as 别名]
 def _write_bytes_raw(self, out):
     """Output bytes on TDI"""
     olen = len(out)-1
     cmd = Array('B', [Ftdi.WRITE_BYTES_NVE_LSB, olen&0xff, (olen>>8)&0xff])
     cmd.fromstring(out)
     self._stack_cmd(cmd)
开发者ID:DavidWC,项目名称:pyftdi,代码行数:8,代码来源:jtag.py

示例13: load_rom

# 需要导入模块: from array import array [as 别名]
# 或者: from array.array import fromstring [as 别名]
    def load_rom(self, filename):
        f = open(filename, 'r')
        #data = f.read()
        data = Array('B')
        data.fromstring(f.read())
        print "Length of data: %d" % len(data)
        print "Type: %s" % str(type(data))
        f.close()
        #if ((data[0] != 'N') or (data[1] != 'E') or (data[2] != 'S') or (data[3] != 0x1A)):
        if ((data[0] != 0x4E) or (data[1] != 0x45) or (data[2] != 0x53) or (data[3] != 0x1A)):
            raise NESError("Invalid ROM header")


        prg_rom_banks = data[4]
        chr_rom_banks = data[5]
        if (prg_rom_banks > 2) or (chr_rom_banks > 1):
            raise NESError("Too many ROM banks: PRG_ROM: %d CHR ROM: %d" % (prg_rom_banks, chr_rom_banks))

        mapper = (((data[6] & 0xF0) >> 4) | ((data[7] & 0xF0)) != 0)
        if mapper != 0:
            raise NESError("Only mapper 0 is supported")

        # Issue a debug break
        yield self.enter_debug()

        #Disable PPU
        yield self.disable_ppu()

        #Set header info to config mapper
        config = [0] * 5
        config[0] = data[4]
        config[1] = data[5]
        config[2] = data[6]
        config[3] = data[7]
        config[4] = data[8]
        yield self.set_cart_config(config)

        #Calculate all the sizes
        prg_rom_size = prg_rom_banks * 0x4000
        chr_rom_size = chr_rom_banks * 0x2000
        total_size = prg_rom_size + chr_rom_size
        transfer_block_size = 0x400

        prg_rom = data[16:16 + prg_rom_size]
        chr_rom = data[16 + prg_rom_size: 16 + prg_rom_size + chr_rom_size]
        
        #XXX: SIMULATION Don't write too much data
        if SIM:
            load_prg_rom = prg_rom[0:256]
            load_chr_rom = prg_rom[0:256]

        prg_offset = 0x8000
        #Copy PRG ROM data
        if SIM:
            yield self.write_cpu_mem(prg_offset, load_prg_rom)
        else:
            yield self.write_cpu_mem(prg_offset, prg_rom)

        #Copy CHR ROM data
        #chr_offset = prg_offset + len(prg_rom)
        chr_offset = 0x00
        #yield self.write_cpu_mem(chr_offset, chr_rom)
        if SIM:
            yield self.write_ppu_mem(chr_offset, load_chr_rom)
        else:
            yield self.write_ppu_mem(chr_offset, chr_rom)

        #Update PC to point to the reset interrupt vector location
        pcl_val = data[16 + prg_rom_size - 4]
        pch_val = data[16 + prg_rom_size - 3]
        print "PCH:PCL: %02X:%02X" % (pch_val, pcl_val)

        yield self.write_cpu_register(CPU_REG_PCL, pcl_val) 
        yield self.write_cpu_register(CPU_REG_PCH, pch_val)

        #Issue a debug run command
        yield self.exit_debug()
开发者ID:CospanDesign,项目名称:nysa-verilog,代码行数:79,代码来源:nes_hci_driver.py

示例14: dump_core

# 需要导入模块: from array import array [as 别名]
# 或者: from array.array import fromstring [as 别名]
  def dump_core(self):
    """dump_core

    reads the state of the wishbone master prior to a reset, useful for
    debugging

    Args:
      Nothing

    Returns:
      Array of 32-bit values to be parsed by core_analyzer

    Raises:
      AssertionError: This function must be overriden by a board specific
      implementation
      OlympusCommError: A failure of communication is detected
    """

    data = Array('B')
    data.extend([0xCD, 0x0F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
    print "Sending core dump request..."

    self.dev.purge_buffers()
    self.dev.write_data(data)

    core_dump = Array('L')
    wait_time = 5
    timeout = time.time() + wait_time

    temp = Array ('B')
    while time.time() < timeout:
      response = self.dev.read_data(1)
      rsp = Array('B')
      rsp.fromstring(response)
      temp.extend(rsp)
      if 0xDC in rsp:
        print "Got a response"  
        break

    if not 0xDC in rsp:
      print "Response not found"  
      raise OlympusCommError("Response Not Found")

    rsp = Array('B')
    read_total = 4
    read_count = len(rsp)

    #get the number of items from the address
    timeout = time.time() + wait_time
    while (time.time() < timeout) and (read_count < read_total):
      response = self.dev.read_data(read_total - read_count)
      temp  = Array('B')
      temp.fromstring(response)
      if (len(temp) > 0):
        rsp += temp
        read_count = len(rsp)

    print "Length of read: %d" % len(rsp)
    print "Data: %s" % str(rsp)
    count  = ( rsp[1] << 16 | rsp[2] << 8 | rsp[3]) * 4
    print "Number of core registers: %d" % (count / 4)

    #get the core dump data
    timeout = time.time() + wait_time
    read_total  = count
    read_count  = 0
    temp = Array ('B')
    rsp = Array('B')
    while (time.time() < timeout) and (read_count < read_total):
      response = self.dev.read_data(read_total - read_count)
      temp  = Array('B')
      temp.fromstring(response)
      if (len(temp) > 0):
        rsp += temp
        read_count = len(rsp)

    print "Length read: %d" % (len(rsp) / 4)
    print "Data: %s" % str(rsp)
    core_data = Array('L')
    for i in range (0, count, 4):
      print "count: %d" % i
      core_data.append(rsp[i] << 24 | rsp[i + 1] << 16 | rsp[i + 2] << 8 | rsp[i + 3])
    
    #if self.debug:
    print "core data: " + str(core_data)

    return core_data
开发者ID:CospanDesign,项目名称:olympus,代码行数:89,代码来源:dionysus.py

示例15: write

# 需要导入模块: from array import array [as 别名]
# 或者: from array.array import fromstring [as 别名]
  def write(self, device_id, address, data=None, mem_device = False):
    """write

    Write data to an Olympus image

    Args:
      device_id: Device identification number, found in the DRT
      address: Address of the register/memory to read
      mem_device: True if the device is on the memory bus
      data: Array of raw bytes to send to the device

    Returns:
      Nothing

    Raises:
      OlympusCommError
    """
    length = len(data) / 4

    # ID 01 NN NN NN OO AA AA AA DD DD DD DD
      # ID = ID BYTE (0xCD)
      # 01 = Write Command
      # NN = Size of write (3 bytes)
      # OO = Offset of device
      # AA = Address (4 bytes)
      # DD = Data (4 bytes)

    #create an array with the identification byte (0xCD)
    #and code for write (0x01)

    data_out = Array('B', [0xCD, 0x01]) 
    if mem_device:
      if self.debug:
        print "memory device"
      data_out = Array ('B', [0xCD, 0x11])
    
    """
    print "write command:\n\t" + str(data_out[:9])
    for i in range (0, len(data_out)):
      print str(hex(data_out[i])) + ", ",
    print " "
    """

 

    #append the length into the frist 32 bits
    fmt_string = "%06X" % (length) 
    data_out.fromstring(fmt_string.decode('hex'))
    offset_string = "00"
    if not mem_device:
      offset_string = "%02X" % device_id
    data_out.fromstring(offset_string.decode('hex'))
    addr_string = "%06X" % address
    data_out.fromstring(addr_string.decode('hex'))
    
    data_out.extend(data)

    """
    #if (self.debug):
    print "data write string:\n"
    print "write command:\n\t" + str(data_out[:9])
    for i in range (0, 9):
      print str(hex(data_out[i])) + ", ",
    print " "
    """


    #print "write data:\n" + str(data_out[9:])

    #avoid the akward stale bug
    self.dev.purge_buffers()

    self.dev.write_data(data_out)
    rsp = Array('B')

    timeout = time.time() + self.read_timeout
    while time.time() < timeout:
      response = self.dev.read_data(1)
      if len(response) > 0:
        rsp = Array('B')
        rsp.fromstring(response)
        if rsp[0] == 0xDC:
          if self.debug:
            print "Got a response"  
          break

    if (len(rsp) > 0):
      if rsp[0] != 0xDC:
        if self.debug:
          print "Response not found"  
        raise OlympusCommError("Did not find identification byte (0xDC): %s" % str(rsp))

    else:
      if self.debug:
        print "No Response"
      raise OlympusCommError("Timeout while waiting for a response")

    response = self.dev.read_data(8)
    rsp = Array('B')
    rsp.fromstring(response)
#.........这里部分代码省略.........
开发者ID:CospanDesign,项目名称:olympus,代码行数:103,代码来源:dionysus.py


注:本文中的array.array.fromstring方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。