简体   繁体   中英

Sending int to arduino through C#

So, I am trying to send an int to an arduino through a C# program. I have the C# program and arduino talking to each other just fine. I can send single ASCII bytes just fine and get the arduino to react; 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. 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:

    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. 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. 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 (]). Then get the value with 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.

Sorry, not a pre-cooked solution here.

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

I answered this question here already. You can use "atoi". The code for c# is extremely simple. In my answer/link you find the code for Arduino. Basically this act as a protocoll.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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