/// <summary> /// This method outputs new commands when necessary. /// </summary> /// <param name="junk"></param> private bool runTick(uint elaspedTime_ms) { if (isDone) { return(false); } lock (commandBuffer) { long elaspedTime = DataStructures.Timing.Shared.MillisecondsToTicks(elaspedTime_ms); if (currentCommand >= commandBuffer.Count) { if (this.Done != null) { this.Done(this, new NationalInstruments.DAQmx.TaskDoneEventArgs(null)); } isDone = true; return(false); } while (elaspedTime >= commandBuffer[currentCommand].commandTime) { device.Write(commandBuffer[currentCommand].command); device.Flush(BufferTypes.OutBuffer, false); AtticusServer.server.messageLog(this, new MessageEvent("Output rs232 command: " + commandBuffer[currentCommand].command, 1, MessageEvent.MessageTypes.Log, MessageEvent.MessageCategories.Serial)); currentCommand++; if (currentCommand >= commandBuffer.Count) { break; } } } // } // catch (Exception e) // { // AtticusServer.server.messageLog(this, new MessageEvent("Caught exception in RS232 task: " + e.Message + e.StackTrace)); // AtticusServer.server.messageLog(this, new MessageEvent("Stopping rs232 task.")); // if (this.Done != null) // { // this.Done(this, new NationalInstruments.DAQmx.TaskDoneEventArgs(e)); // isDone = true; // } // } return(true); }
private void Start_Click(object sender, System.EventArgs e) { //Read in the selected values from the panel and configure the serial port accordingly. myAlias = VISAResource.SelectedItem.ToString(); baudRate = int.Parse(BaudRate.Text); dataBits = short.Parse(DataBits.Text); parity = Parity.Text; stopBits = StopBits2.Text; readDelay = int.Parse(Delay.Text); flowControlText = FlowControl.Text; writeBuffer = WriteBuffer.Text; try { //Open a new VISA session by creating an instance of a SerialSession //object. mySession = new NationalInstruments.VisaNS.SerialSession(myAlias); //Configure the VISA session. mySession.BaudRate = baudRate; mySession.DataBits = dataBits; //Set the flow control. switch (flowControlText) { case "XON/XOFF": mySession.FlowControl = FlowControlTypes.XOnXOff; break; case "None": flowControl = FlowControlTypes.None; break; case "RTS/CTS": flowControl = FlowControlTypes.RtsCts; break; case "DTR/DSR": flowControl = FlowControlTypes.DtrDsr; break; } mySession.FlowControl = flowControl; //Set the parity. switch (parity) { case "None": mySession.Parity = NationalInstruments.VisaNS.Parity.None; break; case "Even": mySession.Parity = NationalInstruments.VisaNS.Parity.Even; break; case "Odd": mySession.Parity = NationalInstruments.VisaNS.Parity.Odd; break; case "Mark": mySession.Parity = NationalInstruments.VisaNS.Parity.Mark; break; case "Space": mySession.Parity = NationalInstruments.VisaNS.Parity.Space; break; } //Set the stop bits. switch (stopBits) { case "1.0": mySession.StopBits = NationalInstruments.VisaNS.StopBitType.One; break; case "1.5": mySession.StopBits = NationalInstruments.VisaNS.StopBitType.OneAndOneHalf; break; case "2.0": mySession.StopBits = NationalInstruments.VisaNS.StopBitType.Two; break; } //Check if the Write button is set to on or off. if (axCWButton1.Value) { mySession.Write(writeBuffer); //Write the data to the buffer } //Delay between writing and reading. Thread.Sleep(readDelay); //Check if the Read button is set to on or off. if (axCWButton2.Value) { //Read all available data from the buffer and display it onscreen. readBuffer = mySession.ReadString(mySession.AvailableNumber); ReadBuffer.Text = readBuffer; bytesRead = readBuffer.Length; BytesRead.Text = bytesRead.ToString(); } } catch (VisaException ex) { MessageBox.Show(ex.Message); } finally { //Dispose of the SerialSession instance to close the VISA session. mySession.Dispose(); } }