#how to not read data from serial, for real
1 messages · Page 1 of 1 (latest)
can't understand the bit order from the serial output of the arduino
alr
let me go take that piece the code and some illustrative video
arduino.readSerialPort(output, MAX_DATA_LENGTH);
//cout<<">> "<<output<<endl;
/*
for (int i = 0; i < MAX_DATA_LENGTH; i++)
{
printf("%c", output[i]);
}*/
/*
printf("%c \n", output[0]);
printf("%c \n", output[1]); //bit giusti
printf("%c \n", output[2]);*/
tripl[0] = (output[0] - 48) * 100;
tripl[1] = (output[1] - 48)* 10;
tripl[2] = output[2] - 48;
x = tripl[0] + tripl[1] + tripl[2];
printf("%d \n", x);
I'm reading some bits from the serial port through the random library I found on the internet
the char output has some limit size that is 255, As I understood, 255 should be bytes
now every half second I'm getting the first 3 bytes from the serial output of the arduino, but during the runtime the numbers keep changing and I have no idea how to solve that
https://www.youtube.com/watch?v=tpEo5AOSSkg
I found this video that also explain some of the single bits order, but I'm not sure how this gonna actually affect the char variables
Want to learn more? Check out our courses!
https://bit.ly/3B8YW3y
We designed this circuit board for beginners!
Kit-On-A-Shield: https://amzn.to/3lfWClU
Get your Free Trial of Altium PCB design Software
https://www.altium.com/yt/programmingelectronicsacademy
SHOP OUR FAVORITE STUFF! (affiliate links)
-----------------------------------...
then
I'm also gonna show my arduino code and some of the console output
ok, on the start everything runs perfect, then it start getting some negative random values, then it go back to normal
I'm also trying to change it, but the problem remain
// SerialMouse sketch
//#define potYPin 5
int vale = 0;
void setup()
{
Serial.begin(9600);
}
void loop()
{
int x = analogRead(A0);
vale = map(x, 0, 1023, 0, 255);
//int y = analogRead(potYPin);
Serial.print(vale,DEC);
//Serial.print(",");
delay(50);
//Serial.print(y,DEC);*/
Serial.println(); // send a cr/lf
delay(50); // send position 20 times a second
}
that's the arduino code
It check if connection is estabilished then it start read data from the serial.print from the arduino and saving it in pc side char array named output, I don't understand how to read function works since it comes from the library and the library doesn't have any docs, anyway, after storing the bytes in the char array, i'm converting char to int by subtracting 48 that it should convert from ascii to normal int, then I'm trying to get the full value of the potentiometer and saving it in a variable x, all this for just move the mouse cursor
you are not reading bits btw, you are reading bytes
can you explain more how you convert the chars to int?
you're making an assumption in your code I think
a number in ascii table have a certain id, kinda 0 from ascii table if directly assigned to an int should be 48, 1 should be 49, 2 = 50 and so on
By sub -48 from every char variable i'm getting the real num
Anyway i think that is possible that sometimes arduino changes the order about how data is sent out in the serial, that's because I was talking about the bits
why do you think that?
Idk, I just saw that in the console, I don't honestly understand why all of the sudden values start changing
don't guess unless you have some evidence
That btw doesn't happens in the serial monitor
how about this: could your PC code fail for some numbers?
I'm just thinking what the problem could be
Idk
Parse the input 10
Uhm what?
Imagine the Arduino sends the input 10 to the computer
Ok
how does your code parse that input?
The code starts by reading byte by byte, start with 1 and multiply it by ten times, so it would be 10, then the 0 will remain the same,
In the end the code will just 10+0 = 10
x= 10
Ok, 🤨
Uhm...
Then, 100 will be converted in the ascii table when it goes out from the arduino
The code process every single byte starting by left
1 (converted from ascii would be 49)
The code store that in the array named output
1 = output[0] \first byte from left
(Some temporary variable for store the value) = (output[0]-48) * 100
0=output [1] \ the second byte took out from the array
(Some temporary variable for store the value)= (output[1]-48)*10
This one should remain 0(48-48*10 = 0)
First byte multiplied by 100+ second byte multiplied by 10 + third byte multiplied by 1 = 100+0+0 = 100
Yes
then why when parsing 10 did you multiply the first byte by 10
It didn't was in the 3 digits format, so I directly started by the second one, but the real first should be 0 or null
💀 could it be that my code tries to multiply null*100 and then the value get corrupted?
Yes
when you send 100 the first digit is 1
Yes
where would it ever multiply null*100
When the value get to 10 (it should become 010)
where are you sending the numbers in 3 digit format?
to the main.cpp
This value should set cursor position
no like what line of code sends the numbers to the computer in 3 digit format
the line 65
Anyway I have to go to sleep,
I will hope to keep talking about this later, (8 hours, just the time to sleep),
I don't know how exactly explain the code, but to every question I will try answer you
sure. maybe in the morning you'll see what the problem is
bye
Uhm,hi
I think I should debug what comes in tripl[] arrays, for understand a bit better
answer me this question again
Uhm, oh, actually there isn't really a format, sorry I just said it in the wrong way, I meant it should be in 3 digit format, but I just noticed few moment ago that arduino changes the bytes order, kinda
if the value entering in the pc is 100
my code works
if it goes under 100
kinda 98
the first byte would be 9
second one would be 8
the third one will return be 9
so now I remapped again the value
and now I just should have 3 bytes
and after I remove a line of code "println" that I used for separate the values, now -3000 doesn't appear anymore
the last problem if I remove the sleep function the code runs too fast and the byte order start to get unstable
and the order start swapping
kinda 251 get printed for few seconds, then by the unstability of the code it become 512 for few moments, then goes back to normal
now I'm printing another byte, and I'm adding a function in the main.cpp for recognize the right order of the bytes
the Arduino isnt changing anything. you never specified a 3 digit format but you assumed there was a 3 digit format
how you're doing it now is really inefficient and prone to bugs
any suggestions for improve?
why not just send the raw binary value
don't convert the int to a string, send the string, then convert to int again
just send int
uhm, I have only a question, where do I store that binary value?
as I see here the library just accept char storage
should I change it?
I mean
modify the library
all right, I will try
thanks for the tip
no problem
fu... my wiring to the potentiometer just got damaged 
ok today I got some free time for modify the library
so we said instead of getting char, we get directly binary
sure
how to not read data from serial, for real