简体   繁体   English

通过C#将int发送到arduino

[英]Sending int to arduino through C#

So, I am trying to send an int to an arduino through a C# program. 因此,我试图通过C#程序将int发送到arduino。 I have the C# program and arduino talking to each other just fine. 我有C#程序和arduino互相交谈就很好。 I can send single ASCII bytes just fine and get the arduino to react; 我可以很好地发送单个ASCII字节,并让arduino做出反应; however, when I try to send it a larger value - say 35 - I can figure out how to get the arduino to take that information from the serial port, convert it to an int so I can later use that value for some mathematical functions. 但是,当我尝试发送一个较大的值(例如35)时,我可以弄清楚如何获取arduino来从串行端口获取该信息,并将其转换为int,以便以后可以将该值用于一些数学函数。 Any tips/code? 有提示/代码吗? Thanks stack guys! 谢谢堆叠的家伙! :) :)

Kind of a pseudo code here, since you state that the comm to the micro is good. 这是一种伪代码,因为您声明与微指令的通信是好的。 On the arduino: 在arduino上:

    char buffer[5]; // or 6 or 7 or... To store numerical chars

You need a way to let the arduino know that a number is coming down the line. 您需要一种让arduino知道某个数字即将降临的方法。 Assume your number is packed in a "packet" like this: 假设您的电话号码打包在一个“数据包”中,如下所示:

    [I123]

The brackets indicate start and end of packet, the I indicates an integer is the data payload. 方括号表示数据包的开始和结束,I表示整数,即数据有效载荷。 So in the Serial.Read() function you have to test for start of packet ([), then test for next character which indicates an integer (I), then the remaining characters should be saved in buffer up to but not including (]). 因此,在Serial.Read()函数中,您必须测试数据包的开始([),然后测试指示整数(I)的下一个字符,然后其余字符应保存在缓冲区中,最多但不包括(] )。 Then get the value with value = atoi(buffer). 然后使用value = atoi(buffer)获得该值。

/*
 Simple code to read in an integer value sent over the Serial port.
 */

char ch;         // incoming serial character

char chStartPacket = '[';
char chEndpacket =']';
char integerDataType = 'I';
char buf[10];
int index = 0;
int intValue = 0;

void setup()
{
  // start serial port at 9600 bps:
  Serial.begin(9600);

}

void loop()
{
  // assume packet of format [Xddd], where X is packet data type, ddd is data payload
  // this is code which compiles, but not tested. Neither is any check done on the payload data 
  // to ensure good data

  if(Serial.available() > 0){

    if(Serial.peek() == chStartPacket ){
      index = 0;                                // reset index of next integer in buffer
      Serial.read();                            // get rid of start of packet in serial port buffer
    }  
    else if (Serial.peek()  == integerDataType){
      Serial.read();                           // get rid of data type character in serial port buffer
    }
    else if (Serial.peek() == chEndpacket){    // all data received, get integer value
      Serial.read();                           // get rid of end of packet in serial port buffer
      intValue = atoi(buf);
      Serial.println(intValue);                // for visual feedback
    }
    else{                                      //must be payload data, add to buffer
      buf[index] = Serial.read();
      index++;
      buf[index] = '\0';
    }

  }
}

It depends a little on the desired protocol. 它在某种程度上取决于所需的协议。 Do you want a compact data format or do you rather want a human readable/writable format? 您是要使用紧凑的数据格式,还是要使用人类可读/可写的格式?

If you go for the compact data structure you should look into the concept of serialization . 如果您选择紧凑的数据结构,则应考虑序列化的概念。 If you prefer the easy to read and write by hand format, you will have to implement some sort of protocol your self. 如果您希望使用易于读写的格式,则必须自行实现某种协议。 To begin with I think I will suggest the later. 首先,我认为我会建议稍后。

Start with something that can pack up a stream of chars into a string and then feed that to the atoi function. 从可以将字符流打包成字符串的东西开始,然后将其提供给atoi函数。

Sorry, not a pre-cooked solution here. 抱歉,这里不是预先准备好的解决方案。

您可能想要使用CmdMessenger库,或者想要使用此处概述在我的博客中的示例中实现的有限状态机。

I answered this question here already. 我已经在这里回答了这个问题。 You can use "atoi". 您可以使用“ atoi”。 The code for c# is extremely simple. C#的代码非常简单。 In my answer/link you find the code for Arduino. 在我的答案/链接中,您找到了Arduino的代码。 Basically this act as a protocoll. 基本上,这充当协议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM