微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

树莓派和 Arduino 之间的通信

如何解决树莓派和 Arduino 之间的通信

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

self.board = 
[['bR','bN','bB','bQ','bK','bR'],['bp','bp','bp'],['--','--','--'],['wp','wp','wp'],['wR','wN','wB','wQ','wK','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','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','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,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]

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。