# Data exchanges via PORTS - Arduino and Processing

**URL:** <https://discourse.processing.org/t/data-exchanges-via-ports-arduino-and-processing/22264>\
**Category:** Electronics (Arduino, etc.)\
**Tags:** homework\
**Created:** [June 29, 2020, 11:23pm UTC](https://discourse.processing.org/t/data-exchanges-via-ports-arduino-and-processing/22264 "2020-06-29T23:23:01Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![kmot](https://avatars.discourse-cdn.com/v4/letter/k/919ad9/32.png) [@kmot](https://discourse.processing.org/u/kmot)\
**Post date:** [June 29, 2020, 11:23pm UTC](https://discourse.processing.org/t/data-exchanges-via-ports-arduino-and-processing/22264/1 "2020-06-29T23:23:01Z")

</div>

**BASIC QUESTION** : : reading and / writing short data streams (char, int, binary. strings).VIA THE ports

The code for each side (IDE and PDE) is from a tutorial web site: [https://forum.arduino.cc/index.php?topic=434516.0](https://forum.arduino.cc/index.php?topic=434516.0)  
The IDE does read the individual characters for a key-in on the "Serial Monitor,: using Serial.read() coding.  
The PDE uses “myPort.write” code to put _something somewhere_. The IDE code does not registers anything on the port, not even the number of “available” items on the port.

I will copy/paste the IDE and PDE code below. (This q/a systems needs an attachment facility).  
----------- IDE ---------------------------------------------------------------------------------------------  
const byte numChars = 32;  
char receivedChars[numChars]; // an array to store the received data  
boolean newData = false;

void setup() {  
Serial.begin(9600);  
Serial.println("");  
}  
void loop() {  
recvWithEndMarker();  
showNewData();  
}

void recvWithEndMarker() {  
static byte ndx = 0;  
char endMarker = ‘|’;  
char rc;  
while (Serial.available() \> 0 && newData == false) {  
rc = Serial.read();  
if (rc != endMarker) {  
receivedChars[ndx] = rc;  
ndx++;  
if (ndx \>= numChars) {  
ndx = numChars - 1;  
}  
}  
else {  
receivedChars[ndx] = ‘\0’; // terminate the string  
ndx = 0;  
newData = true;  
}  
}  
---------- PDE -----------------------------------------------------------  
size(200, 200); //make our canvas 200 x 200 pixels big  
String portName = Serial.list()[0]; //change the 0 to a 1 or 2 etc. to match your port  
myPort = new Serial(this, “COM5”, 9600);  
}

void draw() {

if (mousePressed == true)  
{ //if we clicked in the window  
myPort.write(“hallo”); //send a 1  
println(“test”);  
} else  
{ //otherwise  
myPort.write(‘0’); //send a 0  
}  
}

---

<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 30, 2020, 9:36pm UTC](https://discourse.processing.org/t/data-exchanges-via-ports-arduino-and-processing/22264/2 "2020-06-30T21:36:11Z")

</div>

Hello,

Please format your code:  
[https://discourse.processing.org/faq#format-your-code](https://discourse.processing.org/faq#format-your-code)

`:)`

---

<div class="post-metadata">

**Author:** ![kmot](https://avatars.discourse-cdn.com/v4/letter/k/919ad9/32.png) [@kmot](https://discourse.processing.org/u/kmot)\
**Post date:** [June 30, 2020, 10:29pm UTC](https://discourse.processing.org/t/data-exchanges-via-ports-arduino-and-processing/22264/3 "2020-06-30T22:29:12Z")

</div>

Ahhhh. Thank you for this information. I have to ackowledge that this is one of the more courteous fora (fora?) I’ve encountered. I will work on the proper (and easier to read) format this evening (June 30).

Thomas (Tom) Kane a.k.a.“kmot”)

---

<div class="post-metadata">

**Author:** ![kmot](https://avatars.discourse-cdn.com/v4/letter/k/919ad9/32.png) [@kmot](https://discourse.processing.org/u/kmot)\
**Post date:** [June 30, 2020, 10:44pm UTC](https://discourse.processing.org/t/data-exchanges-via-ports-arduino-and-processing/22264/4 "2020-06-30T22:44:07Z")

</div>

> [@kmot](#):
>
> size(200, 200); //make our canvas 200 x 200 pixels big  
> String portName = Serial.list()[0]; //change the 0 to a 1 or 2 etc. to match your port  
> myPort = new Serial(this, “COM5”, 9600);  
> }
> 
> void draw() {
> 
> if (mousePressed == true)  
> { //if we clicked in the window  
> myPort.write(“hallo”); //send a 1  
> println(“test”);  
> } else  
> { //otherwise  
> myPort.write(‘0’); //send a 0  
> }  
> }

Is this satisfactory?  
kmot

---

<div class="post-metadata">

**Author:** ![kmot](https://avatars.discourse-cdn.com/v4/letter/k/919ad9/32.png) [@kmot](https://discourse.processing.org/u/kmot)\
**Post date:** [June 30, 2020, 10:46pm UTC](https://discourse.processing.org/t/data-exchanges-via-ports-arduino-and-processing/22264/5 "2020-06-30T22:46:25Z")

</div>

My apology. I am not sure if I corrected my post correctly/

---

<div class="post-metadata">

**Author:** ![kmot](https://avatars.discourse-cdn.com/v4/letter/k/919ad9/32.png) [@kmot](https://discourse.processing.org/u/kmot)\
**Post date:** [July 1, 2020, 2:02am UTC](https://discourse.processing.org/t/data-exchanges-via-ports-arduino-and-processing/22264/6 "2020-07-01T02:02:22Z")

</div>

> [@kmot](#):
>
> const byte numChars = 32;  
> char receivedChars[numChars]; // an array to store the received data  
> boolean newData = false;

HOW DO I DELETE ALL THE CODING AND THeN REPLACE IT WITH MUCH LESS CODE?  
TOM KANE (“kmot”)

---

<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:** [July 1, 2020, 11:09am UTC](https://discourse.processing.org/t/data-exchanges-via-ports-arduino-and-processing/22264/7 "2020-07-01T11:09:28Z")

</div>

> [@kmot](#):
>
> Is this satisfactory?

This is formatted:

```auto
String str1 = "CCCP";
char data[] = {'C', 'C', 'C', 'P'};
String str2 = new String(data);
println(str1); // Prints "CCCP" to the console
println(str2); // Prints "CCCP" to the console

```

This is **not** formatted:

String str1 = “CCCP”;  
char data = {‘C’, ‘C’, ‘C’, ‘P’};  
String str2 = new String(data);  
println(str1); // Prints “CCCP” to the console  
println(str2); // Prints “CCCP” to the console

I borrowed above from here because it had single and double quotes:

> **[String / Reference](https://processing.org/reference/String.html)**
>
> A string is a sequence of characters. The class String includes methods for examining individual characters, comparing strings, searching strings, extracting parts of strings, and for convertin…

If it is formatted correctly the community can hover over it and easily copy it in the top right.

If it is **not** formatted it is difficult to read and the quotes have to be corrected if you cut and paste code.  
Try a cut and past into Processing and see the difference.

Use the edit button (looks like a pencil or pen) to edit your original post:  
 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/1/1aba2004cf834ecd781a36006ae150b4f8b53399.png)

[https://discourse.processing.org/faq#format-your-code](https://discourse.processing.org/faq#format-your-code)

`:)`

---

<div class="post-metadata">

**Author:** ![kmot](https://avatars.discourse-cdn.com/v4/letter/k/919ad9/32.png) [@kmot](https://discourse.processing.org/u/kmot)\
**Post date:** [July 1, 2020, 4:21pm UTC](https://discourse.processing.org/t/data-exchanges-via-ports-arduino-and-processing/22264/8 "2020-07-01T16:21:08Z")

</div>

Ahhh. thank you. More space between lines to make reading easier. Good plan.

Thomas (Tom) Kane / “kmot”

---

<div class="post-metadata">

**Author:** ![kmot](https://avatars.discourse-cdn.com/v4/letter/k/919ad9/32.png) [@kmot](https://discourse.processing.org/u/kmot)\
**Post date:** [July 1, 2020, 6:19pm UTC](https://discourse.processing.org/t/data-exchanges-via-ports-arduino-and-processing/22264/9 "2020-07-01T18:19:52Z")

</div>

**"Problem" resolved.** Slower cycling of the two applications (PDE and IDE) demonstrated that the desired data is being sent AND received. Anotehr problem arose, but I will present a new topic to request assistance. Thank you for the admin and editing assistance.  
Thomas Kane
