Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# c与arduino之间的串行数据通信_C#_Arduino_Serial Port - Fatal编程技术网

C# c与arduino之间的串行数据通信

C# c与arduino之间的串行数据通信,c#,arduino,serial-port,C#,Arduino,Serial Port,这是我的问题。我使用这段代码通过串行端口向我的arduino发送字符串: Arduino.Open(); Arduino.WriteLine("1.5,3.7,2"); 之后,我用我的arduino收到它,如下所示: char buffer[12]; String inc; void setup() { Serial.begin(115200); } void loop() { if (Serial.available() >= 11) { for (int i=0

这是我的问题。我使用这段代码通过串行端口向我的arduino发送字符串:

Arduino.Open();
Arduino.WriteLine("1.5,3.7,2");
之后,我用我的arduino收到它,如下所示:

char buffer[12];
String inc;

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

void loop()
{
 if (Serial.available() >= 11) 
 {
    for (int i=0; i<11   ; i++) 
    {
      buffer[i] = Serial.read();
      inc += buffer[i];
    }
    memset(buffer, 0, sizeof(buffer));
    Serial.println(inc);
    inc = "";
  }
}
波特率相同,奇偶校验设置为无,默认设置为停止位。
我希望你能帮助我,我对这件事非常着迷,我就是找不到我的错误或格式错误的原因。

这与UNO兼容的Arduino 1.6完全兼容。请注意,这段代码根本没有错误处理,所以只作为起点使用

C样品

using System;
using System.IO.Ports;

namespace SerialPortTest
{
    class Program
    {
        static void Main(string[] args)
        {
            var p = new SerialPort(args[0], 9600, Parity.None, 8, StopBits.One);
            p.Open();

            Console.WriteLine("Sending ({1}) {0}", args[1], args[1].Length);

            p.WriteLine(args[1]);

            Console.WriteLine("Reading back");

            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine(p.ReadLine());

            Console.ResetColor();
        }
    }
}
阿杜伊诺代码

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

char buffer[64];
char *p = buffer;

void loop() 
{
    char ch = ' ';
    while (Serial.available() > 0)
    {
       ch = Serial.read();
       *p++ = ch;    

       if (ch == '\n')
       {
         *p = 0;
         Serial.print("got ");
         Serial.println(buffer);      
         p = buffer;
         break;
      }
   }  
}

您的Arduino代码与数据传输不同步。您发送10个字节,而不是11个字节。您还可以发送行反馈,但随后使用println添加两个额外的字节。正确的Arduino代码只是将字符追加到缓冲区,直到您得到换行符“\n”。很抱歉,这不是问题所在。我修复了您指出的问题,但是问号仍然是四个????在绳子前面。是否只有打开Serialport后的第一个字符串才有某种标记?我不知道怎么解决这个问题。谢谢你的帮助。它解决了这个问题,但不是因为代码中有错误。只有波特率设置为115200时,才会出现问号。如果我使用波特率为9600的oof,它也能与我的代码完美配合。我不知道为什么会这样!正如我刚刚发现的,它似乎只能在9600波特或更低的波特率下工作……我不明白。你试过我的样品了吗?我的价格是115200,没有问题。我试过你的样品。事情是这样的:每当我第一次通过串行监视器写一个结尾有换行符的句子时,结果是????然后是句子。如果我现在输入一个新的句子????你走了。但当我断开arduino的电源并重新连接时,同样的事情再次发生。所有arduinos、megas和unos都是如此。您知道如何处理此问题吗?您确定发送的字符数不超过64个吗?
void setup() 
{
    Serial.begin(9600);
}

char buffer[64];
char *p = buffer;

void loop() 
{
    char ch = ' ';
    while (Serial.available() > 0)
    {
       ch = Serial.read();
       *p++ = ch;    

       if (ch == '\n')
       {
         *p = 0;
         Serial.print("got ");
         Serial.println(buffer);      
         p = buffer;
         break;
      }
   }  
}