---------- 20190806 public Boolean SendComPort(Boolean bReadLight) { try { //Boolean bSendRed = false; using (SerialPort comport = new SerialPort(msCOMPort, 9600, Parity.None, 8, StopBits.One)) { //SerialPort comport; //comport = new SerialPort(msCOMPort, 9600, Parity.None, 8, StopBits.One); Byte[] buffer = new Byte[1]; if (bReadLight) { //buffer[0] = 0x11; // 紅燈開啟 buffer[0] = 0x19; // 紅燈+蜂鳴器開啟 } else { //buffer[0] = 0x21; // 紅燈關閉 buffer[0] = 0x29; // 紅燈+蜂鳴器 關閉 } if (!comport.IsOpen) comport.Open(); comport.Write(buffer, 0, buffer.Length); comport.Close(); miErrorCOMPort = 0; return true; } } catch (Exception e1) { miErrorCOMPort++; CProject.gMe.ShowError($"COMPORT 警示燈無法控制. ${e1.Message}"); return false; } } ---------- SerialPort.txt The following code example demonstrates the use of the SerialPort class to allow two users to chat from two separate computers connected by a null modem cable. In this example, the users are prompted for the port settings and a username before chatting. Both computers must be executing the program to achieve full functionality of this example. Remarks Use this class to control a serial port file resource. This class provides synchronous and event-driven I/O, access to pin and break states, and access to serial driver properties. Additionally, the functionality of this class can be wrapped in an internal Stream object, accessible through the BaseStream property, and passed to classes that wrap or use streams. The SerialPort class supports the following encodings: ASCIIEncoding, UTF8Encoding, UnicodeEncoding, UTF32Encoding, and any encoding defined in mscorlib.dll where the code page is less than 50000 or the code page is 54936. You can use alternate encodings, but you must use the ReadByte or Write method and perform the encoding yourself. You use the GetPortNames method to retrieve the valid ports for the current computer. If a SerialPort object becomes blocked during a read operation, do not abort the thread. Instead, either close the base stream or dispose of the SerialPort object. // Use this code inside a project created with the Visual C# > Windows Desktop > Console Application template. // Replace the code in Program.cs with this code. using System; using System.IO.Ports; using System.Threading; public class PortChat { static bool _continue; static SerialPort _serialPort; public static void Main() { string name; string message; StringComparer stringComparer = StringComparer.OrdinalIgnoreCase; Thread readThread = new Thread(Read); // Create a new SerialPort object with default settings. _serialPort = new SerialPort(); // Allow the user to set the appropriate properties. _serialPort.PortName = SetPortName(_serialPort.PortName); _serialPort.BaudRate = SetPortBaudRate(_serialPort.BaudRate); _serialPort.Parity = SetPortParity(_serialPort.Parity); _serialPort.DataBits = SetPortDataBits(_serialPort.DataBits); _serialPort.StopBits = SetPortStopBits(_serialPort.StopBits); _serialPort.Handshake = SetPortHandshake(_serialPort.Handshake); // Set the read/write timeouts _serialPort.ReadTimeout = 500; _serialPort.WriteTimeout = 500; _serialPort.Open(); _continue = true; readThread.Start(); Console.Write("Name: "); name = Console.ReadLine(); Console.WriteLine("Type QUIT to exit"); while (_continue) { message = Console.ReadLine(); if (stringComparer.Equals("quit", message)) { _continue = false; } else { _serialPort.WriteLine( String.Format("<{0}>: {1}", name, message)); } } readThread.Join(); _serialPort.Close(); } public static void Read() { while (_continue) { try { string message = _serialPort.ReadLine(); Console.WriteLine(message); } catch (TimeoutException) { } } } // Display Port values and prompt user to enter a port. public static string SetPortName(string defaultPortName) { string portName; Console.WriteLine("Available Ports:"); foreach (string s in SerialPort.GetPortNames()) { Console.WriteLine(" {0}", s); } Console.Write("Enter COM port value (Default: {0}): ", defaultPortName); portName = Console.ReadLine(); if (portName == "" || !(portName.ToLower()).StartsWith("com")) { portName = defaultPortName; } return portName; } // Display BaudRate values and prompt user to enter a value. public static int SetPortBaudRate(int defaultPortBaudRate) { string baudRate; Console.Write("Baud Rate(default:{0}): ", defaultPortBaudRate); baudRate = Console.ReadLine(); if (baudRate == "") { baudRate = defaultPortBaudRate.ToString(); } return int.Parse(baudRate); } // Display PortParity values and prompt user to enter a value. public static Parity SetPortParity(Parity defaultPortParity) { string parity; Console.WriteLine("Available Parity options:"); foreach (string s in Enum.GetNames(typeof(Parity))) { Console.WriteLine(" {0}", s); } Console.Write("Enter Parity value (Default: {0}):", defaultPortParity.ToString(), true); parity = Console.ReadLine(); if (parity == "") { parity = defaultPortParity.ToString(); } return (Parity)Enum.Parse(typeof(Parity), parity, true); } // Display DataBits values and prompt user to enter a value. public static int SetPortDataBits(int defaultPortDataBits) { string dataBits; Console.Write("Enter DataBits value (Default: {0}): ", defaultPortDataBits); dataBits = Console.ReadLine(); if (dataBits == "") { dataBits = defaultPortDataBits.ToString(); } return int.Parse(dataBits.ToUpperInvariant()); } // Display StopBits values and prompt user to enter a value. public static StopBits SetPortStopBits(StopBits defaultPortStopBits) { string stopBits; Console.WriteLine("Available StopBits options:"); foreach (string s in Enum.GetNames(typeof(StopBits))) { Console.WriteLine(" {0}", s); } Console.Write("Enter StopBits value (None is not supported and \n" + "raises an ArgumentOutOfRangeException. \n (Default: {0}):", defaultPortStopBits.ToString()); stopBits = Console.ReadLine(); if (stopBits == "" ) { stopBits = defaultPortStopBits.ToString(); } return (StopBits)Enum.Parse(typeof(StopBits), stopBits, true); } public static Handshake SetPortHandshake(Handshake defaultPortHandshake) { string handshake; Console.WriteLine("Available Handshake options:"); foreach (string s in Enum.GetNames(typeof(Handshake))) { Console.WriteLine(" {0}", s); } Console.Write("Enter Handshake value (Default: {0}):", defaultPortHandshake.ToString()); handshake = Console.ReadLine(); if (handshake == "") { handshake = defaultPortHandshake.ToString(); } return (Handshake)Enum.Parse(typeof(Handshake), handshake, true); } }