public static bool WriteToSerialPort(byte[]byteArr) { SerialPort Com = newSerialPort(); try { Com.ReadTimeout = 5000; Com.WriteTimeout = 5000; Com.PortName = "Com1"; Com.BaudRate = 9600; Com.StopBits = StopBits.One; Com.Parity = Parity.None; Com.Open(); Com.Write(byteArr, 0,byteArr.Length); return true; } catch(Exception ex) { return false; } finally { Com.Close(); } }
本例使用的是方法Write(Byte[]buffer, Int32 offset, Int32 count)。該方法使用緩沖區的數據將指定數量的字節寫入串行端口。buffer為緩沖區,offset表示從此處開始將字節復制到端口,count表示要寫入的字節數。
新聞熱點
疑難解答