Hello,
I receive int values from serial connection which are the bytes of a number. Lets say i send 123, i ll receive 495051. What i want is to convert this 495051 back to 123. Not print 123 to serial, just convert them in the program so i can do calculations. I searched everywhere including the forum and all that i have found for this topic is how to turn int 53 to char "53" and how to correctly print it to the serial monitor. But nothing of these help me. I tried itoa but it just turns the bytes to char type. If i print sendbuf the characters are still 495051.
Here is a part of my code that shows how i receive the bytes (may have forgotten something because its part of a large code):
#include <SoftwareSerial.h>
static const int RXPin1 = 2, TXPin1 = 3, RXPin2 = 7, TXPin2 = 8, RPin1 = 4, RPin2 = 6;
static const uint32_t GPSBaud = 9600;
int i = 0, j = 0;
int varbuf[32];
char sendbuf[32];
bool received = false;
SoftwareSerial s2(RXPin2, TXPin2);
void setup()
{
s2.begin(GPSBaud);
}
void loop()
{
Serial.println("Checking for new coordinates...");
delay(1000);
static bool reading = false;
while (s2.available() > 0 && received == false) {
varbuf[i] = s2.read();
//itoa(varbuf[i],sendbuf,10);
if (varbuf[i] == 60) {
reading = true;
}
if (reading) {
if (varbuf[i] != 60 && varbuf[i] != 62) {
Serial.print(varbuf[i]);
}
if (varbuf[i] == 62) {
received = true;
reading = false;
break;
}
i++;
}
}
if (received) {
Serial.println();
received = false;
i = 0;
}
else {
Serial.println("No message");
}
}
sirick:
The program doesn't see it that way. How i make 57 to '9'. I can't do calculations on 57 neither on a char.
char c = '9';
int i = c - '0';
if (i == 9)
{
//....
}
This code is plainly nonsense...
if (varbuf[i] != 60 && varbuf[i] != 62) {
varbuf[i] cannot possibly be both 60 ('<') and 62 ('>') at the same time.
Perhaps you meant OR (||) and not AND (&&)?
Also why not use the character literals '<' and '>' instead of ASCII codes. It will save you the effort of lookng them up, and save us reaching for the ASCII table to decipher your code.
sirick:
I receive int values from serial connection which are the bytes of a number. Lets say i send 123, i ll receive 495051. What i want is to convert this 495051 back to 123.
1. The following Table of Fig-1 shows the ASCII codes (in hex base) for the printable characters of the English Language (a - z, A - Z, 0 - 9, special characters like $ and others, punctuation marks like ! and others).
2. Save the received ASCII codes in the following array. In fact, you are receiving 0x31, 0x32, and 0x33 and not 495051 which are the images/values that can be seen on the OutputBox of Serial Moniotr in response to print() command.
char myData[4]; //it will hold 0x31, 0x32, 0x33, and null character (00x00)
3. Now, extract 1 from the content of myData[0]; extract 2 from the content of myData[1]; extract 3 from the content of myData[2].
varbuf cannot possibly be both 60 ('<') and 62 ('>') at the same time. Perhaps you meant OR (||) and not AND (&&)?[/quote] I think you overlooked the '!='. This is like: * *if (not (varbuf[i] == 60 or varbuf[i] == 62) ) {* *
GolamMostafa: 1. The following Table of Fig-1 shows the ASCII codes (in hex base) for the printable characters of the English Language (a - z, A - Z, 0 - 9, special characters like $ and others, punctuation marks like ! and others).
2. Save the received ASCII codes in the following array. In fact, you are receiving 0x31, 0x32, and 0x33 and not 495051 which are the images/values that can be seen on the OutputBox of Serial Moniotr in response to print() command.
char myData[4]; //it will hold 0x31, 0x32, 0x33, and null character (00x00)
3. Now, extract 1 from the content of myData[0]; extract 2 from the content of myData[1]; extract 3 from the content of myData[2].
What is the maximum int an arduino uno can receive? If i send 12345, it prints correctly but if go above that (123456), atoi won't work. It will print a random number. So i suspect that the buffer overloads?
sirick:
What is the maximum int an arduino uno can receive? If i send 12345, it prints correctly but if go above that (123456), atoi won't work. It will print a random number.