Serial Port - read data

Hi,

I'm new at Radzen.

In the designer, I would like to have a textbox showing a scale weighing info.

For that, I need to read the data from a serial port and put it in the textbox.

Best would be to get the information, as soon as the scale sends new data or second best to push a button and read the data upon request.

Any good and simple ideas?

Thank you,

Thomas

Hi @THW,

I am afraid the Radzen.Blazor components do not include capabilities for reading data from the serial port. You can try with the built-in .NET SerialPort class.

I found this short C# to be working. But how can I asign it to a button in Radzen and display the result in a textbox?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Console.WriteLine("Serial read init");
SerialPort port = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One);
port.Open();
while (true)
{
Console.WriteLine(port.ReadLine());
}

    }



    
}

}

Add an event handler for the Click event of the button and set the result to a variable. Then use the variable for the textbox value. You can check the online demos for the button and textbox to get some ideas.

To use serial communications, I obviously need to include the "using System.IO.Ports" somewhere.

When I press the button, it has to execute C#:

Console.WriteLine("Serial read init");
SerialPort port = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One);
port.Open();
while (true)
{
Console.WriteLine(port.ReadLine());
}

I have found the place to enter this code, but I can't find an info about where to put the "using System.IO.Ports".

assuming you have created a project reference the code you posted above shows how.
if the code is in a razor page you can use @using System.IO.Ports

I would recommend checking the official Microsoft documentation about Blazor. Your questions aren't related to the Radzen.Blazor component library.