From 897211d6f15cd772c136a2a2a3681bd6f2e68473 Mon Sep 17 00:00:00 2001 From: BuildTools Date: Mon, 15 Jan 2024 21:11:22 +0100 Subject: [PATCH] Tests mit SerialPort --- CocktailWeb/Pages/Index.razor | 46 ++++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/CocktailWeb/Pages/Index.razor b/CocktailWeb/Pages/Index.razor index 6085c4a..cf453b0 100644 --- a/CocktailWeb/Pages/Index.razor +++ b/CocktailWeb/Pages/Index.razor @@ -1,4 +1,5 @@ @page "/" +@using System.IO.Ports Index @@ -6,4 +7,47 @@ Welcome to your new app. - + + + +

+ +

+ + + +@code { + SerialPort? port; + string? portname = "COM5"; + string? receivedText; + + public async Task SendData() + { + if (port == null && portname != null) + { + port = new SerialPort(portname); + port.BaudRate = 9600; + port.DataReceived += port_DataReceived; + port.DataBits = 8; + port.StopBits = StopBits.One; + port.Parity = Parity.None; + port.Handshake = Handshake.None; + port.RtsEnable = true; + + } + if (port == null || port.IsOpen) return; // Port konnte nicht initialisiert werden oder ist bereits geöffnet - dann nicht nochmal öffnen + port.Open(); + port.WriteLine(""); + + } + + private void port_DataReceived(object sender, SerialDataReceivedEventArgs e) + { + if (port != null) + { + var text = port.ReadExisting(); + receivedText = text; + port.Close(); + } + } +}