# Can I Sort my incoming String based on the first char?

**URL:** <https://discourse.processing.org/t/can-i-sort-my-incoming-string-based-on-the-first-char/15847>\
**Category:** Electronics (Arduino, etc.)\
**Created:** [November 26, 2019, 9:55pm UTC](https://discourse.processing.org/t/can-i-sort-my-incoming-string-based-on-the-first-char/15847 "2019-11-26T21:55:22Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![laptophead](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/laptophead/32/2701_2.png) [@laptophead](https://discourse.processing.org/u/laptophead)\
**Post date:** [November 26, 2019, 9:55pm UTC](https://discourse.processing.org/t/can-i-sort-my-incoming-string-based-on-the-first-char/15847/1 "2019-11-26T21:55:23Z")

</div>

I am getting a lot of data from an Arduino Serial port.  
The problem is that some are numbers , some are sentences, etc

While I can split a String in Floats, that string can have only numbers.

So I decided to have several transmission from the Arduino. It makes the burst shorter and does not block my Arduino sketch for so long.

Here is how I tried to separate the stings and then split them:

```auto
 
    while (BTPort.available() > 0) {
    inBuffer = BTPort.readStringUntil(lf);  
    if (inBuffer != null) {
      println(inBuffer);
      c1 = inBuffer.charAt(0);
    }
  } // end of read string
  
  if (c1 == 'e')
  {
  float[] errs = float(split(inBuffer, ' '));
  // pos 0 is the "e
  X_Err = errs[1] ;
  Y_Err = errs[2] ;
  Z_Err = errs[3] ;
  T_Err = errs[4] ;
  }

else 
{
 float[] nums = float(split(inBuffer, ' '));
// nums[0] 
// String[] list = split(inBuffer, " ");
  BaseEncGlobal = nums[0] ; 
  ShoEncGlobal = nums[1] ;
  ElbowEncGlobal = nums[2] ;
  VertRotEnc = nums[3] ;
  HorRotEnc = nums[4] ;
  GripEncGlobal = nums[5] ;
  X_axis = nums[6] ;
  Y_axis = nums[7] ;
  Z_axis = nums[8] ;
  HorAngRad = nums[9] ;
  VerAngRad = nums[10] ;
  Mode = nums[11] ;
  TheStepNo = nums[12] ;
  GripCommand = nums[13] ;
  CommandSent = nums[14] ;
  CommandCompleted = nums[14] ;

}
  

```

the two strings seem nice and clean. Here they are from the print command:

158018 70222 149288 44793 12944 94 -96.0 -305.8 129.4 221.9 45.2 1 -1 0 0 0

e 96.0 305.8 129.4 221.9

What do I get:

All values in the “e” strings are correct, but the other string (in the else) does not split.  
I get all zeros.  
Please help.

Thank you,

Mitch

---

<div class="post-metadata">

**Author:** ![Lexyth](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lexyth/32/7403_2.png) [@Lexyth](https://discourse.processing.org/u/Lexyth)\
**Post date:** [November 26, 2019, 11:13pm UTC](https://discourse.processing.org/t/can-i-sort-my-incoming-string-based-on-the-first-char/15847/2 "2019-11-26T23:13:46Z")

</div>

It‘s probably not that it doesn‘t split, but that it‘s never happening. Although i can‘t say that with certainty, try adding a println() in the Else section and see if it‘s ever printed.

---

<div class="post-metadata">

**Author:** ![laptophead](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/laptophead/32/2701_2.png) [@laptophead](https://discourse.processing.org/u/laptophead)\
**Post date:** [November 27, 2019, 12:01am UTC](https://discourse.processing.org/t/can-i-sort-my-incoming-string-based-on-the-first-char/15847/3 "2019-11-27T00:01:34Z")

</div>

Thanks,  
I started working on it and I went to a Serial event solution.  
I think it keeps things cleaner.

I put all my splitting in a function and terminated each “if” with a inBuffer = null;  
This will clean slate for the next transmission.

Here is my Event

```auto
void serialEvent(Serial BTPort) {
inBuffer = BTPort.readStringUntil(lf);  
      if (inBuffer != null)
      {println(inBuffer);
      c1 = inBuffer.charAt(0);
      DataSplit();
      }  
}

```

By the way, I am having a terrible time with “COM3 port busy” every time I re-open the sketch,

Any ideas?  
Thanks

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [November 27, 2019, 2:41am UTC](https://discourse.processing.org/t/can-i-sort-my-incoming-string-based-on-the-first-char/15847/4 "2019-11-27T02:41:08Z")

</div>

thanks for posting the split code,  
but in that form we only can guess what might go wrong.

but for test, to get something run-able, must put in some more minutes and make a MVCE  
i show you what i tried and the results.

( as close as possible to your code  
but without having that many variables / you not posted the declaration )

```auto
String inBuffer;

inBuffer = "158018 70222 149288 44793 12944 94 -96.0 -305.8 129.4 221.9 45.2 1 -1 0 0 0";
//inBuffer = "e 96.0 305.8 129.4 221.9";

char c1 = inBuffer.charAt(0);

if (c1 == 'e') {
  float[] errs = float(split(inBuffer, ' '));
  println(errs);
} else {
  float[] nums = float(split(inBuffer, ' '));
  println(nums);
}

/*

[0] 158018.0
[1] 70222.0
[2] 149288.0
[3] 44793.0
[4] 12944.0
[5] 94.0
[6] -96.0
[7] -305.8
[8] 129.4
[9] 221.9
[10] 45.2
[11] 1.0
[12] -1.0
[13] 0.0
[14] 0.0
[15] 0.0
*/

/*
[0] NaN
[1] 96.0
[2] 305.8
[3] 129.4
[4] 221.9
*/

```

in other words, with the provided 2 lines your problem could not be reproduced here.

* * *

to the serial code, is might help to use also the  
[https://processing.org/reference/trim\_.html](https://processing.org/reference/trim_.html)  
on the “inBuffer” to clean it.

* * *

also about the inBuffer eval,  
do use data only if you sure they exist! as i posted  
[Trouble with the Serial port read](https://discourse.processing.org/t/trouble-with-the-serial-port-read/10724/4) here

---

<div class="post-metadata">

**Author:** ![laptophead](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/laptophead/32/2701_2.png) [@laptophead](https://discourse.processing.org/u/laptophead)\
**Post date:** [November 28, 2019, 8:57pm UTC](https://discourse.processing.org/t/can-i-sort-my-incoming-string-based-on-the-first-char/15847/5 "2019-11-28T20:57:00Z")

</div>

I made a Data split based on the lenght of the string and works a lot better.

However after 5-10 min I still get:  
Error, disabling serialEvent() for COM4  
null  
No glyph found for the ? (\uFFFD) character

Out of the blue.

Here is my new splitting ;

```auto
void serialEvent(Serial BTPort) {
  if (BTPort != null) 
  {//println("All Good");
    inBuffer = BTPort.readStringUntil(lf);  
    if (inBuffer != null)
    { 
      inBufferLenght = inBuffer.length();
      // println (inBufferLenght);
      println(inBuffer);
      DataSplit();

    
    }
  }
}

data splt goes
void DataSplit()
{

  

if (inBufferLenght <40) {
  Message = inBuffer;//.substring(1, 29); //
   
} 

else {
 float[] nums = float(split(inBuffer, ' '));

    BaseEncGlobal = nums[0] ; 
    ShoEncGlobal = nums[1] ;
    ElbowEncGlobal = nums[2] ;

etc

```

What to do about the gliph? Mabye is just noise, but how do I recover the sketch and keep running , processing the serial stream?

Thanks a lot

Mitch

---

<div class="post-metadata">

**Author:** ![Lexyth](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lexyth/32/7403_2.png) [@Lexyth](https://discourse.processing.org/u/Lexyth)\
**Post date:** [November 28, 2019, 9:16pm UTC](https://discourse.processing.org/t/can-i-sort-my-incoming-string-based-on-the-first-char/15847/6 "2019-11-28T21:16:59Z")

</div>

—— This is wrong —— (Mixed Font with PDF Fonts-.- apparently there‘s a difference 😑 )

Use this to check wether the characters are encodable.

```auto
try {
        currentFont.getFont.decode(Character.toString(character));
        return true;
    } catch (IllegalArgumentException iae) {
        println(„Error, character not supported“);
        return false;
    }

```

If it returns false you could just ignore the whole string.

---

<div class="post-metadata">

**Author:** ![laptophead](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/laptophead/32/2701_2.png) [@laptophead](https://discourse.processing.org/u/laptophead)\
**Post date:** [December 1, 2019, 1:13am UTC](https://discourse.processing.org/t/can-i-sort-my-incoming-string-based-on-the-first-char/15847/7 "2019-12-01T01:13:58Z")

</div>

Thanks Lexyth

I guess I am not formating this correctly.

Here is how I put it:

```auto
void serialEvent(Serial BTPort) 
{
  if (BTPort != null) 
  {
   

    inBuffer = BTPort.readStringUntil(lf);  
    if (inBuffer != null)
    { 
      inBufferLenght = inBuffer.length();
      
        currentFont.encode(Character.toString(character));
        {return true;
        }
        catch (IllegalArgumentException iae)
        {
        println("Error, character not supported");
        return false;
        }
        
      println(inBuffer);
      DataSplit();
    }
  
}

```

but it does not compile,

I get a:  
“Found one too many { characters without a } to match it.”

What to do?  
Thanks

---

<div class="post-metadata">

**Author:** ![Lexyth](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lexyth/32/7403_2.png) [@Lexyth](https://discourse.processing.org/u/Lexyth)\
**Post date:** [December 1, 2019, 10:53am UTC](https://discourse.processing.org/t/can-i-sort-my-incoming-string-based-on-the-first-char/15847/8 "2019-12-01T10:53:15Z")

</div>

You forgot to close your if BTPort != null statement. You‘re missing a closing curled bracket ‘}’ right before the last }.

---

<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:** [December 1, 2019, 11:52am UTC](https://discourse.processing.org/t/can-i-sort-my-incoming-string-based-on-the-first-char/15847/9 "2019-12-01T11:52:05Z")

</div>

Hello,

> [@Lexyth](#):
>
> try { currentFont.encode(Character.toString(character)); return true; } catch (IllegalArgumentException iae) { println(„Error, character not supported“); return false; }

Can you please provide a simple working example of this? And a reference please.  
Example does not have to be integrated with the code in topic.

I am stuck on:

> Cannot find anything named “currentFont”

🙂

---

<div class="post-metadata">

**Author:** ![Lexyth](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lexyth/32/7403_2.png) [@Lexyth](https://discourse.processing.org/u/Lexyth)\
**Post date:** [December 1, 2019, 3:29pm UTC](https://discourse.processing.org/t/can-i-sort-my-incoming-string-based-on-the-first-char/15847/10 "2019-12-01T15:29:45Z")

</div>

——This is wrong ——

@glv currentFont refers to the font object that @laptophead uses.

```auto
import java.lang.Character;
import java.lang.IllegalArgumentException;

PFont currentFont;

void setup() {
   size(800, 600);
   currentFont = loadFont("YourFont.vlw");
   textFont(currentFont, 32);
   text("Hello", 50, 50);
   checkIfValidFont('a');
}

void checkIfValidFont(char a) {
   try {
      currentFont.getFont().decode(Character.toString(a));
      return true;
   } catch (IllegalArgumentException iae) {
      println("Error, char not supported");
   }
}

```

Actually, i forgot about getting the Font last time, so i edited the Code. Processings PFont doesn‘t have an encode function…

This should pretty much work, but i can‘t Test it myself right now. (Can‘t access my PC…).

Do the import statements suffice as references? Oh and the PFont class maybe, which uses the java.awt.Font to store the Font for PGraphics faster/easier use.

---

<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:** [December 1, 2019, 3:43pm UTC](https://discourse.processing.org/t/can-i-sort-my-incoming-string-based-on-the-first-char/15847/11 "2019-12-01T15:43:34Z")

</div>

Thanks,

This is the error now and I did add a font:

> The function encode(String) does not exist.

Not urgent for me. I was just interested.

🙂

---

<div class="post-metadata">

**Author:** ![Lexyth](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lexyth/32/7403_2.png) [@Lexyth](https://discourse.processing.org/u/Lexyth)\
**Post date:** [December 1, 2019, 4:00pm UTC](https://discourse.processing.org/t/can-i-sort-my-incoming-string-based-on-the-first-char/15847/12 "2019-12-01T16:00:40Z")

</div>

😅 Well, apparently it‘s decode, not encode… i tend to overlook such small differences 😅

Edit : no it‘s not… you know what, i‘ll tell you once i found out where i got that from 😅

Edit : ok, nvm what i wrote!!! That‘s all only for PDF fonts 😑

Here‘s the correct way to do it in Processing (a link to someone else having the same question answered by someone who doesn‘t mess everything up 😅 )

> **[Processing 2.x and 3.x Forum](https://forum.processing.org/two/discussion/10107/how-to-detect-no-glyph-char)**
>
> Processing is an electronic sketchbook, a language and a worldwide community. This is its forum.

It‘s pretty simple actually…

---

<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:** [December 3, 2019, 11:00am UTC](https://discourse.processing.org/t/can-i-sort-my-incoming-string-based-on-the-first-char/15847/13 "2019-12-03T11:00:41Z")

</div>

Hello,

I “strung” together (no pun intended) some minimal code to send data (strings) from Arduino to Processing.

It was running all night and I only got a few “short” ones overnight out of 429455; 1 at the start of sketch and then 2 later.  
I may consider adding logging; I suspect it is when I checked in on progress (twice).

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/8/8581426c49839585f294ce6fc04f51b0e2bb6d35.png)

Minimal code will be posted here later once I clean it up.

🙂
