Python 3 CSV整数到字节+ \ n [英] Python 3 CSV integers to bytes + \n

查看:93
本文介绍了Python 3 CSV整数到字节+ \ n的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用python中的串行通信与Arduino进行通信. arduino有此程序 https://www.arduino.cc/zh-CN/Tutorial/ReadASCIIString .只需发送"120,200,100"即可控制3个LED.当我在python中尝试将数据写入arduino时

I'am Trying to Communicate with Arduino using serial communication in python. There is this program from arduino https://www.arduino.cc/en/Tutorial/ReadASCIIString . Simply sending a " 120,200,100" to control 3 LEDS. When I tried it in python writing data to the arduino so simply

arduino.write(b'120,10,244 \ n')

arduino.write(b'120,10,244\n')

,并且有效.但是我的主要问题是,如果我将这些值分配给通过GUI进行更改的变量(例如,我计划在其上实现的PyQT滑块),我应该怎么做?

and it works. But my main problem is that if I assigned those values to a variable which gets change through a GUI for example a PyQT slider which I'am planning to implement it on, How should I go about this?

如何输出分配给变量的3个整数->到csv->字节+ \ n

How to output 3 integers assigned to variables->to csv -> bytes +\n

例如

P1 = self.PWM1horizo​​ntalSlider.value()#假定值为120

P1 = self.PWM1horizontalSlider.value() # ASSUMING a value of 120

P2 = self.PWM2horizo​​ntalSlider.value()#假定值为200

P2 = self.PWM2horizontalSlider.value() # ASSUMING a value of 200

P3 = self.PWM3horizo​​ntalSlider.value()#假定值为100

P3 = self.PWM3horizontalSlider.value() # ASSUMING a value of 100

变成b'120,200,100 \ n'

into b'120,200,100\n'

读取ASCII字符串代码

Read ASCII String Code

// pins for the LEDs:
const int redPin = 3;
const int greenPin = 5;
const int bluePin = 6;

void setup() {
  // initialize serial:
  Serial.begin(9600);
  // make the pins outputs:
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);

}

void loop() {
  // if there's any serial available, read it:
  while (Serial.available() > 0) {

    // look for the next valid integer in the incoming serial stream:
    int red = Serial.parseInt();
    // do it again:
    int green = Serial.parseInt();
    // do it again:
    int blue = Serial.parseInt();

    // look for the newline. That's the end of your sentence:
    if (Serial.read() == '\n') {
      // constrain the values to 0 - 255 and invert
      // if you're using a common-cathode LED, just use "constrain(color, 0, 255);"
      red = 255 - constrain(red, 0, 255);
      green = 255 - constrain(green, 0, 255);
      blue = 255 - constrain(blue, 0, 255);

      // fade the red, green, and blue legs of the LED:
      analogWrite(redPin, red);
      analogWrite(greenPin, green);
      analogWrite(bluePin, blue);

      // print the three numbers in one string as hexadecimal:
      Serial.print(red, HEX);
      Serial.print(green, HEX);
      Serial.println(blue, HEX);
    }
  }
}

推荐答案

要在示例之外回答您的问题,

To answer your question, outside of the example:

p1 = 120
p2 = 200
p3 = 100

the_bytes = bytes(f'{p1},{p2},{p3}\n', 'utf-8')

这是假设您希望字节使用UTF-8作为编码,这很常见,但是您需要检查一下.也可能类似于cp1252-更多信息,请参见 https://docs.python.org/3/library/codecs.html#standard-encodings

This is assuming you want the bytes to use UTF-8 as an encoding, which is common, but something you'd want to check. It could also be something like cp1252 - more here https://docs.python.org/3/library/codecs.html#standard-encodings

然后您可以在需要的任何地方发送the_bytes.

You could then send the_bytes wherever you need them.

这篇关于Python 3 CSV整数到字节+ \ n的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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