简体   繁体   English

C#Arduino串行通信(2组不同的数据)

[英]C# Arduino serial communication (2 different sets of data)

I have been reading about image processing data using C#. 我一直在阅读有关使用C#进行图像处理的信息。 I need to send to Arduino object coordinates. 我需要发送到Arduino对象坐标。 I sent data about x coordinate with the code I wrote below, but I still could not send y coordinates, because I don't know how Arduino will separate x and y coordinates. 我使用下面编写的代码发送了有关x坐标的数据,但是我仍然无法发送y坐标,因为我不知道Arduino如何分隔x和y坐标。 Is there a method to send data from 2 different channels? 有没有一种方法可以从2个不同的通道发送数据?

if (serialok == true) {
  int second =0;
  int offset=300;
  second = offset - Math.Abs(objectX);
  map =(float) 0.85 * second;
  buffer[0] = (byte)Math.Abs((int)map);
  serialPort1.Write(buffer, 0, 1);

This is how I read the above code from Arduino. 这就是我从Arduino阅读上述代码的方式。

if(Serial.available()>0) {
  inbyte=Serial.read();
}
servo1.write(map(inbyte,0,255,0,180));
delay(15);

Sorry for my English. 对不起我的英语不好。

Solution by OP. 由OP解决。

combines the data as a string in c# , send that sting value serial port 1 and parse on arduino . 在c#中将数据合并为字符串,发送字符串值1并在arduino上解析。

#include <Servo.h>

Servo servo1;
Servo servo2;
const byte numChars = 32;
char receivedChars[numChars];
char tempChars[numChars];        // temporary array for use when parsing

     // variables to hold the parsed data
char messageFromPC[numChars] = {0};
int integerFromPC = 0;
float floatFromPC = 0.0;

boolean newData = false;

//============

void setup() {
   Serial.begin(9600);

   servo1.attach(2);
   servo2.attach(4);
}

//============

void loop() {
   recvWithStartEndMarkers();
   if (newData == true) {
       strcpy(tempChars, receivedChars);
           // this temporary copy is necessary to protect the original data
           //   because strtok() used in parseData() replaces the commas with \0
       parseData();
       showParsedData();
       newData = false;
   }
}

//============

void recvWithStartEndMarkers() {
   static boolean recvInProgress = false;
   static byte ndx = 0;
   char startMarker = '<';
   char endMarker = '>';
   char rc;

   while (Serial.available() > 0 && newData == false) {
       rc = Serial.read();

       if (recvInProgress == true) {
           if (rc != endMarker) {
               receivedChars[ndx] = rc;
               ndx++;
               if (ndx >= numChars) {
                   ndx = numChars - 1;
               }
           }
           else {
               receivedChars[ndx] = '\0'; // terminate the string
               recvInProgress = false;
               ndx = 0;
               newData = true;
           }
       }

       else if (rc == startMarker) {
           recvInProgress = true;
       }
   }
}

//============

void parseData() {      // split the data into its parts

   char * strtokIndx; // this is used by strtok() as an index

   strtokIndx = strtok(tempChars,",");      // get the first part - the string
   strcpy(messageFromPC, strtokIndx); // copy it to messageFromPC

   strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
   integerFromPC = atoi(strtokIndx);     // convert this part to an integer
servo1.write(map(integerFromPC, 0, 255, 0, 180));


   strtokIndx = strtok(NULL, ",");
   floatFromPC = atoi(strtokIndx);     // convert this part to a float
servo2.write(map(floatFromPC, 0, 255, 0, 180));
}

//============

void showParsedData() {

}

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

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