Функция чтения PySerial читает больше, чем должна - PullRequest
0 голосов
/ 16 января 2020

У меня есть небольшой python скрипт, который связывается с моим BusPirate, настраивает его и читает содержимое NAND Fla sh, который к нему прикреплен. Для этого я использую некоторые функции модуля pySerial.
Следующий блок показывает, как сконфигурирован БП и как считывается содержимое fla sh.

  #coding=utf-8
  import serial
  import struct
  import time

  port = 'COM5' #Port that belongs to the BusPirate
  speed = 115200 #Baudrate of the BP

  print "Open port", port
  print "Setting baudrate:", speed
  print "--------------------------"
  with serial.Serial(port, speed, timeout=1, write_timeout=0) as ser, open('image.bin', 'a') as out:

      print "Enter BBIO mode..."
      for i in range(20):
          ser.write(b'\x00')
      if b'BBIO1' not in ser.read(5):
          print "Could not get into BBIO mode. :("
          quit()
      else:
          print "Entered BBIO mode."

      print "--------------------------"    
      print "Enter SPI mode..."
      ser.write(b'\x01')
      if b'SPI1' not in ser.read(4):
          print "Could not get into SPI mode. :("
          quit()
      else:
          print "Entered SPI mode."

      print "--------------------------"
      print "Setting SPI speed..."
      ser.write(b'\x64') #2MHz
      if ser.read(1) != b'\x01':
          print "Could not set speed. :("
          quit()
      else:
          print "SPI speed is now at 2MHz."

      print "--------------------------"
      print "Configuring the BP..."
      ser.write(b'\x8a') #configures SPI mode with: CLK idle low, CKE Edge from active to 
                         #idle, w=3.3V, SMP Sample = Middle
      if ser.read(1) != b'\x01':
          print "Error in the configuration. :("
          quit()
      else:
          ser.write(b'\x49') #activate CS and power up the device
          if ser.read(1) != b'\x01':
              print "Could not power up the device. :("
              quit()
      print "Powered Up!"

      print "--------------------------"
      print "Reading bytes..."
      ser.write(b'\x04') #1 Byte for the command read then write
      ser.write(b'\x00\x04') #no. of bytes that are written
      ser.write(b'\x02\x20') #no. of bytes that should be read
      ser.write(b'\x03\x00\x00\x00') #bytes that are written
      buffer = ser.read(544) #Read data from flash
      out.write(buffer) #Write data into image.bin
      print "--------------------------"    
      ser.write(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')
      print "Resetting the BP..."    
      ser.write(b'\x0f')
      quit()

Проблема is out.write (buffer) не записывает ожидаемые 544 байта в файл image.bin. Вместо этого он записывает обратно 548 байт. Это не имеет смысла для меня, так как я указал в строке ранее (buffer = ser.read (544)), чтобы читать только 544 байта. Откуда берутся эти 4 других байта и почему это происходит?
На самом деле, у меня та же проблема со всеми остальными операциями чтения. Особенно странно, что слишком большое количество байтов не соответствует. Я понятия не имею, что может быть источником проблемы.

...