删除功能不适用于串行通信后多个循环中的多个实例

Remove function doesn't work with multiple instances in multiple loops after serial communication

本文关键字:循环 实例 通信 不适用 功能 适用于 删除      更新时间:2023-10-16

>我创建了一个简单的回显程序来接收数据,对其进行修改,然后让它回显修改后的原始数据。当我运行我的代码时,我没有得到我期望的。我创建了一个窗口表单来与Ardino UNO进行通信。我会将字符串发送到("0001","0002","0003","0004"}.

数据将由Arduino读取,然后运行它以删除任何前导0,并且返回的数据应{1","2","3","4"}修改后的数据,("0001","0002","0003","0004"}原始数据。当我运行我的程序时,前几次我发送数据时,我会得到奇怪的数据回显。有时它会移动或以我不期望的顺序移动,但经过几次运行后,它通常会回响我所期望的。

唯一的问题是它没有完全完成删除方法。相反,得到回响的是{"1","0002","0003","0004"}"0001","0002","0003","0004"}。回声的第二部分是预期的,但似乎它只删除了第一个实例的前导零,而没有其他时间。由于我没有在 while 循环中使用共享索引变量,并且总是让它检查第一个元素,因此我认为我的 while 循环没有任何问题。我知道你可以在一个程序中有多个while循环,所以我知道这不会成为问题。

我不明白为什么删除函数只在第一个循环中运行,而在其他循环中没有运行。我是否太超前了,因为显示预期返回数据大约需要两次运行?当我不使用循环而只是练习在字符串上使用 remove 方法时,它工作正常。即使数据没有正确回显,它仍然应该被修改吗?我应该期望得到两组不同的字符串,无论通信是否被错误传输。

Arduino代码读取数据,将其存储在数组中,将数组的内容复制到字符串中以删除前导0,然后回显数据。

// Visual Micro is in vMicro>General>Tutorial Mode
// 
// Define Functions below here or use other .ino or cpp files
//
String *rarray = new String[4]; 
String *carray = new String[4]; 
int blinks;
// The setup() function runs once each time the micro-controller starts
void setup()
{
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(9600);
Serial.println("Arduino is Ready");
}
// Add the main program code into the continuous loop() function
void loop()
{
getstring();
StringtoInt0();
StringtoInt1();
StringtoInt2();
StringtoInt3();
blink();
echo();
delay(1000);
}
void getstring()
{
int ndx = 0;
String rs;
while (Serial.available() > 0)
{
rs = Serial.readStringUntil('');
rarray[ndx] = rs;
carray[ndx] = rarray[ndx];
ndx++;
if (ndx >= 4)
break;
}
}
void StringtoInt0()
{
String R0B0 = carray[0];//get string from element 0
while (R0B0[0] == '0')//while first char is 0
{
R0B0.remove(0, 1);//remove it
}
Serial.println(R0B0);//return the modified data back to the GUI
int int0 = R0B0.toInt();//convert a string into an integer
delay(10);//allow time for the data to be transmitted.
//--------------------------------
}
void StringtoInt1()
{
String R0B = carray[1];
while (R0B[0] == '0')
{
R0B.remove(0, 1);
}
Serial.println(R0B);
blinks = R0B.toInt();
delay(10);
//--------------------------------
}
void StringtoInt2()
{
String R0B2 = carray[2];
while (R0B2[0] == '0')
{
R0B2.remove(0, 1);
}
Serial.println(R0B2);
int int2 = R0B2.toInt();
delay(10);
//--------------------------------
}
void StringtoInt3()
{
String R0B3 = carray[3];
while (R0B3[0] == '0')
{
R0B3.remove(0, 1);
}
Serial.println(R0B3);
int int3 = R0B3.toInt();
delay(10);
}
//--------------------------------
void blink()
{
for (int b = 1; b <= blinks; b++)
{
digitalWrite(LED_BUILTIN, HIGH);
delay(250);
digitalWrite(LED_BUILTIN, LOW);
delay(250);
}
}
void echo()
{
for (int rs = 0; rs <= 3; rs++)
{
Serial.println(carray[rs]);//return the original sent data
delay(20);
}
}

传输数据的 Windows 窗体:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;
using System.Threading;
using System.Collections;
namespace Blink_with_String_GUI
{
public partial class Form1 : Form
{
SerialPort port = new SerialPort();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string S0; string S1; string S2; string S3;
int S0I; int S1I; int S2I; int S3I;
S0I = Convert.ToInt16(b0.Value);
S1I = Convert.ToInt16(b1.Value);
S2I = Convert.ToInt16(b2.Value);
S3I = Convert.ToInt16(b3.Value);
S0 = S0I.ToString("D4");
S1 = S1I.ToString("D4");
S2 = S2I.ToString("D4");
S3 = S3I.ToString("D4");
label1.Text = S0;
label2.Text = S1;
label3.Text = S2;
label4.Text = S3;
string[] string1 = new string[] { S0, S1, S2, S3 };
//-----
port = new SerialPort("COM3", 9600, Parity.None, 8, StopBits.One);
port.Open();
for (int s = 0; s <= 3; s++)    //Write string of padded int
{
port.WriteLine(string1[s]);
Thread.Sleep(10);
}
string rs1; string rs2; string rs3; string rs4; string rs5; string rs6; string rs7; string rs8; string rs9;
rs1 = port.ReadLine();
rs2 = port.ReadLine();
rs3 = port.ReadLine();
rs4 = port.ReadLine();
rs5 = port.ReadLine();
rs6 = port.ReadLine();
rs7 = port.ReadLine();
rs8 = port.ReadLine();
rs9 = port.ReadLine();

label8.Text = rs1;
label7.Text = rs2;
label6.Text = rs3;
label5.Text = rs4;
label12.Text = rs5;
label11.Text = rs6;
label10.Text = rs7;
label9.Text = rs8;


port.Close();

字符串数组中的空字符不会被写入。因此,您不能尝试阅读所述字符。而是等待读取'n'工作,因为 writeLine 打印一个新行字符。谢谢Gre_Gor