Windows 11 install com0com to emulate serialport, disable integrity checks, disable secure boot to run bcdedit /set nointegritychecks on
1.Download from
https://sourceforge.net/projects/com0com/files/com0com/3.0.0/com0com-3.0.0.0-x64-fre.zip
2.Unzip
3.Run exe file and click as the default settings;
4.Disable IntegrityChecks,run Windows powershell as administrator;
bcdedit /set nointegritychecks on
5.If pop error
An error has occurred setting the element data. The value is protected by Secure Boot policy and cannot be modified or deleted.
6.Go to BIOS setup,disble secure boot,restart;
7.Then run Windows powershell as administrator as below again.
bcdedit /set nointegritychecks on
8. Test Ports
Install-Package System.IO.Ports;
9.
using System.IO.Ports; namespace ConsoleApp32 { internal class Program { static void Main(string[] args) { SerialPortDemo(); } static void SerialPortDemo() { var names = SerialPort.GetPortNames(); foreach(var name in names) { Console.WriteLine(name); } } } }
//Sender
using System.IO.Ports; namespace ConsoleApp32 { internal class Program { static void Main(string[] args) { SerialPortDemo(); Console.WriteLine($"Send completed!"); } static int idx = 0; static void SerialPortDemo() { var names = SerialPort.GetPortNames(); foreach (var name in names) { Console.Write(name); } Console.WriteLine("\n\n\n"); try { using (SerialPort serialPort = new SerialPort("COM8", 9600, Parity.None, 8, StopBits.One)) { serialPort.Open(); serialPort.DataReceived += SerialPort_DataReceived; string msg = ""; for (int i = 1; i < 10001; i++) { msg = $"{++idx}_{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ffff")}_{Guid.NewGuid():N}"; serialPort.Write(msg); Console.WriteLine($"Send:{msg}"); Thread.Sleep(1000); } serialPort.Close(); } } catch (Exception ex) { Console.WriteLine(ex.Message); } } static string receivedMsg = ""; private static void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e) { var serialPort = (SerialPort)sender; receivedMsg = serialPort.ReadExisting(); Console.WriteLine($"Received:{serialPort}"); } } }
//Receiver
using System.IO.Ports; namespace ConsoleApp33 { internal class Program { private static SerialPort serialPort; static void Main(string[] args) { Console.CancelKeyPress += (s, e) => { e.Cancel = true; Console.WriteLine("Shutting down"); serialPort?.Close(); Environment.Exit(0); }; SerialPortReceiveDemo(); Console.ReadLine(); } static void SerialPortReceiveDemo() { try { serialPort = new SerialPort("COM9", 9600, Parity.None, 8, StopBits.One) { Handshake = Handshake.None, ReadTimeout = 10000 }; serialPort.Open(); serialPort.DataReceived += SerialPort_DataReceived; Console.WriteLine("Listening on COM9...Press Ctrl+C to exit."); } catch (Exception ex) { Console.WriteLine(ex.Message); } } static string receivedMsg = ""; private static void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e) { try { var serialPort = (SerialPort)sender; if(serialPort.BytesToRead>0) { receivedMsg = serialPort.ReadExisting(); Console.WriteLine($"Received:{receivedMsg}"); } } catch (Exception ex) { Console.WriteLine(ex.Message); } } } }