public void WriteHex(Hex hex) { try { m_IsTryToClosePort = false; if (!sp.IsOpen) { sp.Open(); } byte[] bytes = RS232Utils.Hex2Bytes(hex.hexstring); sp.Write(bytes, 0, bytes.Length); } catch { throw; } }
public void Sp_HexReceived(object sender, SerialDataReceivedEventArgs e) { if (m_IsTryToClosePort) { return; } m_IsReceiving = true; try { if (sp.IsOpen) { Thread.Sleep(100); int n = sp.BytesToRead; byte[] buf = new byte[n]; sp.Read(buf, 0, n); WHHexReader?.Invoke(new Hex(RS232Utils.BytesTohexString(buf)), sp.accessoryData); sp.DiscardInBuffer(); } } finally { m_IsReceiving = false; } }