简体   繁体   English

来自 C# Visual Studio 的 Arduino 串行通信

[英]Arduino Serial Communication from C# visual studio

Arduino code Arduino 代码

void setup() {
   Serial.begin(9600);
   pinMode(GREEN_LED, OUTPUT);
   pinMode(RED_LED, OUTPUT);
   pinMode(BLUE_LED, OUTPUT);
   Serial.write("x\n");
}

void loop() {
  if(Serial.available() > 0){
    if(first == true){ Serial.read(); first = false; }
    if(Serial.peek() == 'r'){
      analogWrite(RED_LED, (Serial.readString() + Serial.readString() + Serial.readString()).toInt());
    }
    else if(Serial.peek() == 'g'){
      analogWrite(GREEN_LED, (Serial.readString() + Serial.readString() + Serial.readString()).toInt());
    }
    else if(Serial.peek() == 'b'){
      analogWrite(BLUE_LED, (Serial.readString() + Serial.readString() + Serial.readString()).toInt());
      Serial.write("x\n");
    }
    else{ 
      error(); 
    }
    Serial.println(String(brightnessR) + "," + String(brightnessG) + "," + String(brightnessB) + "\n");
  }
}

While simple there is an issue, probably due to my inability to understand the Serial methods.虽然很简单,但有一个问题,可能是由于我无法理解 Serial 方法。 It begins with sending a test byte down the serial to tell the C# program on the computer that it can begin sending information.它首先向串口发送一个测试字节,告诉计算机上的 C# 程序它可以开始发送信息。 Next it checks if there are any bytes in the serial and if so it checks if the first byte matches any of the either r, g, or b and grabs the next three bytes from the serial and turns it into an integer.接下来,它检查串行中是否有任何字节,如果有,则检查第一个字节是否与 r、g 或 b 中的任何一个匹配,并从串行中获取接下来的三个字节并将其转换为 integer。

On the visual studio side在视觉工作室方面

string getRGB(Color rgb)
        {
            String rValue = "r";
            if (type == 0)
            {
                if(rgb.R < 100)
                {
                    rValue += "0";
                }
                if (rgb.R < 10)
                {
                    rValue += "0";
                }
                type++;
                return rValue  + (rgb.R).ToString() + "\n";
            }
            String gValue = "g";
            if (type == 1)
            {
                if (rgb.G < 100)
                {
                    gValue += "0";
                }
                if (rgb.G < 10)
                {
                    gValue += "0";
                }
                type++;
                return gValue + (rgb.G).ToString() + "\n";
            }
            String bValue = "b";
            if (type == 2)
            {
                if (rgb.B < 100)
                {
                    bValue += "0";
                }
                if (rgb.B < 10)
                {
                    bValue += "0";
                }
                type = 0;
                return bValue + (rgb.B).ToString() + "\n";
            }
            return "";
        }

public void SerializeRGB()
        {
            Color RGB = GetColorFromScreen(new Point(1160, 450));
            if (LineOpen())
            {
                Updater(getRGB(RGB));
            }
            else
            {
                Values.Text = "Error\nError\nError";
            }
        }
        public void Updater(String n)
        {
            Values.Text = n;
            WriteSerial(n);
        }

On visual studio if it detects that the arduino has requested data with the 'x' then it sends a string through the serial comprised of either r000 g000 b000 with the appropriate rgb values of the middle pixel on my screen.在 Visual Studio 上,如果它检测到 arduino 已请求带有“x”的数据,那么它会通过由 r000 g000 b000 组成的串行发送一个字符串,其中包含我屏幕上中间像素的适当 rgb 值。

The idea of this program is to translate screen colors onto a LED RGB strip hooked up to the Arduino.这个程序的想法是将屏幕 colors 转换到连接到 Arduino 的 LED RGB 灯条上。 Currently the problem comes in that the program cannot identify whether there is an r,g or b at the beginning of the string and never gives a true for an if(){} statement and always end up on the else{}.目前的问题在于程序无法识别字符串开头是否存在 r,g 或 b,并且永远不会为 if(){} 语句提供 true 并且总是以 else{} 结束。

If anyone could help me solve this serial communication problem it would be very appreciated:)如果有人能帮我解决这个串行通信问题,将不胜感激:)

Notes: Serial.readString() returns a string, Serial.read() returns the next byte in the serial.注意: Serial.readString() 返回一个字符串,Serial.read() 返回序列中的下一个字节。

I believe 10 is the char for a newline.我相信 10 是换行符的字符。 Try filtering out for a new line/return.尝试过滤出新行/返回。 I would recommend set a string with a value of ( String input = Serial.readString(); ), and using the first char from the string as you would be able to first filter the string of new lines/returns, which should give you the rgb chars.我建议设置一个值为( String input = Serial.readString(); )的字符串,并使用字符串中的第一个字符,因为您可以首先过滤新行/返回的字符串,这应该会给您rgb 字符。

String input = Serial.readString(); //gets string for serial port
input.trim(); //removes all whitespaces(\n\r, etc)
Char type = input.charAt(0); //gets first char

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

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