没有得到正确的输出
Don't get proper output
我编写了一个用于编码和解码莫尔斯电码的程序,此代码用于解码莫尔斯电码。
我使用 2 个 arduino 板,第一个 Arduino 用于将文本编码为莫尔斯码,第二个用于接收莫尔斯码并解码为 text.Connection 2 个 arduino 板使用红外传感器。
在第一个arduino板上
这个ARDU INO是我代码的输入,然后这样转换.-|.-.|-..|..-| |..|-.|---|,在这个|中是词尾。然后传输这个摩尔斯电码
在第二个 Arduino 板上
它收到 .-|.-.|-..|..-| |..|-.|---|,但它的解码是这样的 "ARDUU INOO ",它在 [=23] 之前打印相同的单词两次=]space
为什么会这样?请帮助我
#define SIZE 26
const int btnPin=7;
String morseCode="";
String text="";
int characterAscii=0;
int startPos=0, endPos=0;
int startPos1=0, endPos1=0;
String characterCode="";
int btnState=0;
unsigned long int duration = 0;
//Array of MorseCode for letters of English Language A to Z
String letters[SIZE]={
// A to I
".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..",
// J to R
".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.",
// S to Z
"...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."
};
void setup() {
pinMode(btnPin, INPUT_PULLUP);
Serial.begin(9600);
//Serial.println("*********************");
Serial.println(" Demonstration of Morse Code ");
Serial.println("********************* ");
//Serial.println("\nInstructions");
Serial.println("1. First Write Your Morse code");
Serial.println("2. When you are done Write 1 on above input box and Press Enter or click Send Button ");
Serial.println("3. For Space between letters write 2 and Press Enter ");
Serial.println("4. For Space between words write 3 and Press Enter ");
Serial.println("5. Thats all Translation of Morse Code will then be Shown ");
Serial.println("\n\nEnter Your Morse Code Here ");
}
void loop() {
while(Serial.available() > 0 ) {
int ascii=Serial.read();
switch(ascii)
{
case 49: // 49 is Ascii value of 1
Serial.print("\n");
morseCode.concat('#');// Placeing # at the end of morseCode to simplify further processing
Serial.print("\nYour Morse code Translation : ");
endPos1=morseCode.indexOf('#');
while(endPos1 < morseCode.length() ){
extractLetters(morseCode.substring(startPos1, endPos1)); // This function would extract Letter as name suggest and would convert code to text SIMPLE!
startPos1=endPos1+1;
if(startPos1 == morseCode.length() ){
break;
}
endPos1= morseCode.indexOf('#', startPos1);
}
startPos1=0;
endPos1=0;
text=""; // For New Translation
morseCode="";
Serial.println("\n\nEnter Your Morse Code Here ");
break;
}
process();
}
void process(){
while(digitalRead(btnPin) == LOW ) {
delay(150); //if you want more resolution, lower this number
duration = duration + 150;
}
switch(duration){
case 450:
morseCode.concat("-"); // Storing code in variable morseCode with the help of concatenation function
Serial.print("-");//Prints User entered Code
//Serial.print(duration);
break;
case 150:
morseCode.concat(".");
Serial.print(".");
//Serial.print(duration);
break;
case 300:
morseCode.concat("@");
Serial.print("|");
//Serial.println(duration);
break;
case 1050:
morseCode.concat("#");
Serial.print(" |");
//Serial.println(duration);
break;
}
duration = 0;
}
char convertIntoText(String characterCode)
{
characterAscii=65;
for(int index=0; index<SIZE; index++)
{
if(characterCode == letters[index])
{
return characterAscii;
}
characterAscii++;
}
}
void extractLetters(String words)
{
words.concat('@'); // Placeing @ at the end of word to simplify further processing
endPos=words.indexOf('@');
//Loop to extracting single character morse Code from string of word
while( endPos<words.length() )
{
characterCode=words.substring(startPos, endPos);
//Now CharacterCode will now convert in text
text.concat(convertIntoText(characterCode));
startPos=endPos+1;
characterCode="";
// if condition is just to terminate loop when our extracting single character code is complete thats all
if(startPos == words.length() )
{
break;
}
endPos=words.indexOf('@', startPos);
}
Serial.print(text);
Serial.print(" ");
startPos=0;
endPos=0;
text="";
}
问题好像出在你的提取逻辑上,没有考虑到#
打断的话,然后因为convertIntoText
没有默认的return路径,当输入字符在预期的 26 个字符中为 NOT 时,草图中使用的函数的响应实际上是该函数调用的先前响应(这是错误状态)
第一条规则,确保函数中的所有逻辑路径 return 一个值,要么注入已知错误值,要么故意引发错误。在这种情况下,非字母字符就足够了:
char convertIntoText(String characterCode)
{
characterAscii=65;
for(int index=0; index<SIZE; index++)
{
if(characterCode == letters[index])
{
return characterAscii;
}
characterAscii++;
}
return '!'; // error code
}
如果您在输出中看到任何 !
个字符,那么您就知道映射输入存在问题...但这无助于调试,您可能应该输出整个 characterCode
值,所以也许试试这个:
return "(" + characterCode + ")";
But you would also need to change the response type of this function to a String
as well.
实际问题,请确保您在 extractLetters
中考虑了 #
,有多种方法可以做到这一点,最容易适应当前逻辑的可能是在开始时循环,只需检查下一个字符:
//Loop to extracting single character morse Code from string of word
while( endPos<words.length() )
{
// check for word break
if(words.substring(startPos,startPos+1) == '#')
{
text.concat(' ');
// advance 1 char
startPos++;
}
characterCode=words.substring(startPos, endPos);
//Now CharacterCode will now convert in text
text.concat(convertIntoText(characterCode));
startPos=endPos+1;
characterCode="";
// if condition is just to terminate loop when our extracting single character code is complete thats all
if(startPos == words.length() )
{
break;
}
endPos=words.indexOf('@', startPos);
}
一些通用的代码反馈:
构建morseCode
时使用@
作为分隔符,但输出到串行流|
。对同一事物有两种含义会使解析代码和向同事解释变得比实际需要的复杂得多。尽量避免使用 internal 表示以及调试或 external 表示。选择 pipe 或 at 并保持一致。
测试这个的时候,一开始不是很明显,但是你对extractLetters
的输入实际上是:
.-@.-.@-..@..-@#..@-.@---@
(其实这更像)在process()
中,为了简化调试,你应该把你正在解释的相同的字符写成并在内部录制时处理到串行输出。
我编写了一个用于编码和解码莫尔斯电码的程序,此代码用于解码莫尔斯电码。
我使用 2 个 arduino 板,第一个 Arduino 用于将文本编码为莫尔斯码,第二个用于接收莫尔斯码并解码为 text.Connection 2 个 arduino 板使用红外传感器。
在第一个arduino板上 这个ARDU INO是我代码的输入,然后这样转换.-|.-.|-..|..-| |..|-.|---|,在这个|中是词尾。然后传输这个摩尔斯电码
在第二个 Arduino 板上 它收到 .-|.-.|-..|..-| |..|-.|---|,但它的解码是这样的 "ARDUU INOO ",它在 [=23] 之前打印相同的单词两次=]space
为什么会这样?请帮助我
#define SIZE 26
const int btnPin=7;
String morseCode="";
String text="";
int characterAscii=0;
int startPos=0, endPos=0;
int startPos1=0, endPos1=0;
String characterCode="";
int btnState=0;
unsigned long int duration = 0;
//Array of MorseCode for letters of English Language A to Z
String letters[SIZE]={
// A to I
".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..",
// J to R
".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.",
// S to Z
"...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."
};
void setup() {
pinMode(btnPin, INPUT_PULLUP);
Serial.begin(9600);
//Serial.println("*********************");
Serial.println(" Demonstration of Morse Code ");
Serial.println("********************* ");
//Serial.println("\nInstructions");
Serial.println("1. First Write Your Morse code");
Serial.println("2. When you are done Write 1 on above input box and Press Enter or click Send Button ");
Serial.println("3. For Space between letters write 2 and Press Enter ");
Serial.println("4. For Space between words write 3 and Press Enter ");
Serial.println("5. Thats all Translation of Morse Code will then be Shown ");
Serial.println("\n\nEnter Your Morse Code Here ");
}
void loop() {
while(Serial.available() > 0 ) {
int ascii=Serial.read();
switch(ascii)
{
case 49: // 49 is Ascii value of 1
Serial.print("\n");
morseCode.concat('#');// Placeing # at the end of morseCode to simplify further processing
Serial.print("\nYour Morse code Translation : ");
endPos1=morseCode.indexOf('#');
while(endPos1 < morseCode.length() ){
extractLetters(morseCode.substring(startPos1, endPos1)); // This function would extract Letter as name suggest and would convert code to text SIMPLE!
startPos1=endPos1+1;
if(startPos1 == morseCode.length() ){
break;
}
endPos1= morseCode.indexOf('#', startPos1);
}
startPos1=0;
endPos1=0;
text=""; // For New Translation
morseCode="";
Serial.println("\n\nEnter Your Morse Code Here ");
break;
}
process();
}
void process(){
while(digitalRead(btnPin) == LOW ) {
delay(150); //if you want more resolution, lower this number
duration = duration + 150;
}
switch(duration){
case 450:
morseCode.concat("-"); // Storing code in variable morseCode with the help of concatenation function
Serial.print("-");//Prints User entered Code
//Serial.print(duration);
break;
case 150:
morseCode.concat(".");
Serial.print(".");
//Serial.print(duration);
break;
case 300:
morseCode.concat("@");
Serial.print("|");
//Serial.println(duration);
break;
case 1050:
morseCode.concat("#");
Serial.print(" |");
//Serial.println(duration);
break;
}
duration = 0;
}
char convertIntoText(String characterCode)
{
characterAscii=65;
for(int index=0; index<SIZE; index++)
{
if(characterCode == letters[index])
{
return characterAscii;
}
characterAscii++;
}
}
void extractLetters(String words)
{
words.concat('@'); // Placeing @ at the end of word to simplify further processing
endPos=words.indexOf('@');
//Loop to extracting single character morse Code from string of word
while( endPos<words.length() )
{
characterCode=words.substring(startPos, endPos);
//Now CharacterCode will now convert in text
text.concat(convertIntoText(characterCode));
startPos=endPos+1;
characterCode="";
// if condition is just to terminate loop when our extracting single character code is complete thats all
if(startPos == words.length() )
{
break;
}
endPos=words.indexOf('@', startPos);
}
Serial.print(text);
Serial.print(" ");
startPos=0;
endPos=0;
text="";
}
问题好像出在你的提取逻辑上,没有考虑到#
打断的话,然后因为convertIntoText
没有默认的return路径,当输入字符在预期的 26 个字符中为 NOT 时,草图中使用的函数的响应实际上是该函数调用的先前响应(这是错误状态)
第一条规则,确保函数中的所有逻辑路径 return 一个值,要么注入已知错误值,要么故意引发错误。在这种情况下,非字母字符就足够了:
char convertIntoText(String characterCode)
{
characterAscii=65;
for(int index=0; index<SIZE; index++)
{
if(characterCode == letters[index])
{
return characterAscii;
}
characterAscii++;
}
return '!'; // error code
}
如果您在输出中看到任何 !
个字符,那么您就知道映射输入存在问题...但这无助于调试,您可能应该输出整个 characterCode
值,所以也许试试这个:
return "(" + characterCode + ")";
But you would also need to change the response type of this function to a
String
as well.
实际问题,请确保您在 extractLetters
中考虑了 #
,有多种方法可以做到这一点,最容易适应当前逻辑的可能是在开始时循环,只需检查下一个字符:
//Loop to extracting single character morse Code from string of word
while( endPos<words.length() )
{
// check for word break
if(words.substring(startPos,startPos+1) == '#')
{
text.concat(' ');
// advance 1 char
startPos++;
}
characterCode=words.substring(startPos, endPos);
//Now CharacterCode will now convert in text
text.concat(convertIntoText(characterCode));
startPos=endPos+1;
characterCode="";
// if condition is just to terminate loop when our extracting single character code is complete thats all
if(startPos == words.length() )
{
break;
}
endPos=words.indexOf('@', startPos);
}
一些通用的代码反馈:
构建
morseCode
时使用@
作为分隔符,但输出到串行流|
。对同一事物有两种含义会使解析代码和向同事解释变得比实际需要的复杂得多。尽量避免使用 internal 表示以及调试或 external 表示。选择 pipe 或 at 并保持一致。测试这个的时候,一开始不是很明显,但是你对
extractLetters
的输入实际上是:.-@.-.@-..@..-@#..@-.@---@
(其实这更像)在
process()
中,为了简化调试,你应该把你正在解释的相同的字符写成并在内部录制时处理到串行输出。