54 lines
1.3 KiB
Plaintext
54 lines
1.3 KiB
Plaintext
@page "/"
|
|
@using System.IO.Ports
|
|
|
|
<PageTitle>Index</PageTitle>
|
|
|
|
<h1>Hello, world!</h1>
|
|
|
|
Welcome to your new app.
|
|
|
|
<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();
|
|
}
|
|
}
|
|
}
|