Hi Folks.
I need a way to read a MAC address from the serial port. The device upon power up spits out the address on the UART. The address looks like the following:
E5 41 A0 C2 F0 4E.
I am using the following code but I believe I am not storing it correctly.
long incomingByte; // for incoming serial data
void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
}
void loop() {
// send data only when you receive data:
if (Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.read();
// say what you got:
Serial.print("MAC Address: ");
Serial.println(incomingByte, HEX);
}
}
This gives me an output of:
MAC Address: F0
MAC Address: 0
MAC Address: 45
MAC Address: 35
MAC Address: 34
MAC Address: 31
MAC Address: 41
MAC Address: 30
MAC Address: 43
MAC Address: 32
MAC Address: 46
MAC Address: 30
MAC Address: 34
MAC Address: 45
What is the correct method to casture this long data??
Thanks,
Mark.
As you've found out from your codes output, Serial.read() returns 1 byte, and if your mac address looks like this "E5 41 A0 C2 F0 4E", then that is 6 bytes after you've stripped out the spaces (which themselves take a byte.
Store each incomingByte into a char array, and write the null terminator when you receive a \r or \n delimiter.
The data viewed in terminal is actually:
UUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUU
BLE XXXXXXX 2
SVN revision: 997
MAC Address: E541A0C2F04E
How exactly can I store this data in an array and then access the bits I need??
I have tried using the following code but all I get is a load of 255's
byte incomingBytes[1000]; // for incoming serial data
long incomingByte=0;
long incomingDataCounter=0;
void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
}
void loop() {
while (Serial.available()) {} // Wait 'till there are 9 Bytes waiting
for(int n=0; n<100; n++)
{
incomingBytes[n] = Serial.read(); // Then: Get them.
Serial.println(incomingBytes[n]);
}
//Serial.println(incomingBytes);
}
Where am I going wrong????
while (Serial.available()) {} // Wait 'till there are 9 Bytes waiting
Where did you get that comment from? I don't recommend doing this, you enter the loop when theres bytes in the serial buffer, and never exit it.
Check out Robins tutorial on Serial:
Chapter 8
That will give you an idea on how to get a buffer containing a line from the serial, then you just need to parse it, something along these lines:
char buffer[64]; // we'll assume this contains your Serial line
char *found_p = strstr( buffer, "MAC Address: " );
if( found_p )
{
found_p += strlen( "MAC Address: " );
// found_p now points to the start of your mac address ...
}
Thanks for the swift response and help. Also forgive me as I am quiet new to parsing and programming in general. I get that it points to the beginning of the address but I cant seem to get the address. I tried adding:
Serial.println(found_p);
but that does nothing.
I also tried just to print the buffer by doing the following:
void loop() {
char inChar = Serial.read();
Serial.println(inChar);
}
All I get is a load of these characters: ÿÿÿÿ
I'm sure this is quiet simple and something basic I am missing..
You need to read my entire post and not just the part where I provide code.
I directed you to Robins tutorial on how to do just what you want with Serial. Then once you have read and put that in to practice, you'll be at a point where you want to parse your newly created string for your keyword and store the mac address, that is where the code I provided should come in handy.
I'm sure this is quiet simple and something basic I am missing..
Yes it is relatively basic, though it won't feel like it until you've got it down. Read through that tutorial.
i have managed to read the device address using the code from the tutorial:
void loop() {
if(Serial.available() > 0)
{
char inChar = Serial.read();
Serial.print(inChar);
}
}
This gives me the following output which is the device address:
E541A0C2F04E
All is good but why does it ignore the preceding text:
UUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUU
BLE XXXXXXX 2
SVN revision: 997
MAC Address:
Is it to do with the spaces??