2016-10-25 30 views
0

私はいくつかのシリアル入出力操作をしようとしていますが、そのうちの1つは8x8配列を外部デバイス(Arduino)に送信することです。 pySerialライブラリでは、送信する情報が1バイトであることが必要です。しかし、私のPythonコードでは、8x8の行列はタイプ<class 'str'>で構成されています。ここに私の送信機能があります:Pythonバイトからバイトへの変換

import serial 
import Matrix 

width = 8 
height = 8 
portName = 'COM3' 

def sendMatrix(matrix): 
    try: 
     port = serial.Serial(portName, 9600, timeout = 1000000) 
     port.setDTR(0) 
     print("Opened port: \"%s\"." % (portName)) 
     receivedByte = port.read() 
     print(int(receivedByte)) 
     if (receivedByte == '1'): 
      port.write('1') 
     bytesWritten = 0 
     for row in range(8): 
      for col in range(8): 
       value = matrix.getPoint(col, row) 
       bytesWritten += port.write(value)//ERROR HERE! 
     print(int(port.read())); 
     port.close() 
     print("Data (%d) sent to port: \"%s\"." % (bytesWritten, portName)) 
    except: 
     print("Unable to open the port \"%s\"." % (portName)) 


def main(): 
    matrix = Matrix.Matrix.readFromFile('framefile', 8, 8) 
    matrix.print() 
    print(type(matrix.getPoint(0, 0))) 
    print(matrix.getPoint(1, 1)) 
    sendMatrix(matrix) 

main() 

は今、私は、問題の配列であるフィールドmapを含むクラスMatrixを、持っている、と私はここにもそのコードが含まれますが、私がいる問題があります配列の各要素はstrの型ですが、バイトに変換する必要があります。私はデータの損失を無視することができます。なぜなら、実際には0と1だけを使用するからです。

マイマトリックスクラス:Unicode文字列にバイト文字列を変換するに

>>> 'foo'.encode('utf-8') 
b'foo' 

class Matrix(object): 

    def __init__(self, width, height): 
     self.width = width 
     self.height = height 
     self.map = [[0 for x in range(width)] for y in range(height)] 

    def setPoint(self, x, y, value): 
     if ((x >= 0) and (x < self.width) and (y >= 0) and (y < self.height)): 
      self.map[y][x] = value 

    def getPoint(self, x, y): 
     if ((x >= 0) and (x < self.width) and (y >= 0) and (y < self.height)): 
      return self.map[y][x] 

    def print(self): 
     for row in range(self.height): 
      for col in range(self.width): 
       print(str(self.map[row][col])+" ", end="") 
      print() 

    def save(self, filename): 
     f = open(filename, 'w') 
     for row in range(self.height): 
      for col in range(self.width): 
       f.write(str(self.map[row][col])) 
      f.write('\n') 
     f.close() 

    def toByteArray(self): 
     matrixBytes = bytearray(self.width * self.height) 
     for row in range(self.height): 
      for col in range(self.width): 
       matrixBytes.append(int(self.map[row][col])) 
     return matrixBytes 

    def getMap(self): 
     return self.map 

    def readFromFile(filename, width, height): 
     f = open(filename, 'r') 
     lines = list(f) 
     matrix = Matrix(width, height) 
     f.close() 
     for row in range(len(lines)): 
      matrix.map[row] = lines[row].strip('\n') 
     return matrix 
+0

なぜ 'bytes(matrix.toByteArray())'をしないのですか? –

+0

行列はオブジェクトであり、その内部に幅、高さ、および2次元配列が含まれています。それは単なる反復可能な構造ではありません。 –

+1

もちろん、 'matrix.toByteArray'メソッドのコードは、行列データをバイト配列に正しくシリアル化するように見えます。組み込みの' bytes'関数は、バイト配列をバイト配列として生成します。 –

答えて

2

Pythonでバイト列にUnicode文字列を変換するためには、これを行う

>>> b'foo'.decode('utf-8') 
'foo' 
+0

これはutf-8の代わりにasciiの値で使えますか? –

+0

はい、 '' utf-8''の代わりに '' ascii''を使います。可能なエンコードのリストについては、[here](https://docs.python.org/3/library/codecs.html#standard-encodings)を参照してください。 – Nils

関連する問題