简体   繁体   English

从 arduino 获取串行输出的奇怪行为

[英]Strange behavior on getting serial outputs from arduino

I made an Arduino script for printing key on button press :我制作了一个 Arduino 脚本,用于在按下按钮时打印按键:

#include <Keypad.h>

//Buttons
const byte ROWS = 2;
const byte COLS = 2;
char keys[ROWS][COLS] = {
    {'1','2'},
    {'3','4'}
};
byte rowPins[ROWS] = {A0, A1};
byte colPins[COLS] = {30, 31};
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

void setup() {
    Serial.begin(9600);
    keypad.addEventListener(keypadEvent);
}

void loop() {
    char key = keypad.getKey();
}

void keypadEvent(KeypadEvent key){
    switch (keypad.getState()){
        case PRESSED:
            Serial.println(key);
            break;
        default:
            break;
    }
}

Then, I made a C# code for printing it in debug console (for testing before going further) :然后,我制作了一个 C# 代码,用于在调试控制台中打印它(在进一步操作之前进行测试):

using System;
using System.Diagnostics;
using System.IO.Ports;

namespace ConsoleApplication1
{
    class Program
    {
        public static void Main()
        {
            SerialPort SP = new SerialPort("COM3");

            SP.BaudRate = 9600;
            SP.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);

            SP.Open();
            Console.WriteLine("Press any key to continue...");
            Console.WriteLine();
            Console.ReadKey();
            SP.Close();
        }

        private static void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
        {
            SerialPort SP = (SerialPort)sender;
            string msg = SP.ReadExisting();
            Debug.Print($"Data Received: {msg}");
        }
    }
}

When using Arduino's serial monitor I get the good behavior :使用 Arduino 的串行监视器时,我得到了良好的行为:

在此处输入图片说明

But when using my console app, I sometime get empty lines :但是当使用我的控制台应用程序时,我有时会得到空行:

在此处输入图片说明

Any ideas on how to improve it ?关于如何改进它的任何想法?

Jasc24 got me on the way. Jasc24 让我上路了。 SerialPort.ReadExisting() reads a stream that can be unfinished and the SerialDataReceivedEventHandler can fire multiple times for a single line. SerialPort.ReadExisting() 读取一个可能未完成的流,并且 SerialDataReceivedEventHandler 可以为一行触发多次。

My workaround was the following :我的解决方法如下:

class Program
{
    static string line = string.Empty;

    public static void Main()
    {
        SerialPort SP = new SerialPort("COM3");

        SP.BaudRate = 9600;
        SP.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);

        SP.Open();
        Console.WriteLine("Press any key to continue...");
        Console.WriteLine();
        Console.ReadKey();
        SP.Close();
    }

    private static void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
    {
        SerialPort SP = (SerialPort)sender;
        line = $"{line}{SP.ReadExisting()}";
        if (line.Contains(Environment.NewLine))
        {
            line = line.Replace(Environment.NewLine, string.Empty);
            Debug.Print($"Data Received: {line}");
            line = string.Empty;
        }            
    }
}

Waiting for the EOL characters to be sure that I got the all line before printing it.等待 EOL 字符以确保我在打印之前得到了所有行。

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

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