试图找出如何构建字符串转换器 [英] trying to figure out how to build a string converter

查看:63
本文介绍了试图找出如何构建字符串转换器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的我试着建立一个字符串转换器,例如输入FF 01 00 20 00 00 21并将其作为A0 02 05 59 FF发送出去。这是我在arduino建立的相机协议转换器。我一直在看字符串的不同功能,我很难过。我想要它做的就是:

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?

推荐答案

你好,

我认为你应该做的是将你的字符串分解为一个字符数组,但引用一些例如 A0 ,如果你有多个参考头,简单使用开关盒:

见下面的例子

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
string[] str = incoming_string.Split(' ');
switch (str[0])
{
   case "A0":
   //do something
   break;
   case "F0":
   //do something else
   break;
}





等等



祝你好运,

z3ngew



an so on

Good Luck,
z3ngew


好的这是我到目前为止所提出的,它经过验证排序没有任何错误



ok this is what i have come up with thus far and it goes through the verify sequencing without any errors


#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?


这篇关于试图找出如何构建字符串转换器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆