229Views8Replies
Arduino Code?
I have a simple problem that I could not solve for days.
I am trying to write a code that takes an input from Serial Communication port such as:
"00aaaaaaa12dddddddd23vvvvvvvvvvvv"
and divides into one string matrix and one array such as
String[] = {"aaaaaaa","dddddddd","vvvvvvvvvvvv"}
int Array[] = {0,12,23} //2-digit int numbers
Thank you for you help
Comments
8 years ago
This is my code but it is not working?? :((
int number,h,i,flag,m = 0;
int temp[20];
int array[20];
char msg[100];
char* strings[20];
String tempstring[30];
void setup()
{
Serial.begin(9600);
}
void loop()
{
while(Serial.available()==0){} // komut yoksa bekle
while(Serial.available() > 0)
{
msg[i]= Serial.read(); // inputu oku
i++;
}
if(flag=0){ // bu kodu sadece bir kere calıstır
for(int j=0;j<100;j++){ // msg stringdeki butun sayıları bir temp arrayıne e kaydet
if(msg[j]<=57 && msg[j]>=48)
{
temp[h]=msg[j]-48;
h++;
}
}
for(int g=0;g<100;g++) // temp arrayindeki 2-digit sayıları int e cevirip array e kaydet
{
int k=0;
array[g]=10*temp[k]+temp[k+1];
k=k+2;
}
for(int u=0;u<100;u++) // msg stringdeki yazıları string arraye kaydet
{
if((msg[u]<=90 && msg[u]>=65)||(msg[u]<=122 && msg[u]>=97)) // yazı bulursan tempstring e kaydet
{
msg[u]=tempstring[m]; // bura kesinlikle error veriyor
m++;
}
for(int h=1;h<20;h=h+2) // sayıya rastlama durumu
{
if(msg[u]==temp[h]) // eger temp arrayinden indexi tek sayı olan herhangi bir rakama rastlarsan tempstringi string e kaydet
{
strings[y]=tempstring;
y++;
}
}
}
flag=1;}
Answer 8 years ago
Look for a distinct string terminating character, then parse the string.
Start at the beginning ! The first two characters are always in position 1 and 2, the first string in 3 to 10 etc etc.
Answer 8 years ago
really helps if the string is a fixed length, but not required - you can just assume to count rather than search for a character...
Answer 8 years ago
He's showing a fixed format string in the question, but you're right - better to count the commas, assuming he's using comma-delimited.
8 years ago
What you are looking for is string Parsing. There are a LOT of articles on the net about this...
First things first, When serial data comes in, it comes one character at a time, and if you're processing it quickly, you take it one character at a time and put each character into a working string. When data stops coming in we can then process it. It also helps to have a Start and Stop byte at the beginning and/or end. Something like "*00aaaaaaa*12dddddddd*23vvvvvvvvvvvv|" is a lot easier to parse, explained below:
The trick is each 'packet' with indexed values needs something unique about it.
If you just look for 2 digits in a row, it works, but its a lot more ifs and or select cases. If you start and end each packet with a unique character, you can just say if character 0 is '*' start processing...convert the next 2 digits into a number,then convert the digits after that into a string, until you hit another '*'.
In this case, a 'while' loop would be great to say 'while the working character is not an asterisk, keep copying the working character to the destination string, and moving on to the next working character
Remember that characters in a string are just bytes...with values from 0-255, You can probably find someone did the work to make 'char to numerical value' conversion based on the ascii chart...
http://arduino.cc/en/Reference/ASCIIchart
if (string[0] == 48) then numeral = 0; //'0'
You only need to do it for the numerals and the start/stop characters. You can use a for loop:
for (i = 48, i<58, i++){ //numerals are 48-57 = 0-9
if (string[x] == i) then numeral = x - 48;
}
Having the | pipe at the end (these could be any characters you simply dont use in the strings 'ever'), tells your code to stop looping, you've reached the end.
Answer 8 years ago
My favourite Delphi library Async Pro tools, has a packet handling function, where you can nominate any characters as start and end of packet. If you're using ASCII comms, I always like to use human readable strings which I terminate in CR/LF. Then I use LF as the start of a packet, and CR as the END of a packet.
Steve
Answer 8 years ago
I like the crlf idea - especially where a human is sending the instructions.
Answer 8 years ago
In mine, they aren't sending the instructions....but boy does it make it easy to examine the logs.....