简体   繁体   English

将字符串从 pyserial 发送到 arduino

[英]Send string from pyserial to arduino

Hello i need to send spotify song name from python to arduino, but arduino receive only numbers like '122', '117' etc. How to send all string?您好,我需要将 Spotify 歌曲名称从 python 发送到 arduino,但 arduino 只接收“122”、“117”等数字。如何发送所有字符串? Here is my code这是我的代码

python code: python 代码:

import serial
import time
from SwSpotify import spotify

ser = serial.Serial('COM3', 9600)

while True:
   print(spotify.song())
   ser.write(spotify.song().encode())
   time.sleep(5)

Arduino code: Arduino 代码:

#include <LiquidCrystal_PCF8574.h>
#include <Wire.h>

LiquidCrystal_PCF8574 lcd(0x27); // set the LCD address to 0x27 for a 16 chars and 2 line display


int incomingByte;     

void setup() {
  Serial.begin(9600);
  lcd.begin(16, 2); 
  lcd.clear();
  lcd.setBacklight(255);
  lcd.print("Hello!");
}

void loop() {
  if (Serial.available() > 0) {
    incomingByte = Serial.read();
    lcd.clear();
    lcd.print(incomingByte);
  }
}

In order to display a character use lcd.write为了显示一个字符使用lcd.write

http://www.arduino.cc/en/Tutorial/LiquidCrystalSerialDisplayhttp://www.arduino.cc/en/Tutorial/LiquidCrystalSerialDisplay

 */ // include the library code: #include <LiquidCrystal.h> // initialize the library by associating any needed LCD interface pin // with the arduino pin number it is connected to const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2; LiquidCrystal lcd(rs, en, d4, d5, d6, d7); void setup() { // set up the LCD's number of columns and rows: lcd.begin(16, 2); // initialize the serial communications: Serial.begin(9600); } void loop() { // when characters arrive over the serial port... if (Serial.available()) { // wait a bit for the entire message to arrive delay(100); // clear the screen lcd.clear(); // read all the available characters while (Serial.available() > 0) { // display each character to the LCD lcd.write(Serial.read()); } } }

Please read manuals and refer to Arduino's examples and tutorials请阅读手册并参考 Arduino 的示例和教程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM