serielle schnittstelle auslesenin c sharp
Serial port readout in C#
serielle schnittstelle auslesenin c sharp
Serial port readout in C#
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Rajanikant HawaldarRank #110 · 20.2K reputationPosted Oct 19, 2023, 1:06 AM EDT
Hi go through this article
https://www.c-sharpcorner.com/UploadFile/eclipsed4utoo/communicating-with-serial-port-in-C-Sharp/
Amit MohantyRank #112 · 20K reputationPosted Oct 19, 2023, 4:23 AM EDT
For more details check the below links:
https://www.codeproject.com/Articles/678025/Serial-Comms-in-Csharp-for-Beginners
https://stackoverflow.com/questions/1243070/how-to-read-and-write-from-the-serial-port
https://codesamplez.com/programming/serial-port-communication-c-sharp
Naimish MakwanaRank #324 · 7.7K reputationPosted Dec 31, 2023, 12:11 AM EST
To read data from a serial port in C#, you can use the
System.IO.Portsnamespace, which provides theSerialPortclass. Here's a simple example of how you can read data from a serial port in C#:using System; using System.IO.Ports; class SerialPortReadout { static void Main() { // Create a new SerialPort object SerialPort serialPort = new SerialPort(); // Set the serial port settings (you need to adjust these based on your device) serialPort.PortName = "COM1"; serialPort.BaudRate = 9600; serialPort.Parity = Parity.None; serialPort.DataBits = 8; serialPort.StopBits = StopBits.One; try { // Open the serial port serialPort.Open(); // Subscribe to the DataReceived event to handle incoming data serialPort.DataReceived += SerialPort_DataReceived; Console.WriteLine("Press any key to exit."); Console.ReadKey(); } catch (Exception ex) { Console.WriteLine($"Error: {ex.Message}"); } finally { // Close the serial port when done if (serialPort.IsOpen) { serialPort.Close(); } } } private static void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e) { // This method will be called when data is received on the serial port SerialPort serialPort = (SerialPort)sender; // Read the available bytes from the serial port int bytesToRead = serialPort.BytesToRead; byte[] buffer = new byte[bytesToRead]; serialPort.Read(buffer, 0, bytesToRead); // Convert the byte array to a string (you may need to adjust the encoding) string receivedData = System.Text.Encoding.ASCII.GetString(buffer); // Display the received data Console.WriteLine($"Received data: {receivedData}"); } }Make sure to adjust the
PortName,BaudRate,Parity,DataBits, andStopBitsproperties based on the settings of your serial device.Thanks
Anandu G NathRank #681 · 3.5K reputationPosted Dec 30, 2023, 8:33 AM EST
@Dieter Kaiser
check below link
SerialPort.Read Method (System.IO.Ports) | Microsoft Learn
Reading serial port in C# - CodeProject