Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
ok im trying to build a string converter that would for example take the input of "FF 01 00 20 00 00 21" and send it out as "A0 02 05 59 FF". this is for a camera protocol converter i am building based in arduino. i have been looking at the different functions of strings and i am stumped. all i want it to do is this:
string[] stringArray = { "FF 01 00 20 00 00 21"};
string value = "A0 02 05 59 FF";
int pos = Array.IndexOf(stringArray, value);
if (pos >- 1)

any suggestions as to what i am doing wrong?
Posted
Comments
Richard C Bishop 15-Aug-13 18:29pm    
Why use an array if you only have one item in it?

Also, the logic of that code is not correct. "value" is not in your array so "pos" will always be 0.
Member 10198769 15-Aug-13 18:34pm    
trying to get this part down before i add the other 5 sets into it. this is like my 20th attempt at it and each time it failed to verify in the arduino ide.
Richard C Bishop 15-Aug-13 18:38pm    
Let me ask you this, does "FF" always equal "A0" in your conversion essentially?
Member 10198769 15-Aug-13 18:46pm    
yes, although im looking to do a comparative type like say "A0 12" = "FF 01" but "A0 01" = "FF 02" .

does that make any sense?
Richard C Bishop 15-Aug-13 18:49pm    
It makes sense, but that sort of functionality is going to be very tedious and involve a ton more than the lines you provided. You will need dictionaries with fixed values that correlate with the value you need to convert it to. You will have to account for all possible conversions.

Hello there,
I think what you should do is to explode your string into an array of characters, but with reference with something for example A0 and if you have more than one reference head, simple use switch case:
see the following example
C#
string[] str = incoming_string.Split(' ');
switch (str[0])
{
   case "A0":
   //do something
   break;
   case "F0":
   //do something else
   break;
}


an so on

Good Luck,
z3ngew
 
Share this answer
 
ok this is what i have come up with thus far and it goes through the verify sequencing without any errors

C#

#include <SoftwareSerial.h>
// software serial #1: TX = digital pin 10, RX = digital pin 11
SoftwareSerial Serial1(10,11);

// software serial #2: TX = digital pin 8, RX = digital pin 9
SoftwareSerial Serial2(8,9);

String stringOne, stringTwo, stringThree, stringFour;

void setup() {
  // Open serial communications and wait for port to open:
  Serial1.begin(19200);
  Serial2.begin(19200);
  while (!Serial) {
    ; 
  }


  stringOne = String("FF 01 00 20 00 00 21");
  stringTwo = String("FF 01 00 40 00 00 41");
  stringThree = String("FF 01 00 80 00 00 81");
  stringFour = String("FF 01 01 00 00 00 02");
  // send an intro:
  Serial.println("");
  Serial.println();

}

void loop() {
  // two strings equal:
  if (stringOne == "") {
    Serial2.println("StringOne == \"A0 02 05 59 FF\""); 
  }
  // two strings not equal:
  if (stringOne != stringTwo) {
    Serial1.println(stringOne + " =! " + stringTwo);
  }
 // two strings equal:
  if (stringTwo == "") {
    Serial2.println("StringTwo == \"A0 02 07 57 FF\""); 
  }
  // two strings not equal:
  if (stringTwo != stringThree) {
    Serial1.println(stringTwo + " =! " + stringThree);
  }
 // two strings equal:
  if (stringThree == "") {
    Serial2.println("StringThree == \"A0 03 06 57 FF\""); 
  }
  // two strings not equal:
  if (stringThree != stringFour) {
    Serial1.println(stringThree + " =! " + stringFour);
  }
 // two strings equal:
  if (stringFour == "") {
    Serial2.println("StringFour == \"A0 03 05 58 FF\""); 
  }
  // two strings not equal:
  if (stringFour != stringOne) {
    Serial1.println(stringFour + " =! " + stringOne);
  }


}


What do you think?
 
Share this answer
 
Comments
idenizeni 15-Aug-13 23:47pm    
Me thinks this should not be tagged as C#.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900