简体   繁体   English

C# 到 arduino 通信

[英]C# to arduino communication

Good afternoon everyone,大家下午好,

I'm experimenting with serial communication between c# and arduino.我正在试验 c# 和 arduino 之间的串行通信。 To understand how the comms work with regards to sending numbers (I know, there's alot being discussed already but bear with me) I want to send the value of a trackbar to the arduino, and then translate this number in movement of a servo.要了解通信在发送数字方面是如何工作的(我知道,已经讨论了很多,但请耐心等待)我想将轨迹栏的值发送到 arduino,然后在伺服的运动中转换这个数字。

If I understand correctly, integers can't be sent directly but have to be converted into bytes first.如果我理解正确,则不能直接发送整数,而必须先将其转换为字节。 So for this I would convert the numeric value of the trackbar into a byte array因此,为此我会将轨迹栏的数值转换为字节数组

in C# :在 C# 中:

byte[] Numbers;
Numbers = BitConverter.GetBytes(trackBar1.Value);

Via serial communication I would send the value通过串行通信我会发送值

port.Write(Numbers, 0, 1);

And this is where I'm going wrong I think The trackbar value goes from 0 to 255, so I guess I'd need to know the bytes that equal 0 to 255 to be able to adjust the last number (in my example '1') to get the correct number after translation in Arduino?这就是我出错的地方我认为轨迹栏值从 0 到 255,所以我想我需要知道等于 0 到 255 的字节才能调整最后一个数字(在我的示例中为 1 ') 在 Arduino 中翻译后获得正确的数字?

As for Arduino, I would 'translate' the bytes as follows:至于 Arduino,我会按如下方式“翻译”字节:

int IncomingValue = Serial.parseInt();

And then I'd like to use the IncomingValue for my servo.然后我想将 IncomingValue 用于我的伺服。

My question is what I'm doing incorrectly.我的问题是我做错了什么。

thanks谢谢

For those interested in tinkering with c# to arduino serial communication (just desribing the issue I had) and sending a value of a trackbar to use in arduino:对于那些有兴趣修改 c# 以进行 arduino 串行通信(只是描述我遇到的问题)并发送轨迹栏值以在 arduino 中使用的人:

make yourself a form project and name it whatever you like.让自己成为一个表单项目,并随意命名。 Then create a trackbar.然后创建一个轨迹栏。 I will create a trackbar with a minimum value of 0 and a maximum of 255 as follows:我将创建一个最小值为 0,最大值为 255 的轨迹栏,如下所示:

trackbar1.Minimum = 0;
trackbar1.Maximum = 255;
trackBar1.TickFrequency = 10;   // this will set ticks every 10 units.

Now we can setup the actual communication between c# and arduino.现在我们可以设置 c# 和 arduino 之间的实际通信。 I'll start with c#.我将从 c# 开始。

Opening a port:打开端口:

  port = new SerialPort("COM5", 9600);   //Set your Arduino COM port and baud rate
            port.Open();

Now we also need to create an event to close the port when the program shuts:现在我们还需要创建一个事件来在程序关闭时关闭端口:

 void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (port != null && port.IsOpen)
            {
                port.Close();
            }
        }

Now we have set up the port, but we still need data to send.现在我们已经设置了端口,但是我们仍然需要发送数据。 In our example we told we were going to send the value of the trackbar.在我们的示例中,我们告诉我们要发送轨迹栏的值。 To do this, click on your trackbar and find the trackBar1_Scroll event and double click it.为此,请单击您的轨迹栏并找到 trackBar1_Scroll 事件并双击它。 This basically creates the event of scrolling the trackbar and now we have to describe in this event what we want to happen.这基本上创建了滚动轨迹栏的事件,现在我们必须在这个事件中描述我们想要发生的事情。

We want to send the data of the trackbar via serial.我们想通过串口发送轨迹栏的数据。 The trackbar value is retrieved as follows:轨迹栏值检索如下:

trackbar1.value

Now we know how to retrieve the value, we have to look at the options of sending it, there's 3 options using port.write() , we'll use this later:现在我们知道如何检索值,我们必须查看发送它的选项,使用port.write()有 3 个选项,我们稍后将使用它:

-Write(String) -写(字符串)
-Write(Byte[], Int32, Int32) -写(字节[],Int32,Int32)
-Write(Char[], Int32, Int32) -Write(Char[], Int32, Int32)

but as I'm working with an int, I found that the second one was the one I needed.但是当我使用 int 时,我发现第二个是我需要的。 As you can see we need a Byte array to be able to send this, so we will cast our trackbar data into a byte array by using BitConverter and declaring a byte array called Numbers:如您所见,我们需要一个 Byte 数组才能发送它,因此我们将使用 BitConverter 将我们的轨迹栏数据转换为一个字节数组,并声明一个名为 Numbers 的字节数组:

byte[] Numbers = BitConverter.GetBytes(trackBar1.Value);

Now we have every element we need:现在我们有了我们需要的所有元素:

  • The port which has been opened.已打开的端口。 We also described when to close it.我们还描述了何时关闭它。
  • The trackbar and its data to send.要发送的轨迹栏及其数据。
  • The data of the trackbar 'packaged' the correct way to be able to send it.轨迹栏的数据以正确的方式“打包”,以便能够发送。

the only thing we didn't do is describe the scroll event, but now we have the correct data format, we can:我们唯一没有做的就是描述滚动事件,但是现在我们有了正确的数据格式,我们可以:

private void trackBar1_Scroll(object sender, EventArgs e)
        {
            
            byte[] Numbers = BitConverter.GetBytes(trackBar1.Value);

            port.Write(Numbers, 0, 1);

        }

so in c# our code will look like this:所以在 C# 中,我们的代码将如下所示:

 public Form1()
        {
            InitializeComponent();
            
            trackBar1.Minimum = 0;
            trackBar1.Maximum = 255;
            trackBar1.TickFrequency = 10;

            port = new SerialPort("COM5", 9600);//Set your board COM
            port.Open();

            this.FormClosed += new FormClosedEventHandler(Form1_FormClosed);
           
        }
 
void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (port != null && port.IsOpen)
            {
                port.Close();
            }
        }

private void trackBar1_Scroll(object sender, EventArgs e)
        {
            byte[] Numbers = BitConverter.GetBytes(trackBar1.Value);

            port.Write(Numbers, 0, 1);

        }

As for arduino, all we need to do is:至于arduino,我们需要做的就是:

A] Start our serial A] 开始我们的连续剧

B] Read the incoming data B]读取传入的数据

#include <LiquidCrystal.h>                 // include the LCD library

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);     //define the LCD display

int Number;                               //define the number int (the one we'll store our received trackbar value in)
    
    void setup()
    {
      Serial.begin(9600);           //A] Start our serial
    }
    
    void loop()
    {
      
       lcd.setCursor(0, 0);     // Set the LCD cursor at the top left corner

      if (Serial.available() > 0 ) {         //B] Read the incoming data
        nummer = Serial.read();              //
        lcd.print(nummer);                   //
      }                                      //
      else {};                               //
                      
   }

I used an LCD to read the data as I couldn't figure out a way to open the serial monitor with the serial communication between c# and arduino happening.我使用 LCD 来读取数据,因为我无法找到一种方法来打开串行监视器,并在 c# 和 arduino 之间进行串行通信。 that's why I included an LCD.这就是为什么我包括一个液晶显示器。

I feel explaining this is beyond the scope of this little issue I had.我觉得解释这超出了我遇到的这个小问题的范围。

I hope this helps other beginners like me in the future.我希望这对将来像我一样的其他初学者有所帮助。

thanks to those who were helpful!感谢那些有帮助的人!

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

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