public RS232Task(NationalInstruments.VisaNS.SerialSession device) { this.device = device; }
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(); } }