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:
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
Lexyth
November 26, 2019, 11:13pm
2
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.
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
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
kll
November 27, 2019, 2:41am
4
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 )
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
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 here
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 ;
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
Lexyth
November 28, 2019, 9:16pm
6
—— This is wrong —— (Mixed Font with PDF Fonts-.- apparently there‘s a difference )
Use this to check wether the characters are encodable.
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.
Thanks Lexyth
I guess I am not formating this correctly.
Here is how I put it:
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
Lexyth
December 1, 2019, 10:53am
8
You forgot to close your if BTPort != null statement. You‘re missing a closing curled bracket ‘}’ right before the last }.
glv
December 1, 2019, 11:52am
9
Hello,
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”
Lexyth
December 1, 2019, 3:29pm
10
——This is wrong ——
@glv currentFont refers to the font object that @laptophead uses.
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.
glv
December 1, 2019, 3:43pm
11
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.
Lexyth
December 1, 2019, 4:00pm
12
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 )
It‘s pretty simple actually…
glv
December 3, 2019, 11:00am
13
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).
Minimal code will be posted here later once I clean it up.