public int ByteRead() { if (xSerialPort == null) { return(-1); } try { int xReadByte = xSerialPort.ReadByte(); if (xReadByte > 0) { int RxData = (int)xSerialPort.recvBuffer[0]; return(RxData); } return(-1); } catch (IOException ex) { MessageBox.Show("ポートからの読み込み中、I/O 例外が発生しました。" + ex.ToString(), "エラー", MessageBoxButtons.OK, MessageBoxIcon.Error); return(-1); } catch (InvalidOperationException ex) { MessageBox.Show("ポートからの読み込み中、不正処理例外が発生しました。" + ex.ToString(), "エラー", MessageBoxButtons.OK, MessageBoxIcon.Error); return(-1); } }
/// <summary> /// Receives the data. /// </summary> public byte[] ReceiveData() { byte[] RxText; if (xSerialPort == null) { RxText = new byte[1]; RxText[0] = 0; return(RxText); } try { /* * int rbyte = xSerialPort.BytesToRead; * byte[] buffer = new byte[rbyte]; * int read = 0; * while (read < rbyte) * { * int length = xSerialPort.Read(buffer, read, rbyte - read); * read += length; * } * if (rbyte > 0) * { * DataReceived(buffer); * } */ int xReadByte = xSerialPort.ReadByte(); if (xReadByte > 0) { RxText = new byte[xReadByte + 1]; RxText[0] = 1; for (int i = 1; i < (xReadByte + 1); i++) { RxText[i] = xSerialPort.RxBuffer[i - 1]; } return(RxText); } } catch (IOException ex) { MessageBox.Show("ポートからの読み込み中、I/O 例外が発生しました。" + ex.ToString(), "エラー", MessageBoxButtons.OK, MessageBoxIcon.Error); } catch (InvalidOperationException ex) { MessageBox.Show("ポートからの読み込み中、不正処理例外が発生しました。" + ex.ToString(), "エラー", MessageBoxButtons.OK, MessageBoxIcon.Error); } RxText = new byte[1]; RxText[0] = 0; return(RxText); }