简体   繁体   English

使用C#读取arduino发送到串行端口的信息

[英]Reading Info sent by arduino to Serial Port with C#

I am trying to read simple sensor reading from a arduino. 我正在尝试从arduino读取简单的传感器读数。 The arduino is connected to COM3 (used for sending data and programming the arduino). arduino连接到COM3(用于发送数据和对arduino进行编程)。 The C# Programm is very simple and tries to read what the arduino sends. C#程序非常简单,它尝试读取arduino发送的内容。 Problem: I can not open COM3 Port with C# or the arduino when the other side (C# or arduino respectively) already opened it. 问题:当另一端(分别为C#或arduino)已经打开COM3端口时,我无法用C#或arduino打开它。 Just sending without opening doesnt produce any results aswell. 只是发送而没有打开不会产生任何结果。 How are you supposed to "connect" them? 您应该如何“连接”它们? My understanding was that both devices open the port with the same baudrate and then you can send and read data. 我的理解是,两个设备都以相同的波特率打开端口,然后您可以发送和读取数据。 When I am trying to open, I will get a UnauthorizedAccess on the C# side or a "can't open serial" on the arduino side. 当我尝试打开时,我将在C#端获得一个UnauthorizedAccess或在arduino端获得一个“无法打开序列”。

Arduino C-Code: Arduino C代码:

#include <DHT.h>

#define DHTPIN A4
#define DHTTYPE DHT11
#define THERPIN A0

DHT dht(DHTPIN,DHTTYPE);   
String hum="Humidity:";
String temptext="Temp:";
String semi=";";

void setup() {  
    Serial.begin(9600);
    dht.begin();
    pinMode(A0,INPUT);
}    
void loop() {   
    float humidity = dht.readHumidity();
    delay(300);
    float temp = dht.readTemperature();
    delay(300);


    if (isnan(humidity)||isnan(temp))
    {
      Serial.println("Fehler beim Lesen(NAN)");
      delay (5000);
   }else
    {
      Serial.print(temp + semi);
      Serial.print(humidity);
      Serial.flush();
      delay(1000);  
    }
}

C# Code C#代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.IO.Ports;

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {    
            SerialPort serialPort1;
            serialPort1 = new SerialPort();
            serialPort1.PortName = "COM3";
            serialPort1.BaudRate = 9600;
            serialPort1.DtrEnable = true;
            REPEAT:
            if (serialPort1.IsOpen)
            {
                string reading = serialPort1.ReadLine();
                Console.WriteLine(reading);
                serialPort1.Close();                   
            }
            else
            {
                Console.WriteLine("closed,opening");
                serialPort1.Open();
                goto REPEAT;    
            }    
        }
    }
}

While searching the solution was always that another programm was already using the COM Port, but isn't that exactly what I need to communicate? 在搜索解决方案时,总是有另一个程序已经在使用COM端口,但这不是我需要传达的信息吗? Obviously, the arduino has to use the same COM-Port as my C# app, as far as I understand. 显然,据我所知,arduino必须使用与C#应用程序相同的COM端口。

Thanks 谢谢

Your code is opening and closing the serial port perpetually. 您的代码将永久打开和关闭串行端口。 This does not work, because when .NET code closes the connection Windows internally will close the port asynchronously. 这是行不通的,因为当.NET代码关闭连接时,Windows内部将异步关闭端口。 It can take a few seconds before the port is actually closed. 实际关闭端口可能需要几秒钟。 This is why the program almost immediately blocks. 这就是程序几乎立即被阻止的原因。

Open the connection only once at the start of your program. 在程序开始时仅打开一次连接。

Besides: avoid GOTO statements at any cost. 此外:不惜一切代价避免GOTO语句。 Edgar Dijkstra wrote a paper against its use many years ago: Go To Statement Considered Harmful . 埃德加·迪克斯特拉(Edgar Dijkstra)多年前写了一篇反对其使用的论文: 认为有害的陈述

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

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