Hello everyone!
I would like to creat an Serial port C# application with Mono.
I'm searching an usefull example to writing an platform independent C# based Serial port example using Mono.
Can you help me?
Thanks in advance!
Sandor
Hello everyone!
I would like to creat an Serial port C# application with Mono.
I'm searching an usefull example to writing an platform independent C# based Serial port example using Mono.
Can you help me?
Thanks in advance!
Sandor
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
Jayraj ChhayaRank #304 · 8.2K reputationPosted Feb 5, 2024, 7:57 AM EST
you'll need to install Mono on your system.
Once you have Mono installed, you can use the
System.IO.Portsnamespace in C# to work with Serial ports. This namespace provides classes and methods to communicate with Serial ports in a platform-independent manner.using System; using System.IO.Ports; class Program { static void Main(string[] args) { // Get the available Serial ports string[] ports = SerialPort.GetPortNames(); if (ports.Length > 0) { // Select the first available Serial port string portName = ports[0]; // Create a new SerialPort object SerialPort serialPort = new SerialPort(portName, 9600); try { // Open the Serial port serialPort.Open(); // Write data to the Serial port serialPort.WriteLine("Hello, Serial port!"); // Close the Serial port serialPort.Close(); Console.WriteLine("Data written to Serial port successfully."); } catch (Exception ex) { Console.WriteLine("Error: " + ex.Message); } } else { Console.WriteLine("No Serial ports found."); } } }Remember to handle exceptions appropriately when working with Serial ports, as they can throw exceptions for various reasons (e.g., port not found, permission denied, etc.).
Jithu ThomasRank #437 · 5.7K reputationPosted Feb 5, 2024, 4:18 AM EST
Defenitly,
here is an example: -
using System; using System.IO.Ports; class SerialPortExample { static void Main() { // Get available serial ports string[] portNames = SerialPort.GetPortNames(); if (portNames.Length == 0) { Console.WriteLine("No serial ports available."); return; } // Select the first available serial port string selectedPort = portNames[0]; Console.WriteLine("Available serial ports:"); foreach (var port in portNames) { Console.WriteLine(port); } Console.WriteLine("\nSelected serial port: " + selectedPort); // Create a SerialPort object SerialPort serialPort = new SerialPort(selectedPort, 9600, Parity.None, 8, StopBits.One); try { // Open the serial port serialPort.Open(); Console.WriteLine("Serial port opened."); // Write data to the serial port string dataToSend = "Hello, Serial Port!"; serialPort.WriteLine(dataToSend); Console.WriteLine("Data sent: " + dataToSend); // Read data from the serial port string receivedData = serialPort.ReadLine(); Console.WriteLine("Data received: " + receivedData); } catch (Exception ex) { Console.WriteLine("Error: " + ex.Message); } finally { // Close the serial port if (serialPort.IsOpen) { serialPort.Close(); Console.WriteLine("Serial port closed."); } } } }Dhiraj PoojaryRank #1,039 · 1.9K reputationPosted Feb 7, 2024, 4:46 AM EST
Feel free to explore the following GitHub repositories this can help with that
https://github.com/topics/serialport?l=c%23