# Python TO Serial TO Arduino. Send a string?

**URL:** <https://forum.arduino.cc/t/python-to-serial-to-arduino-send-a-string/428465>\
**Category:** Interfacing w/ Software on the Computer\
**Created:** [January 2, 2017, 11:25pm UTC](https://forum.arduino.cc/t/python-to-serial-to-arduino-send-a-string/428465 "2017-01-02T23:25:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![withayk](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/withayk/32/1139276_2.png) [@withayk](https://forum.arduino.cc/u/withayk)\
**Post date:** [January 2, 2017, 11:25pm UTC](https://forum.arduino.cc/t/python-to-serial-to-arduino-send-a-string/428465/1 "2017-01-02T23:25:55Z")

</div>

Hey team, I'm struggling to send a string (multiple characters) to Arduino. Here is what I currently have in Arduino:

```cpp
int my_led = 13;
String IncomingData = "";
String Temp = "";
char var;

void setup()
{
  Serial.begin(9600);
  Serial.println("Ready");
  pinMode(my_led, OUTPUT);
}
void loop()
{
  while(Serial.available())
  {
    var = Serial.read();
    Temp = String(var);
    IncomingData+= Temp;
  }
 if (IncomingData == "HHH"){
    digitalWrite(my_led, HIGH);
 }
 if (IncomingData == "LLL"){
    digitalWrite(my_led, LOW);
 }
 IncomingData = "";
}

```

And in Python:

```cpp
import serial
from tkinter import *

def led_on():
    arduinoData.write('HHH'.encode())
def led_off():
    arduinoData.write('LLL'.encode());

led_control_window = Tk()
led_control_window.minsize(width=266, height=266)
led_control_window.maxsize(width=266, height=266)

btn = Button(led_control_window,text="led on",command=led_on)
btn2 = Button(led_control_window,text="led off",command=led_off)

btn.pack()
btn2.pack()

arduinoData = serial.Serial('com5', 9600)

mainloop()

```

Really hope this make sense to somebody. Basically, It works when sending a single letter ('h' instead of 'hhh'). But for my next project (piezo + note) I need to send a string... ('1C440') - Pin-Note-Pitch.  
Thank for any advice you might have.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [January 3, 2017, 10:25am UTC](https://forum.arduino.cc/t/python-to-serial-to-arduino-send-a-string/428465/2 "2017-01-03T10:25:28Z")

</div>

```cpp
  while(Serial.available())
  {
    var = Serial.read();
    Temp = String(var);
    IncomingData+= Temp;
  }

```

Why do you think you need to wrap the character in a String, in order to append it to a String?

Why do you think that all the serial data will arrive at once? IT WILL NOT.

There is a reason that decent serial communications protocols use start and end markers. See Robin2's page on serial data handling: [Serial Input Basics - updated - Introductory Tutorials - Arduino Forum](http://forum.arduino.cc/index.php?topic=396450.0)

---

<div class="post-metadata">

**Author:** ![Robin2](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/robin2/32/216045_2.png) [@Robin2](https://forum.arduino.cc/u/Robin2)\
**Post date:** [January 3, 2017, 12:02pm UTC](https://forum.arduino.cc/t/python-to-serial-to-arduino-send-a-string/428465/3 "2017-01-03T12:02:15Z")

</div>

This [Python - Arduino demo](http://forum.arduino.cc/index.php?topic=225329.msg1810764#msg1810764) may also be useful. Just be aware that it was written before I wrote the tutorial in the link that @PaulS has posted.

...R

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 6, 2021, 11:32am UTC](https://forum.arduino.cc/t/python-to-serial-to-arduino-send-a-string/428465/4 "2021-05-06T11:32:45Z")

</div>

Closed on May 6, 2021, 11:32am UTC


