简体   繁体   English

如何从PC接收数据到Arduino?

[英]How can I receive data from a PC to an Arduino?

I developed an application that sends data for an Arduino by the serial port, but I can't understand how I can receive it on the Arduino. 我开发了一个通过串口发送Arduino数据的应用程序,但我无法理解如何在Arduino上接收它。 I send a string by the serial port for the Arduino and the Arduino receives it, but it does not work in my code (on the Arduino, I receive a byte at a time). 我通过Arduino的串口发送一个字符串,Arduino接收它,但它在我的代码中不起作用(在Arduino上,我一次收到一个字节)。

Update: it's working ;) 更新:它正在运行;)

The code in C# that sends data: C#中发送数据的代码:

using System;
using System.Windows.Forms;

using System.Threading;
using System.IO;
using System.IO.Ports;

pulic class senddata() {

    private void Form1_Load(object sender, System.EventArgs e)
    {
        //Define a serial port.
        serialPort1.PortName = textBox2.Text;
        serialPort1.BaudRate = 9600;
        serialPort1.Open();
    }

    private void button1_Click(object sender, System.EventArgs e)
    {
        serialPort1.Write("10");  //This is a string. The 1 is a command. 0 is interpeter.
    }
}

The Arduino code: Arduino代码:

I have Update the Code 我有更新代码

#include <Servo.h>

Servo servo;
String incomingString;
int pos;

void setup()
{
    servo.attach(9);
    Serial.begin(9600);
    incomingString = "";
}

void loop()
{
    if(Serial.available())
    {
        // Read a byte from the serial buffer.
        char incomingByte = (char)Serial.read();
        incomingString += incomingByte;

        // Checks for null termination of the string.
        if (incomingByte == '0') { //When 0 execute the code, the last byte is 0.
            if (incomingString == "10") { //The string is 1 and the last byte 0... because incomingString += incomingByte.
                servo.write(90);
            }
            incomingString = "";
        }
    }
}

Some things which make my eyebrow raise: 一些令我眉毛的东西:

serialPort1.Write("1");

This will write exactly one byte, the 1 , but no newline and no trailing NUL-Byte. 恰好写一个字节,在1 ,但没有换行,并没有尾随NUL字节。 But here you are waiting for an additional NUL byte: 但是你在这里等待一个额外的NUL字节:

if (incomingByte == '\0') {

You should use WriteLine instead of Write , and wait for \\n instead of \\0 . 您应该使用WriteLine而不是Write ,并等待\\n而不是\\0

This has two side effects: 这有两个副作用:

First: If there is some buffering configured, then there is a certain chance, than a new line will push the buffered data to the Arduino. 第一: 如果配置了一些缓冲,那么有一定几率,新线将把缓冲的数据推送到Arduino。 To be certain you have to dig through the docs at MSDN. 确定你必须深入研究MSDN上的文档。

Second: This makes your protocol ASCII-only. 第二:这使您的协议仅限ASCII。 This is important for easier debugging. 这对于简化调试很重要。 You can then use a plain terminal program like Hyperterm or HTerm ( edit ) or even the Serial Monitor in the Arduino IDE itself ( edit ) to debug your Arduino-Code without worrying about bugs in your C# code. 然后,您可以使用Hyperterm或HTerm( 编辑 )等普通终端程序 ,甚至Arduino IDE中的串行监视器( 编辑 )来调试Arduino代码,而无需担心C#代码中的错误。 And when the Arduino code works you can concentrate on the C# part. 当Arduino代码工作时,您可以专注于C#部分。 Divide et impera. 划分et impera。

Edit: Another thing I noticed after digging out my own Arduino: 编辑:在挖掘出我自己的Arduino之后我注意到的另一件事:

incomingString += incomingByte;
....
if (incomingByte == '\n') { // modified this
  if(incomingString == "1"){

This will of course not work as expected, because the string will contain "1\\n" at this point. 这当然不会按预期工作,因为此时字符串将包含“1 \\ n”。 Either you compare to "1\\n" or move the += line after the if . 您可以比较“1 \\ n”或移动if后面的+=行。

You could alternatively try using the Firmata library - it's a much better way of having standard firmware on the Arduino and managing it from .net 您也可以尝试使用Firmata库 - 这是在Arduino上使用标准固件并从.net管理它的更好方法

I believe, Firmata 2.0+ has support for I2C and servo control. 我相信,Firmata 2.0+支持I2C和伺服控制。

http://firmata.org/ http://firmata.org/

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

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