Seriellen Part auf separate Seite gepackt
This commit is contained in:
parent
16a63c6a8c
commit
c475af3f6d
@ -1,5 +1,4 @@
|
|||||||
@page "/"
|
@page "/"
|
||||||
@using System.IO.Ports
|
|
||||||
|
|
||||||
<PageTitle>Index</PageTitle>
|
<PageTitle>Index</PageTitle>
|
||||||
|
|
||||||
@ -7,47 +6,7 @@
|
|||||||
|
|
||||||
Welcome to your new app.
|
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 {
|
@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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
71
CocktailWeb/Pages/SerialTest.razor
Normal file
71
CocktailWeb/Pages/SerialTest.razor
Normal file
@ -0,0 +1,71 @@
|
|||||||
|
@page "/serialtest"
|
||||||
|
@using System.IO.Ports
|
||||||
|
|
||||||
|
<PageTitle>Serial Test</PageTitle>
|
||||||
|
|
||||||
|
<label for="portname">Port:</label>
|
||||||
|
<InputText id="portname" @bind-Value="portname"></InputText>
|
||||||
|
<label for="inputdata">Data:</label>
|
||||||
|
<InputText id="inputdata" @bind-Value="inputdata"></InputText>
|
||||||
|
<button @onclick="SendData">Send</button>
|
||||||
|
<p>
|
||||||
|
<label>Received: @receivedText</label>
|
||||||
|
</p>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@code {
|
||||||
|
SerialPort? port;
|
||||||
|
string? portname = "COM5";
|
||||||
|
string? receivedText;
|
||||||
|
string? inputdata ="LED";
|
||||||
|
|
||||||
|
bool status = false;
|
||||||
|
|
||||||
|
bool Sending = false;
|
||||||
|
bool Receiving = false;
|
||||||
|
|
||||||
|
public async Task SendData()
|
||||||
|
{
|
||||||
|
if (Sending) return; // nichts senden, wenn bereits am Senden
|
||||||
|
Sending = true;
|
||||||
|
if (port == null && portname != null)
|
||||||
|
{
|
||||||
|
port = new SerialPort(portname);
|
||||||
|
port.BaudRate = 115200; //9600
|
||||||
|
port.DataReceived += port_DataReceived;
|
||||||
|
//port.DataBits = 8;
|
||||||
|
//port.StopBits = StopBits.One;
|
||||||
|
//port.Parity = Parity.None;
|
||||||
|
//port.Handshake = Handshake.None;
|
||||||
|
//port.RtsEnable = true;
|
||||||
|
port.Open();
|
||||||
|
}
|
||||||
|
if (port == null || !port.IsOpen) return; // Port konnte nicht initialisiert werden oder ist bereits geöffnet - dann nicht nochmal öffnen
|
||||||
|
// port.Write($"<REZEPT:1-40,3-20,5-30,7-30,1-40,3-20,5-30,7-30,1-40,3-20,5-30,7-30:REZEPT>");
|
||||||
|
port.Write($"<{inputdata}> ");
|
||||||
|
status = !status;
|
||||||
|
Sending = false;
|
||||||
|
// inputdata = null;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
|
||||||
|
{
|
||||||
|
if (Receiving) return;
|
||||||
|
Receiving = true;
|
||||||
|
SerialPort port = (SerialPort)sender;
|
||||||
|
if (port != null)
|
||||||
|
{
|
||||||
|
var text = port.ReadExisting();
|
||||||
|
Console.WriteLine(text);
|
||||||
|
receivedText = text;
|
||||||
|
InvokeAsync(StateHasChanged);
|
||||||
|
|
||||||
|
// port.Close();
|
||||||
|
}
|
||||||
|
Receiving = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -15,6 +15,7 @@
|
|||||||
<div class="dropdown-menu dropdown-menu-end" aria-labelledby="dropdownMenuLink">
|
<div class="dropdown-menu dropdown-menu-end" aria-labelledby="dropdownMenuLink">
|
||||||
<a class="dropdown-item" href="/flaschen">Zutaten</a>
|
<a class="dropdown-item" href="/flaschen">Zutaten</a>
|
||||||
<a class="dropdown-item" href="/cocktails">Cocktails</a>
|
<a class="dropdown-item" href="/cocktails">Cocktails</a>
|
||||||
|
<a class="dropdown-item" href="/serialtest">Serial Test</a>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user