In this code i am taking the TID data(20bytes of 160bits) in the form of array according documentation and and its working correctly and getting the output of RFID tags.
Now I just need your guidance that how can i convert the array of 20 Bytes into a single variable or into a double long datatype? or another. Because I have to push it on to my MySQL database server for further processing.
I had tried the following code which works fine
byte x[10] = "450210921";
long result = atol(x);
Serial.print("long value of the byte is: ");
Serial.println(result);
but when i put the above code into my main code, getting not any error and I am not getting the required output.!
void setup()
{
Serial.begin(115200);
}
void loop()
{
byte response;
byte myTID[20]; //TIDs are 20 bytes
byte tidLength = sizeof(myTID);
//Read unique ID of tag
response = nano.readTID(myTID, tidLength);
if (response == RESPONSE_SUCCESS)
{
for(byte x = 0 ; x < tidLength ; x++)
{
if(myTID[x] < 0x10) Serial.print("0");
/*temp[x]=(myTID[x], HEX);*/
//Swapping values into temp variable
temp[x]=myTID[x];
Serial.print(temp[x]);
}
/*byte x[10] = "450210921";*/
//Convert a whole array into a single long variable
long result = atol(temp);
Serial.print("long value of the byte is: ");
Serial.println(result);
}
else
Serial.println("Failed read");
}
-
There is no such thing as a single variable that is 160 bits in size. I think you want to format the bytes as a string (and there are plenty of questions about that on here).Majenko– Majenko2018年11月04日 13:01:39 +00:00Commented Nov 4, 2018 at 13:01
-
Ok @Majenko but is there any way to push complete an array to MySQL into a single table cell using nodemcu?Eza– Eza2018年11月04日 13:09:50 +00:00Commented Nov 4, 2018 at 13:09
-
I have no idea about the MySQL side of things, but you can take each byte in the array, turn it into a text representation of a hexadecimal pair, concatenate them all in a string, and use that string.Majenko– Majenko2018年11月04日 13:11:50 +00:00Commented Nov 4, 2018 at 13:11
-
arduino.stackexchange.com/questions/53258/…Majenko– Majenko2018年11月04日 13:12:09 +00:00Commented Nov 4, 2018 at 13:12
-
There are several simple errors: 1) "atol(x)" should be "atol(tmp)", 2) but then "tmp" should be null-terminated, 3) which then implies that it is too short. And so on.Mikael Patel– Mikael Patel2018年11月04日 13:16:07 +00:00Commented Nov 4, 2018 at 13:16
1 Answer 1
You can't convert 20 bytes to long. Send it as string of hex values. A byte takes two characters in hex, so you will send a 40 characters string, which can be simply decoded back to bytes.
char str[sizeof(myTID) * 2 + 1];
const char* hex = "0123456789ABCDEF";
for (int i = 0; i < sizeof(myTID); i++) {
str[i * 2] = hex[(myTID[i] >> 4)];
str[i * 2 + 1] = hex[(myTID[i] & 0x0F)];
}
str[sizeof(str)] = 0;
Serial.println(str);
-
It works! Thank, what about for this for(byte x = 0 ; x < sizeof(myTID) ; x++) { content.concat(String(myTID[x] < 0x10 ? " 0" : " ")); content.concat(String(myTID[x], HEX)); } content.toUpperCase(); softSerial.print(content);Eza– Eza2018年11月05日 20:12:16 +00:00Commented Nov 5, 2018 at 20:12
-
-
Could you explain it that how the string is not good!!Eza– Eza2018年11月06日 05:48:31 +00:00Commented Nov 6, 2018 at 5:48
-
hackingmajenkoblog.wordpress.com/2016/02/04/…2018年11月06日 05:49:42 +00:00Commented Nov 6, 2018 at 5:49