简体   繁体   English

C#将串行命令发送到Arduino

[英]C# Sending Serial Commands to Arduino

I am using an Arduino Mega 2560 board to control a strip of LED's. 我正在使用Arduino Mega 2560开发板来控制LED灯条。 I am trying to use a short C# console program to send serial commands to my Arduino program. 我正在尝试使用一个简短的C#控制台程序向我的Arduino程序发送串行命令。 In Arduino, I have a library called SerialCommand where I can read a serial string and use it to execute a function and also pass along arguments like so: "functionName arg1 arg2 arg3". 在Arduino中,我有一个名为SerialCommand的库,可以在其中读取串行字符串并使用它执行函数,还可以传递类似这样的参数:“ functionName arg1 arg2 arg3”。 I've tested this by sending serial commands through a terminal and it works great! 我已经通过终端发送串行命令进行了测试,效果很好! I've gotten Arduino functions to run by sending serial commands from my C# program also but only if that serial string doesn't include any spaces. 我也通过从C#程序发送串行命令来运行Arduino函数,但前提是该串行字符串不包含任何空格。

Here is my C# code: 这是我的C#代码:

class Program
{
    public static System.IO.Ports.SerialPort serialPort1;

    private void establishConnection()
    {
        serialPort1 = new System.IO.Ports.SerialPort("COM4");
        serialPort1.BaudRate = 115200;

        serialPort1.Open();
    }

    static void Main(string[] args)
    {
        Program p = new Program();
        p.establishConnection();

        string i;
        while (true)
        {
            Console.Write("Enter command:  ");
            i = Console.ReadLine();
            if (i == "exit")
            {
                serialPort1.Close();
                break;
            }
            else if (i == "1")
            {
                // Turn LED's on and pass argument "test"
                serialPort1.Write("ON test\n");
            }
        }
    }
}

I know my connection works and my commands are sending because if I program my ON function in Arduino to work without the argument, it works great. 我知道我的连接有效并且我的命令正在发送,因为如果我在Arduino中对ON函数进行编程以使其不带参数就可以正常工作。 However if I send the "ON" command and include the argument after a space, or use spaces in general, the Arduino doesn't read it properly. 但是,如果我发送“ ON”命令并在空格后加上参数,或者通常使用空格,则Arduino无法正确读取它。

Do you have any idea of why the space is messing with my serial commands in C#? 您是否知道为什么空间会干扰我在C#中的串行命令? Thanks for any help. 谢谢你的帮助。

Here is a simple sample of what I'm doing in Arduino: 这是我在Arduino中所做的一个简单示例:

#include "SerialCommand.h"
#include <Adafruit_NeoPixel.h>

SerialCommand sCmd;

#define PIN7 7
// Strip of LED's
Adafruit_NeoPixel testStrip = Adafruit_NeoPixel(10, PIN7, NEO_GRB + NEO_KHZ800);
uint32_t testStripColor = 0xFFFFFF;

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

  sCmd.addCommand("ON",  lightsOn);
  sCmd.addCommand("OFF",  lightsOff);

  testStrip.begin();
  testStrip.show();
}

void loop() {
  sCmd.readSerial();
}

void lightsOn() {
  char *lightSet;
  lightSet = sCmd.next();  // Read argument

  if (strcmp(lightSet, "test") == 0) {
    for (int i=0; i < testStrip.numPixels(); i++) 
    {
      testStrip.setPixelColor(i, testStripColor);
    }
    testStrip.show();
  }
}

void lightsOff() {
  char *lightSet;
  lightSet = sCmd.next();  // Read argument

  if (strcmp(lightSet, "test") == 0) {
    testStrip.clear();  
    testStrip.show();
  }
}

请试试:

serialPort1.WriteLine("ON test");

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

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