通过pySerial通过Python控制Adruino

时间:2015-07-09 18:12:35

标签: python tkinter arduino ascii pyserial

您好,并提前感谢您的时间,

我正在尝试使用pySerial和Tkinter通过python脚本控制连接到我的Arduino的一系列继电器。问题是,虽然我知道我的代码连接到我的Arduino(我听到当我使用Arduino软件上传代码时得到的同样的继电器颤动),我无法让继电器响应我发送的命令Tkinter GUI。这是我的python代码:

ser = Serial(port=3, timeout=1, writeTimeout=1, baudrate=9600)  # ensure non-blocking

class Application(Frame):

    print("Arduino Dish Control Ready")

    def __init__(self, parent):  # create constructor
        Frame.__init__(self, parent)  # define parent
        self.parent = parent  # save reference of parent widget
        self.initUI()  # delegate creation of the initUI() method

    def initUI(self):
        self.parent.title("Dish Control")  # title of window

        Style().configure("TButton", padding=(0, 5, 0, 5), font='serif 10')

        self.columnconfigure(0, pad=4)
        self.columnconfigure(1, pad=4)
        self.columnconfigure(2, pad=4)
        self.columnconfigure(3, pad=4)
        self.columnconfigure(4, pad=4)

        self.rowconfigure(0, pad=3)
        self.rowconfigure(1, pad=3)
        self.rowconfigure(2, pad=3)
        self.rowconfigure(3, pad=3)

        def UP():  # define the UP command.
            ser.write(str(32))  # convert "32" to ASCII and send it via serial port (USB) to arduino.
            print ser.write(str(64))
            # sleep(0.1)
        up = Button(self, text="Up", command=UP)  # create button UP and set the command.
        up.grid(row=0, column=1)  # define position of UP button.


        self.pack()

以下是相关的Arduino代码:

void loop(){
  if(Serial.available() > 0){
    Serial.begin(9600);
    int inByte = Serial.read(); //read the incoming data
    Serial.print("I received: ");
    Serial.println(inByte, DEC);

    if(Serial.read() == 32){//If the serial reads 32...
      digitalWrite(8, LOW); //Motor Select Low
      digitalWrite(9, LOW);
      digitalWrite(10, LOW); //Motor 1 Low
      digitalWrite(11, LOW);
      digitalWrite(12, LOW); // Motor 2 Low
      digitalWrite(13, LOW);
      digitalWrite(6, HIGH); //Motor Power High
      digitalWrite(7, HIGH);
    }
  }
}

很抱歉包括这么多,但我不确定我的错误在哪里。

编辑:当我在python代码中只包含ser.write(32)而不是ser.write(str(32))时,我已经请求包含错误的回溯:

Traceback (most recent call last):
  File "C:\Python27\lib\lib-tk\Tkinter.py", line 1536, in __call__
    return self.func(*args)
  File "C:/Users/Radio Astro 4/PycharmProjects/untitled/DishControl.py", line 46, in UP
    ser.write(32)  # convert "1" to ASCII and send it via serial port (USB) to arduino.
  File "C:\Python27\lib\site-packages\serial\serialwin32.py", line 283, in write
    data = to_bytes(data)
  File "C:\Python27\lib\site-packages\serial\serialutil.py", line 75, in to_bytes
    for item in seq:
TypeError: 'int' object is not iterable

1 个答案:

答案 0 :(得分:1)

Arduino documentation说明了Serial.read()命令:

  

返回

     

可用的传入串行数据的第一个字节(如果没有数据,则为-1) - int

您正在发送字符串" 32"对于微控制器,这实际上是两个字节[51, 50]被发送。检查if(Serial.read() == 32)时,检查字节值 32,但永远不会发送字节值32(ASCII空格)。

更改send命令直接传输字节值:

ser.write(32)  # without str()

编辑:

Serial.write()将(byte-)字符串作为参数,因此您必须执行以下操作:

ser.write(bytes([32]))  # convert the list `[32]` to a bytestring