private void openPort_Click(object sender, EventArgs e) { if (CheckBoxFill()) { //获取工作路径,若不存在该测试者,则创建文件夹 CommonVal.DataWorkSpace = SetWorkSpace(); _serialPort.Open(); _serialPort.DataReceived += new SerialDataReceivedEventHandler(p_DataReceived); MessageBox.Show("打开成功,正在接收数据"); // 在开始之前,先清除buffer中缓存的数据 _serialPort.DiscardInBuffer(); _serialPort.RequestWaveTransfer(DataConstants.WF_REQ_CONT_START, DataConstants.DRI_LEVEL_2005); _serialPort.RequestWaveTransfer(DataConstants.WF_REQ_CONT_START, DataConstants.DRI_LEVEL_2003); _serialPort.RequestWaveTransfer(DataConstants.WF_REQ_CONT_START, DataConstants.DRI_LEVEL_2001); _serialPort.DiscardInBuffer(); _serialPort.DataReceived += new SerialDataReceivedEventHandler(p_DataReceived); } }
public static void Main(string[] args) { Console.WriteLine("VitalSignsCaptureDraegerVent v1.003 (C)2017-20 John George K."); // Create a new SerialPort object with default settings. DSerialPort _serialPort = DSerialPort.getInstance; Console.WriteLine("Select the Port to which Draeger Ventilator (Medibus.X protocol) is to be connected, Available Ports:"); foreach (string s in SerialPort.GetPortNames()) { Console.WriteLine(" {0}", s); } Console.Write("COM port({0}): ", _serialPort.PortName.ToString()); string portName = Console.ReadLine(); if (portName != "") { // Allow the user to set the appropriate properties. _serialPort.PortName = portName; } Console.WriteLine(); Console.WriteLine("Numeric Data Transmission sets:"); Console.WriteLine("1. 5 second"); Console.WriteLine("2. 10 second"); Console.WriteLine("3. 1 minute"); Console.WriteLine("4. 5 minute"); Console.WriteLine("5. Single poll"); Console.WriteLine(); Console.Write("Choose Data Transmission interval (1-5):"); string sIntervalset = Console.ReadLine(); int[] setarray = { 5, 10, 60, 300, 0 }; short nIntervalset = 2; int nInterval = 10; if (sIntervalset != "") { nIntervalset = Convert.ToInt16(sIntervalset); } if (nIntervalset > 0 && nIntervalset < 6) { nInterval = setarray[nIntervalset - 1]; } Console.WriteLine(); Console.WriteLine("Waveform data export options:"); Console.WriteLine("0. None"); Console.WriteLine("1. Airway Pressure, Flow, Resp Volume"); Console.WriteLine("2. Pleth, O2 conc.(%), CO2 conc.(mmHg), Primary agent conc.(%)"); Console.WriteLine("3. Tracheal pressure, Inspiratory Flow"); Console.WriteLine("4. All"); Console.WriteLine(); Console.Write("Choose Waveform data export priority option (0-4):"); string sWaveformSet = Console.ReadLine(); short nWaveformSet = 0; if (sWaveformSet != "") { nWaveformSet = Convert.ToInt16(sWaveformSet); } try { _serialPort.Open(); _serialPort.m_nWaveformSet = nWaveformSet; if (_serialPort.OSIsUnix()) { dataEvent += new EventHandler((object sender, EventArgs e) => ReadData(sender)); } if (!_serialPort.OSIsUnix()) { _serialPort.DataReceived += new SerialDataReceivedEventHandler(p_DataReceived); } Console.WriteLine("You may now connect the serial cable to the Draeger Ventilator"); Console.WriteLine("Press any key to continue.."); Console.ReadKey(true); Console.WriteLine(); //Console.WriteLine("Requesting Transmission from monitor"); Console.WriteLine("Requesting Transmission set {0} from monitor", nIntervalset); Console.WriteLine(); Console.WriteLine("Data will be written to CSV file DrgVentExportData.csv in same folder"); _serialPort.RequestICC(); WaitForMilliSeconds(200); Task.Run(() => _serialPort.SendCycledRequests(nInterval)); //RequestRealtimeData after DevID response Task.Run(() => _serialPort.KeepConnectionAlive(2)); Console.WriteLine("Press Escape button to Stop"); if (_serialPort.OSIsUnix()) { do { if (_serialPort.BytesToRead != 0) { dataEvent.Invoke(_serialPort, new EventArgs()); } if (Console.KeyAvailable == true) { if (Console.ReadKey(true).Key == ConsoleKey.Escape) { break; } } }while (Console.KeyAvailable == false); } if (!_serialPort.OSIsUnix()) { ConsoleKeyInfo cki; do { cki = Console.ReadKey(true); }while (cki.Key != ConsoleKey.Escape); } } catch (Exception ex) { Console.WriteLine("Error opening/writing to serial port :: " + ex.Message, "Error!"); } finally { _serialPort.StopTransfer(); _serialPort.Close(); } }