C# SerialPort 读写三菱FX系列PLC
1:串口初始化 com = new SerialPort("COM3", 9600, Parity.Even, 7, StopBits.One); 2:打开关闭串口 1 if (com.IsOpen) 2 { 3 com.Close();//关闭 4 } 5 6 com.Open();//打开 3:C# ASCII转字符及字符转ASCII 1 public static string Chr(int asciiCode) 2 { 3 if (asciiCode >= 0 && asciiCode <= 255) 4 { 5 System.Text.ASCIIEncoding asciiEncoding = new System.Text.ASCIIEncoding(); 6 byte[] byteArray = new byte[] { (byte)asciiCode }; 7 string strCharacter = asciiEncoding.GetString(byteArray); 8 return (strCharacter); 9 } 10 else 11 { 12 throw new Exception("ASCII Code is not valid."); 13 } 14 } 15 16 17 18