關於c#串列埠通訊
阿新 • • 發佈:2019-02-16
/// <summary> /// 獲取serialPort /// </summary> private SerialPort serialPort = new SerialPort(); /// <summary> /// Set/Get serial baudrate /// 設定/獲取序列波特率 /// </summary> public override int BaudRate { get { return serialPort.BaudRate; } set { serialPort.BaudRate = value; } } /// <summary> /// Discards data from the serial driver's receive buffer. /// 從串列埠的接收緩衝區丟棄資料 /// </summary> public override void Flush() { serialPort.DiscardInBuffer(); } /// <summary> /// Gets a value indicating the open or closed status of the SerialPort object. /// 獲取一個值,該值指示序列化埠物件的開啟或關閉狀態。 /// </summary> public override bool IsOpen { get { return serialPort.IsOpen; } } /// <summary> /// Opens a new serial port connection. /// 開啟一個新的串列埠連線 /// </summary> public override void Open() { serialPort.Open(); } /// <summary> /// Set/Get the port for communications, including but not limited to all available /// COM ports. /// 獲取或者設定串列埠埠號 /// </summary> public override string PortName { get { return serialPort.PortName; } set { serialPort.PortName = value; } } /// <summary> /// Gets or sets the number of milliseconds before a time-out occurs when /// a read operation does not finish. /// 獲取或設定串列埠讀取時間 /// </summary> public override int ReadTimeout { get { return serialPort.ReadTimeout; } set { serialPort.ReadTimeout = value; } } /// <summary> /// Reads a number of bytes from the SerialPort input buffer and writes those /// bytes into a byte array at the specified offset. /// </summary> /// <param name="length">The number of bytes to read.</param> /// <param name="messageSpace">The byte array to write the input to.</param> /// <param name="offset">The offset in the buffer array to begin writing.</param> /// <returns>The number of bytes read</returns> /// 讀取資料 public override int ReceiveBytes(int length, byte[] messageSpace, int offset) { return serialPort.Read(messageSpace, offset, length); } /// <summary> /// Writes a specified number of bytes to the serial port using data from a buffer. /// </summary> /// <param name="length">The number of bytes to write.</param> /// <param name="message">The byte array that contains the data to write to the port.</param> /// <param name="offset">The zero-based byte offset in the buffer parameter at which to begin copying bytes to the port.</param> /// 傳送資料 public override void SendBytes(int length, byte[] message, int offset) { serialPort.Write(message, offset, length); } /// <summary> /// Closes the port connection, /// 關閉串列埠 /// </summary> public override void Shutdown() { serialPort.Close(); } /// <summary> /// Gets or sets the number of milliseconds before a time-out occurs when /// a write operation does not finish. /// 寫資料超時時間 /// </summary> public override int WriteTimeout { get { return serialPort.WriteTimeout; } set { serialPort.WriteTimeout = value; } }