当前位置: 首页 > 知识库问答 >
问题:

c#通过串行端口向arduino发送int数组

楚嘉玉
2023-03-14

我正试图使用串口从C#向Arduino发送一个int数组。在C#中,首先是输入字符串

input = "98;1;5;160;0;255;421;101";

然后,我将其转换为int数组

int [] sendint = input.Split(';').Select(n => Convert.ToInt32(n)).ToArray(); 
//this array is what I need to send to Arduino

然后,我将其转换为字节数组,通过串行端口发送

byte[] sendbytes = sendint.Select(x => (byte)x).ToArray();
// because Write() requires byte, not int

最后,我把它寄出去了

serialport.Write(sendbytes,0,sendbytes.Length); 
// port is open, baudrate is OK, portname is OK

那么,应该由我的Arduino接收

int recdata[10];
int bytes = 0;
if(Serial.available())
{     
  while(Serial.available())
  {
    recdata[bytes]=Serial.read();
    bytes++;
  }
  checkdata(); //function which checks the received data
}

所以recdata应该是一个int数组

recdata = {98,1,5,160,0,255,421,101};

但事实并非如此。当我将其打印到另一个串行端口进行检查时。。

for(int i = 0; i < 10; i++) //called befory checkdata() function in code above
{
  Serial1.print(recdata[i] + "  ");
}

我得到3个输出,而不是1个,就好像serialport先发送一个int,然后发送第二个int,然后发送其余的int。

98  0  0  0  0  0  0  0  0  0  1checkfail //1checkfail is from function checkdata()
1  0  0  0  0  0  0  0  0  0  1checkfail //and it's saying, that data
5  160  0  255  165  101  0  0  0  0  1checkfail//are wrong

98 1    5  160   0   255 421 101 0  0 1checkok //this is how it should like
                      //421 and 165 - i know, that i'm trying to save 421 in byte, which has a limit of 256, so this is another problem

有人对这个问题有什么建议吗?

共有2个答案

王庆
2023-03-14

Arduino循环正在追赶串行流,因此循环被输入三次而不是一次。

你可以用串口。readBytes()而不是串行。read()。它会一直填满你的缓冲区,直到超时。使用序列号。setTimeout()选择一个合理的超时,而不是默认的1000毫秒。

char recdata[10];
int bytes = 0;
if(Serial.available())
{     
  bytes = Serial.readBytes(recdata, MAX_LENGTH);
  checkdata(); //function which checks the received data
}

关于将整数转换为字节的问题,请看这个问题。

尹昀
2023-03-14

我们应该看看你的checkdata()函数做了什么,但我确信你没有检查bytes变量。

发生的是......您正在使用SERIAL通信。这意味着你不会一次得到所有的数据,而是一次得到一个字节。如果您从PC发送8个字节,但在收到两个字节后检查Serial.available()函数,您将只得到2个作为答案。我认为如果你这样修改你的代码

// Move these OUTSIDE the loop function
int recdata[10];
int bytes = 0;

// This is in the loop function
if(Serial.available())
{     
  while(Serial.available())
  {
    recdata[bytes]=Serial.read();
    bytes++;
  }
  if (bytes >= 8)
  {
    checkdata(); //function which checks the received data
    bytes = 0;
  }
}

它会正常工作。。。

否则编写checkdata函数,我们拭目以待。

顺便说一下...我会在阅读部分加张支票,比如

while(Serial.available())
{
  if (bytes < 10)
  {
    recdata[bytes]=Serial.read();
    bytes++;
  }
}

因此,如果你收到的是12字节而不是10字节,你就可以避免内存损坏。。。

 类似资料:
  • 我试图让Arduino在串口读取字符“s”时触发一个中继。该字符“s”由python根据从屏幕读取的图像发送。 我的问题是arduino似乎无法从串行端口读取数据,因为它从不执行if条件。我的猜测是这两者之间存在某种死锁(这就是为什么我将ardu.close()放在函数foo中) 这是我的arduino代码: 这是我的python代码:

  • 我一直在试图找到一种方法,从我的PC(Windows 7)向Arduino Uno R3发送串行命令。我一直在做这个简单的程序,你应该通过USB电缆发送一个简单的“1”,以便打开车载LED。我的Arduino代码应该可以工作(我知道的,但我会确保上传它)。我一直在尝试使用Python和pySerial发送此命令,但我似乎无法让pySerial正常工作。我也尝试过使用CMD,但当我输入命令(ECHO

  • 我试图做一些非常简单的东西,按钮计数器,有一个按钮在我的HTML-php当我点击它我发送数据'按'到我的arduino和它使led闪烁,到目前为止,还不错,但然后我想发送信号,当我在arduino中按下按钮,我想让我的php读取字符串"点击次数: N"的串行端口,N是从arduio的推送计数。 这里的问题是,在arduino串行我可以看到他字符串,一切正常,但在PHP我做

  • 我将像素数组转换为字符串,并将此字符串发送到arduino.但是我认为此字符串没有正确转换,因为Serial.write发送(8位或8字符)我不知道.并且还想发送100个字符的字符串到串行.请发送您的建议,并获得帮助我解决这个问题。对于任何错误,请提前道歉。 并告诉我如何停止字符串后100个字符不使用(\n或\r)

  • 我有一个BotBoarduino(一个带有一些额外功能的Duemilanove),我试图使用Perl脚本与之通信。如果我从Arduino IDE打开串行监控器,我可以毫无问题地发送和接收数据。在此之后,我的Perl脚本可以毫无问题地进行通信。但是,如果Arduino断开连接,然后重新连接到PC,则Arduino似乎不会监听从我的Perl脚本发送的命令,直到再次打开Serial Monitor。 我

  • 我对通过python发送串行数据有问题。让我这样解释。我在arduino上有一个马达代码,它从串口接收数据,并在接收到的数据中移动。然而,在本例中,每当我发送8个列表中的数据时,它都不会读取第8个列表。通过玩弄时间。在python代码中睡眠我可以看到一些差异(我可以看到发送了4个列表,然后我将time.sleep增加到8,现在它发送了7个列表)我知道玩弄时间。但睡眠并不是解决办法。我还尝试清空输入