private void SerialOpenClose(object obj) { // Close port if (SelectedPort.IsOpen) { MessageWriter.LogWrite(string.Format("Closing {0}...", SelectedPort.PortName)); SelectedPort.Close(); NotifyPropertyChanged("Port_IsOpen"); MessageWriter.LogWrite(SelectedPort.PortName + " closed"); SelectedPort.DataReceived -= new SerialDataReceivedEventHandler(SerialDataReceivedHandler); discoveredPeripherals.Clear(); } // Open port else { MessageWriter.LogWrite(string.Format("Opening {0}...", SelectedPort.PortName)); SelectedPort.PortName = SelectedPort.PortName; SelectedPort.Open(); SelectedPort.DiscardInBuffer(); SelectedPort.DiscardOutBuffer(); NotifyPropertyChanged("Port_IsOpen"); MessageWriter.LogWrite(SelectedPort.PortName + " opened"); SelectedPort.DataReceived += new SerialDataReceivedEventHandler(SerialDataReceivedHandler); BLEScan(); } }
private bool _SendData(string text) { bool result = false; if (SelectedPort.IsOpen) { if ((SelectedPort.WriteBufferSize - SelectedPort.BytesToWrite) > text.Length) { SelectedPort.Write(text); result = true; } else { if (null != ErrorData) { ErrorData("Tidak cukup buffer untuk mengirim pesan :" + text); } } } else { if (null != ErrorData) { ErrorData("Serial port closed"); } } return(result); }
public void Close() { if (SelectedPort.IsOpen) { while (SelectedPort.BytesToWrite > 0) { } SelectedPort.DataReceived -= dataReceivedHandler; SelectedPort.ErrorReceived -= errorReceivedHandler; SelectedPort.Close(); SelectedPort.Dispose(); } }
internal void SerialDataReceived(object sender, SerialDataReceivedEventArgs e) { string receivedData = string.Empty; if (DataMode == DataMode.Text) { receivedData = SelectedPort.ReadExisting(); } else if (DataMode == DataMode.Hex) { int length = SelectedPort.BytesToRead; byte[] datas = new byte[length]; SelectedPort.Write(datas, 0, length); receivedData = SerialUtilities.ByteArrayToHexString(datas); } // raise event to make the data available to other modules if (null != IncomingData) { IncomingData(receivedData); } }
public MainPageViewModel() { ConnectionSettingSelected = new RelayCommand(() => { if (SelectedPort != null && SelectedBoundRate != null) { CanOpenPort = true; } else { CanOpenPort = false; CanClosePort = false; } }); OpenPortButtonClick = new RelayCommand(() => { if (SelectedPort == null || SelectedBoundRate == null) { return; } try { serialPort = new Models.SerialPortCommunication(SelectedPort.ToString(), Convert.ToInt32(SelectedBoundRate.ToString())); try { serialPort.OpenConnection(); } catch (Exception ex) { System.Windows.MessageBox.Show($"{ex.Message}", "Error", MessageBoxButton.OK); return; } CanOpenPort = false; CanClosePort = true; /* Port Listening Button Enabling */ CanClear = true; CanReadContinuously = true; CanReadLine = true; CanSetTimeout = true; /* Port Sending Button Enabling */ CanSendData = true; /* Disabling Port and Bound Rate Selection */ CanSelectPort = false; CanSelectBoundRate = false; /* Updating Connection Status Ribbon */ StatusColor = ConnectedColor; } catch (Exception ex) { System.Windows.MessageBox.Show(ex.Message, $"Error opening port {SelectedPort}", MessageBoxButton.OK); } }); ClosePortButtonClick = new RelayCommand(() => { if (SelectedPort != null && serialPort.IsOpen == true) { try { if (listener != null && (listener.ThreadState == System.Threading.ThreadState.Running || listener.ThreadState == System.Threading.ThreadState.Background || listener.ThreadState == System.Threading.ThreadState.WaitSleepJoin)) { end = true; listener.Join(); ContinuousButton_text = "Read Continuously"; IsListening = false; } serialPort.CloseConnection(); /* Disabling receiving and sending data buttons */ CanReadContinuously = false; CanReadLine = false; CanSendData = false; CanClear = false; /* Enabling Port Settings Selection */ CanSelectPort = true; CanSelectBoundRate = true; /* Resetting Port Settings */ SelectedPort = null; SelectedBoundRate = null; CanOpenPort = false; CanClosePort = false; /* Updating Connection Status Ribbon */ StatusColor = NotConnectedColor; } catch (Exception ex) { MessageBox.Show(ex.Message, $"Error closing port {SelectedPort}", MessageBoxButton.OK); } } }); ClearButtonClick = new RelayCommand(() => { ReceivedData = ""; }); ContinuousReading_Click = new RelayCommand(() => { if (!IsListening) { IsListening = true; listener = new Thread(ListenSerialPort); //listeningMutex.WaitOne(); end = false; //listeningMutex.ReleaseMutex(); ContinuousButton_text = "Stop"; CanSetTimeout = false; CanReadLine = false; listener.Start(); //ListenSerialPort(); } else { //listeningMutex.WaitOne(); end = true; try { listener.Join(); } catch (Exception ex) { MessageBox.Show(ex.Message, "Exception", MessageBoxButton.OK); } //listeningMutex.ReleaseMutex(); ContinuousButton_text = "Read Continuously"; CanReadLine = true; CanSetTimeout = true; IsListening = false; } }); ReadDataClick = new RelayCommand(() => { if (!IsListening) { try { string receving = serialPort.ReadLine(timeout); ReceivedData += receving; } catch (Exception ex) { MessageBox.Show(ex.Message, $"Error reading Data from {SelectedPort}", MessageBoxButton.OK); } } }); SendDataButtonClick = new RelayCommand(() => { if (!IsListening) { try { serialPort.WriteLine(SendingData); SendingData = ""; } catch (Exception ex) { MessageBox.Show(ex.Message, $"Error sending Data on {SelectedPort}", MessageBoxButton.OK); } } }); }
public void Open() { SelectedPort.Open(); }