Arduino和Python之间的串行通信,使用十六进制值的问题 [英] Serial Communication between Arduino and Python, issue of using hexidecimal values

查看:171
本文介绍了Arduino和Python之间的串行通信,使用十六进制值的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过Python 3.4中的代码从计算机启动电动机,使用pySerial与Arduino Uno通信.我已经打包了要发送给十六进制的值,所以一次只能有一个字节,但是在Python端发送时,在Arduino端获取正确的数字时遇到了问题.

I am attempting to start a motor from the computer by code in Python 3.4, using pySerial to communicate to an Arduino Uno. I have packed the value I am sending to hexidecimal, so I only have one byte at a time, but am having a problem getting the correct number on the Arduino side as I am sending on the Python side.

PYTHON代码:

import serial 
import struct


ser = serial.Serial(
    port ='COM4', 
    baudrate = 9600, 
    parity = serial.PARITY_ODD, 
    stopbits = serial.STOPBITS_TWO, 
    bytesize = serial.EIGHTBITS
    )

#ser.open()     #opens port 
ser.isOpen()    #returns true?


motorState = 0
wristBend = 'Left'

while True:
    #need to create options to send to arduino

    if wristBend == 'Left':
        motorState = 2
    elif wristBend == 'Right':
        motorState = 3
    else:
        motorState = 1

    motorChar = struct.pack('<B', motorState)          #returns the value as a character interger
    #motorChar = str(hex(motorState))

    print(motorChar)
    ser.write(motorChar)
    print(ser.read())
    break

ARDUINO代码:

ARDUINO CODE:

int motorPinLeft = 10;
int motorPinRight = 11; 
int motorSpeedLeft = 0;
int motorSpeedRight = 0;


void setup() {
  pinMode(motorPinLeft, OUTPUT);
  pinMode(motorPinRight, OUTPUT);
  Serial.begin(9600);
  while(!Serial); //waits for arduino to be ready, not neaded for duo
  Serial.println("Speed 0 - 255");
}

void loop() 
{
  if (Serial.available())
  {
    int ch = Serial.read();

      switch (ch)
      {
        case 1:
          motorSpeedLeft = 0;
          motorSpeedRight = 0;
          break;
        case 2:
          motorSpeedLeft = 127;
          motorSpeedRight = 0;
          break;
        case 3:
          motorSpeedLeft = 0;
          motorSpeedRight = 127;
          break;

      }

      Serial.write(ch);

      analogWrite(motorPinLeft, motorSpeedLeft);
      analogWrite(motorPinRight, motorSpeedRight);

      delay(2500);     //wait for 2.5 seconds to make the motor vibrate
      motorSpeedRight = 0;
      motorSpeedLeft = 0;

      analogWrite(motorPinLeft, motorSpeedLeft);
      analogWrite(motorPinRight, motorSpeedRight);

不仅没有任何东西与我的电路通信,而且还有我的python代码的输出,我在其中打印要发送到Arduino的内容和从Arduino发送的内容:

Not only is nothing communicating with my circuit, but the output from my python code, where I print what is being sent to the Arduino and what is being sent from the Arduino is this:

b'\x02'
b'S'

如果我将切换用例代码更改为83(S的ASCII代码),或将变量的类型更改为byte,int,uint8_t,则会得到完全相同的输出.我在这里做错了什么?抱歉,如果它很明显,我对python和arduino还是很陌生.预先感谢您的帮助!

If I change a switch case code to 83 (the ASCII code of S), or change the type of the variable to byte, int, uint8_t, I get the exact same output. What am I doing wrong here? Sorry if it is sort of obvious, I'm fairly new to python and arduino. Thanks in advance for any help!

推荐答案

在这里举一个简单的例子并不难,通常是一个好主意,从简单开始,然后将功能增加到您想要的.

its not hard here is a simple example, its usually a good idea to start simple and then increase the functionality to what you want.

test_serial.py

test_serial.py

import serial
ser = serial.Serial("COM4",timeout=5) # everything else is default
ser.write("\x45")
print "RECIEVED BACK:",repr(ser.read(5000))

test_serial.ino

test_serial.ino

int incomingByte = 0;   // for incoming serial data

void setup() {
        Serial.begin(9600);     // opens serial port, sets data rate to 9600 bps
}

void loop() {

        // send data only when you receive data:
        if (Serial.available() > 0) {
                // read the incoming byte:
                incomingByte = Serial.read();

                // say what you got:
                Serial.print("I received: ");
                Serial.println(incomingByte, DEC);
        }
}

这篇关于Arduino和Python之间的串行通信,使用十六进制值的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆