# Read Serial Value (From Arduino)

**URL:** <https://discourse.processing.org/t/read-serial-value-from-arduino/5829>\
**Category:** Processing.py\
**Created:** [November 22, 2018, 12:54pm UTC](https://discourse.processing.org/t/read-serial-value-from-arduino/5829 "2018-11-22T12:54:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![KeirMeDear](https://avatars.discourse-cdn.com/v4/letter/k/8c91f0/32.png) [@KeirMeDear](https://discourse.processing.org/u/KeirMeDear)\
**Post date:** [November 22, 2018, 12:54pm UTC](https://discourse.processing.org/t/read-serial-value-from-arduino/5829/1 "2018-11-22T12:54:19Z")

</div>

Hey,

so all I’m trying to do is use processing to read a value from the Serial port of an arduino. I have already connected I just don’t know the correct python method to read the value.

Current code:

```auto
def setup():
    global arduino
    size(10,10)
    
    println(Arduino.list())
    
    arduino = Arduino(this, Arduino.list()[1], 9600)
    
def draw():
    #val = arduino.analogRead(0)
    
    for i in range(10):
        val = arduino.readString(i) #<----------------- Here 'readString' doesn't work, what should this be?
        print("{} ___ {}".format(frameCount,val))
    delay(10000)

```

Many thanks

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [November 22, 2018, 1:51pm UTC](https://discourse.processing.org/t/read-serial-value-from-arduino/5829/2 "2018-11-22T13:51:52Z")

</div>

This is my lousy attempt to convert an old Java Mode sketch from the link below to Python Mode: 🐍

- [Forum.Processing.org/two/discussion/16618/processing-with-arduino-void-serialevent#Item\_5](http://Forum.Processing.org/two/discussion/16618/processing-with-arduino-void-serialevent#Item_5)

```python
"""
 * Efficient Serial Reading (v1.01)
 * GoToLoop (2018-Nov-22)
 *
 * Discourse.Processing.org/t/read-serial-value-from-arduino/5829/2
 *
 * Forum.Processing.org/two/discussion/16618/
 * processing-with-arduino-void-serialevent#Item_5
"""

add_library('serial')

PORT_INDEX, BAUDS = 0, 9600
myString = ''

def setup():
    noLoop()
    ports = Serial.list()
    Serial(this, ports[PORT_INDEX], BAUDS).bufferUntil(ENTER)

def draw(): print myString

def serialEvent(s):
    global myString
    myString = s.readString().trim()
    redraw()

```

Since I don’t have any of such hardware, I can’t tell whether it’ll work or not. 😶

You should ask in the repo below if it doesn’t work for you: 🤕

> <https://github.com/jdf/Processing.py-Bugs/issues/157>

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [June 11, 2022, 9:11pm UTC](https://discourse.processing.org/t/read-serial-value-from-arduino/5829/3 "2022-06-11T21:11:44Z")

</div>

> [@GoToLoop](#):
>
> Since I don’t have any of such hardware, I can’t tell whether it’ll work or not. 😶

It does not work.

This will work with Processing 3.5.4 Python mode:

```auto
# Python Serial Reading
# v1.0.0
# GLV 2022-06-11

add_library("serial")
myPort = Serial(this, "COM2", 9600)
inString = ""

def setup():
    size(400, 100)

    # This does not work:
    #myPort.bufferUntil(ENTER)
    
    # This works:
    myPort.bufferUntil(0x0A)
    
    textSize(24);
    textAlign(LEFT, CENTER);
    fill(0);
    noLoop()
    
def draw():
    background(255); # I needed something here
    text (inString, 50, height/2);
    
def serialEvent(Serial):
    global inString
    
    # This does not work:
    # inString = myPort.readString().trim() 
    
    # This works:
    inString = myPort.readString()
    inString = trim(inString)
    
    println(inString)
    
    arr = inString.split(",")
    for i in arr:
        print(i)
    
    redraw()        

```

Above code does NOT work with Processing 4.0.b8

You can use a virtual serial port if you do not have hardware.  
There is one available here:  
[https://www.hhdsoftware.com/](https://www.hhdsoftware.com/)  
See bottom for free version.

Use any serial software or code of your choice to send serial data.  
I used the Serial Monitor that comes with Arduino software (hardware not required) to send data.

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/a/e/aedde0c6c2cb07da97eae9e5b86fce1c002a919d.png)

This is the link to the free virtual serial port software that I tested:

> **[FREE Virtual Serial Ports driver, Rs-232 null modem emulator](https://freevirtualserialports.com/)**
>
> Freeware virtual com port driver and Rs232/Rs485/Rs422 serial ports null-modem cable emulator

Disclaimer: Use at your own risk!  
This is just one of many options out there.

`:)`
