在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
View Code
1 public static class ComPort// 2 { 3 public static Queue ByteList = Queue.Synchronized(new Queue()); 4 public static int JsByteSum = 0; 5 public static int FsByteSum = 0; 6 public static bool IsGroup = false; 7 public static int OneGroupSum = 16; 8 public static bool IsOpenCom 9 { 10 get 11 { 12 if (aSerialPor == null) 13 return false; 14 15 return aSerialPor.IsOpen; 16 } 17 } 18 public static string ErrMsg 19 { 20 get { return aErrMsg; } 21 } 22 23 private static SerialPort aSerialPor = null; 24 private static string aErrMsg = ""; 25 //private static C_Order aBuilderOrder = new C_Order(); 26 private static ComPortInfo aComInfo; 27 28 public static void InitComPort(ComPortInfo ComSet) 29 { 30 aComInfo = ComSet; 31 aSerialPor = new SerialPort(); 32 aSerialPor.BaudRate = ComSet.PortBaudRate; 33 aSerialPor.DataBits = 8; 34 aSerialPor.StopBits = StopBits.One; 35 aSerialPor.Parity = Parity.None; 36 aSerialPor.PortName = ComSet.PortName; 37 } 38 39 private static void InitComPort() 40 { 41 if (aSerialPor != null) 42 ColseCom(); 43 44 aSerialPor = new SerialPort(); 45 aSerialPor.BaudRate = aComInfo.PortBaudRate; 46 aSerialPor.DataBits = 8; 47 aSerialPor.StopBits = StopBits.One; 48 aSerialPor.Parity = Parity.None; 49 aSerialPor.PortName = aComInfo.PortName; 50 } 51 52 public static bool OpenCom() 53 { 54 if (aSerialPor == null) 55 { 56 aErrMsg = "未初始化的COM对象!"; 57 return false; 58 } 59 60 try 61 { 62 aSerialPor.DataReceived += new SerialDataReceivedEventHandler(aSerialPor_DataReceived); 63 64 aSerialPor.Open(); 65 JsByteSum = 0; 66 } 67 catch (Exception Err) 68 { 69 aErrMsg = Err.Message; 70 return false; 71 } 72 return true; 73 } 74 75 public static bool ColseCom() 76 { 77 ByteList.Clear(); 78 79 try 80 { 81 if (aSerialPor != null) 82 { 83 aSerialPor.DiscardInBuffer(); 84 aSerialPor.DiscardOutBuffer(); 85 aSerialPor.Close(); 86 aSerialPor.Dispose(); 87 aSerialPor = null; 88 } 89 } 90 catch (Exception Err) 91 { 92 MessageBox.Show(Err.Message, "提示"); 93 } 94 95 return true; 96 } 97 98 public static bool ComSetData(byte[] SetData) 99 { 100 if (aSerialPor == null) 101 { 102 aErrMsg = "未初始化COM对象"; 103 return false; 104 } 105 if (aSerialPor.IsOpen == false) 106 { 107 aErrMsg = "未打开的COM对象"; 108 return false; 109 } 110 111 aSerialPor.DiscardOutBuffer(); 112 113 try 114 { 115 aSerialPor.Write(SetData, 0, SetData.Length); 116 FsByteSum += SetData.Length; 117 } 118 catch (Exception Err) 119 { 120 aErrMsg = Err.Message; 121 return false; 122 } 123 return true; 124 } 125 126 public static bool ClearDataBuffer() 127 { 128 if (aSerialPor == null) 129 return true; 130 if (aSerialPor.IsOpen == false) 131 return true; 132 133 try 134 { 135 aSerialPor.DiscardInBuffer(); 136 } 137 catch (Exception Err) 138 { 139 aErrMsg = Err.Message; 140 return false; 141 } 142 143 return true; 144 } 145 146 private static void aSerialPor_DataReceived(object sender, SerialDataReceivedEventArgs e) 147 { 148 int iByteLen = aSerialPor.BytesToRead; 149 150 byte[] iBytes; 151 if (IsGroup) 152 { 153 int iGroup = iByteLen / OneGroupSum; 154 for (int i = 0; i < iGroup; i++) 155 { 156 iBytes = new byte[OneGroupSum]; 157 aSerialPor.Read(iBytes, 0, OneGroupSum); 158 JsByteSum += OneGroupSum; 159 ByteList.Enqueue(iBytes); 160 } 161 } 162 else 163 { 164 iBytes = new byte[iByteLen]; 165 aSerialPor.Read(iBytes, 0, iByteLen); 166 JsByteSum += iByteLen; 167 ByteList.Enqueue(iBytes); 168 } 169 } 170 171 } 172 173 public class ComPortInfo 174 { 175 public string PortName = "COM1"; 176 public int PortBaudRate = 9600; 177 public int InoupSize = 5120; 178 public int OutPutSize = 1024; 179 public int DataBits = 8; 180 public StopBits ComStopBits = StopBits.One; 181 public Parity ComParity = Parity.None; 182 } 183 } 184 185 下面是调用ComPort对象的方法 186 187 我是用了一个窗体来显示效果的 188 189 读的是两对红外传给电脑的信号 190 191 QQ截图20110920095642 192 193 StringBuilder aBytesStr = new StringBuilder(); 194 Regex aStrReg = new Regex("[^A-F0-9]"); 195 196 private void btn_Com_Click(object sender, EventArgs e) 197 { 198 if (btn_Com.Text == "打开串口") 199 { 200 if (CheckInfo() == false) 201 return; 202 203 ComPortInfo iComInfo = new ComPortInfo(); 204 205 int iComBtl = 19200; 206 Parity iComJyw = Parity.Odd; 207 int iComDataBit = 8; 208 StopBits iComStopBits = StopBits.One; 209 210 iComInfo.PortName = "COM4"; 211 iComInfo.ComParity = iComJyw; 212 iComInfo.ComStopBits = iComStopBits; 213 iComInfo.InoupSize = 5120; 214 iComInfo.OutPutSize = 1024; 215 iComInfo.PortBaudRate = iComBtl; 216 iComInfo.DataBits = iComDataBit; 217 218 ComPort.InitComPort(iComInfo); 219 if (ComPort.OpenCom() == false) 220 { 221 MessageBox.Show("打开串口出错!\n" + C_ComPort.ErrMsg, "提示"); 222 return; 223 } 224 225 this.btn_Com.Text = "关闭串口"; 226 timer_Ret.Start(); 227 } 228 else if (btn_Com.Text == "关闭串口") 229 { 230 ComPort.ColseCom(); 231 this.btn_Com.Text = "打开串口"; 232 timer_Ret.Stop(); 233 } 234 235 } 236 private bool CheckInfo() 237 { 238 return true; 239 } 240 241 private void timer_Ret_Tick(object sender, EventArgs e) 242 { 243 if (ComPort.ByteList.Count <= 0) 244 return; 245 246 byte[] iBytes = (byte[])ComPort.ByteList.Dequeue(); 247 OrderStr(iBytes); 248 } 249 250 private void OrderStr(byte[] OrderByte) 251 { 252 string lib1 = ""; 253 //string lib2 = ""; 254 for (int i = 4; i < OrderByte.Length; i++) 255 { 256 if (!("FF".Equals(OrderByte[i].ToString("X").PadLeft(2, '0')))) 257 { 258 aBytesStr.Append(OrderByte[i].ToString("X").PadLeft(2, '0') + " "); 259 } 260 } 261 aBytesStr.AppendLine(); 262 txt_Ret.Text += aBytesStr.ToString(); 263 lab_Jszjs.Text = ComPort.JsByteSum.ToString();//显示接收数 264 lib1 = aBytesStr.ToString(); 265 if(lib1.Length>2) 266 { 267 this.lab1h.Text = lib1.Substring(0, 2).Trim();//第一组数据 268 this.lab2h.Text = (lib1.Substring(5, 4)).Trim();//第二组数据 269 this.lab3h.Text = lib1.Length+""; 270 } 271 txt_Ret.SelectionStart = txt_Ret.Text.Length; 272 txt_Ret.ScrollToCaret(); 273 aBytesStr.Remove(0, aBytesStr.Length); 274 275 }
先创建一个XX.cs类,来初始化COM对象,详细代码如下:
如果要用在其他地方可做一方法。 如果有什么不对的地方请朋友们多多指教!
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论