Tests mit SerialPort
This commit is contained in:
parent
45b33fcc3a
commit
897211d6f1
@ -1,4 +1,5 @@
|
||||
@page "/"
|
||||
@using System.IO.Ports
|
||||
|
||||
<PageTitle>Index</PageTitle>
|
||||
|
||||
@ -6,4 +7,47 @@
|
||||
|
||||
Welcome to your new app.
|
||||
|
||||
<SurveyPrompt Title="How is Blazor working for you?" />
|
||||
<label for="portname">Port:</label>
|
||||
<InputText id="portname" @bind-Value="portname"></InputText>
|
||||
<button @onclick="SendData">Send data to Arduino</button>
|
||||
<p>
|
||||
<label>Received: @receivedText</label>
|
||||
</p>
|
||||
|
||||
|
||||
|
||||
@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("<RECIPE>");
|
||||
|
||||
}
|
||||
|
||||
private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
|
||||
{
|
||||
if (port != null)
|
||||
{
|
||||
var text = port.ReadExisting();
|
||||
receivedText = text;
|
||||
port.Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user