树莓派和 Arduino 之间的通信

时间:2021-05-31 11:38:07

标签: python arduino pygame chess

我正在尝试通过串行通信将棋盘从我的 Arduino 获取到我的 Rasbperry。在我的国际象棋程序中定义了棋盘:

self.board = 
[['bR', 'bN', 'bB', 'bQ', 'bK', 'bB', 'bN', 'bR'],
 ['bp', 'bp', 'bp', 'bp', 'bp', 'bp', 'bp', 'bp'],
 ['--', '--', '--', '--', '--', '--', '--', '--'],
 ['--', '--', '--', '--', '--', '--', '--', '--'],
 ['--', '--', '--', '--', '--', '--', '--', '--'],
 ['--', '--', '--', '--', '--', '--', '--', '--'],
 ['wp', 'wp', 'wp', 'wp', 'wp', 'wp', 'wp', 'wp'],
 ['wR', 'wN', 'wB', 'wQ', 'wK', 'wB', 'wN', 'wR']]

像这样,董事会运作良好并加载。但我想通过以下方式从 Arduino 获取董事会信息:

   def brett(self):
        
            ser = serial.Serial('/dev/ttyACM0', 9600, timeout=1)
            ser.flush()
            while True:
                if ser.in_waiting > 0:
                    Brett = [ser.readline().decode('utf-8').rstrip()]
                    return Brett   

    def __init__(self):

        self.board = self.brett()
...

这里 Brett 是我的电路板,形成了我得到的 Arduino:

['bR', 'bN', 'bB', 'bQ', 'bK', 'bB', 'bN', 'bR'],
['bp', 'bp', 'bp', 'bp', 'bp', 'bp', 'bp', 'bp'],
['--', '--', '--', '--', '--', '--', '--', '--'],
['--', '--', '--', '--', '--', '--', '--', '--'],
['--', '--', '--', '--', '--', '--', '--', '--'],
['--', '--', '--', '--', '--', '--', '--', '--'],
['wp', 'wp', 'wp', 'wp', 'wp', 'wp', 'wp', 'wp'],
['wR', 'wN', 'wB', 'wQ', 'wK', 'wB', 'wN', 'wR']

正是董事会没有?不知何故我无法运行棋盘,有人知道为什么它不能像这样工作,或者我能做些什么来让它工作?总体上有可能做这样的事情吗? 我目前收到的错误是:

string index out of Range
end_piece = self.board[end_row][end_col]

我认为它仍然是因为 self.board 没有从我的 Arduino 中获取数组,因为当我在第一部分中使用数组时,字符串没有超出范围。

编辑: Arduino 代码非常简单,我只是想先将数组作为输入:

void setup() {
  Serial.begin(9600);
}
void loop() {
  Serial.println("['bR', 'bN', 'bB', 'bQ', 'bK', 'bB', 'bN', 'bR'],['bp', 'bp', 'bp', 'bp', 'bp', 'bp', 'bp', 'bp'],['--', '--', '--', '--', '--', '--', '--', '--'],['--', '--', '--', '--', '--', '--', '--', '--'],['--', '--', '--', '--', '--', '--', '--', '--'],['--', '--', '--', '--', '--', '--', '--', '--'],['wp', 'wp', 'wp', 'wp', 'wp', 'wp', 'wp', 'wp'],['wR', 'wN', 'wB', 'wQ', 'wK', 'wB', 'wN', 'wR']");
  delay(1000000000);
}

这是代码的一部分,错误开始的地方,但我认为它没有用,因为代码在我编写数组时有效。

    def checkForPinsAndChecks(self):
        pins = []  # squares pinned and the direction its pinned from
        checks = []  # squares where enemy is applying a check
        in_check = False
        if self.white_to_move:
            enemy_color = "b"
            ally_color = "w"
            start_row = self.white_king_location[0]
            start_col = self.white_king_location[1]
        else:
            enemy_color = "w"
            ally_color = "b"
            start_row = self.black_king_location[0]
            start_col = self.black_king_location[1]
        # check outwards from king for pins and checks, keep track of pins
        directions = ((-1, 0), (0, -1), (1, 0), (0, 1), (-1, -1), (-1, 1), (1, -1), (1, 1))
        for j in range(len(directions)):
            direction = directions[j]
            possible_pin = ()  # reset possible pins
            for i in range(1, 8):
                end_row = start_row + direction[0] * i
                end_col = start_col + direction[1] * i
                if 0 <= end_row <= 7 and 0 <= end_col <= 7:
                    end_piece = self.board[end_row][end_col]

对不起,我是编程新手,这是我的项目。
在此先感谢您的帮助

0 个答案:

没有答案