将字符串从 pyserial 发送到 arduino
Send string from pyserial to arduino
你好,我需要将 spotify 歌曲名称从 python 发送到 arduino,但 arduino 只收到像“122”、“117”等数字。如何发送所有字符串?这是我的代码
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代码:
#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);
}
}
为了显示一个字符使用lcd.write
http://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());
}
}
}
请阅读手册并参考Arduino的示例和教程
你好,我需要将 spotify 歌曲名称从 python 发送到 arduino,但 arduino 只收到像“122”、“117”等数字。如何发送所有字符串?这是我的代码
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代码:
#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);
}
}
为了显示一个字符使用lcd.write
http://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()); } } }
请阅读手册并参考Arduino的示例和教程