static void Main(string[] args) { // Change this variable to the address of your instrument string VISA_ADDRESS = "Your instrument's VISA address goes here!"; // Create a connection (session) to the TCP/IP socket on the instrument. // Change VISA_ADDRESS to a SOCKET address, e.g. "TCPIP::169.254.104.59::5025::SOCKET" IMessageBasedSession session = GlobalResourceManager.Open(VISA_ADDRESS) as IMessageBasedSession; // The first thing you should do with a SOCKET connection is enable the Termination Character. Otherwise all of your read's will fail session.TerminationCharacterEnabled = true; // We can find out details of the connection ITcpipSocketSession socket = session as ITcpipSocketSession; Console.WriteLine("IP: {0}\r\nHostname: {1}\r\nPort: {2}\r\n", socket.Address, socket.HostName, socket.Port); // Send the *IDN? and read the response as strings MessageBasedFormattedIO formattedIO = new MessageBasedFormattedIO(session); formattedIO.WriteLine("*IDN?"); string idnResponse = formattedIO.ReadLine(); Console.WriteLine("*IDN? returned: {0}", idnResponse); // Close the connection to the instrument session.Dispose(); Console.WriteLine("Press any key to exit..."); Console.ReadKey(); }
static void Main(string[] args) { // Change this variable to the address of your instrument string VISA_ADDRESS = "Your instrument's VISA address goes here!"; // Create a connection (session) to the RS-232 device. // Change VISA_ADDRESS to a serial address, e.g. "ASRL1::INSTR" IMessageBasedSession session = GlobalResourceManager.Open(VISA_ADDRESS) as IMessageBasedSession; // The first thing you should do with a serial port is enable the Termination Character. Otherwise all of your read's will fail session.TerminationCharacterEnabled = true; // If you've setup the serial port settings in Connection Expert, you can remove this section. // Otherwise, set your connection parameters ISerialSession serial = session as ISerialSession; serial.BaudRate = 9600; serial.DataBits = 8; serial.Parity = SerialParity.None; serial.FlowControl = SerialFlowControlModes.DtrDsr; // Send the *IDN? and read the response as strings MessageBasedFormattedIO formattedIO = new MessageBasedFormattedIO(session); formattedIO.WriteLine("*IDN?"); string idnResponse = formattedIO.ReadLine(); Console.WriteLine("*IDN? returned: {0}", idnResponse); // Close the connection to the instrument session.Dispose(); Console.WriteLine("Press any key to exit..."); Console.ReadKey(); }
private void TempMonitor() { string idnResponse; while (true) { System.Threading.Thread.Sleep(500); formattedIOTEMPCON.WriteLine("input? a"); idnResponse = formattedIOTEMPCON.ReadLine().Replace("\n", ""); labelRealTimeTemperature.Text = idnResponse; fRealtimeTemp = Convert.ToSingle(idnResponse); } }
static void Main(string[] args) { // Change this variable to the address of your instrument string VISA_ADDRESS = "Your instrument's VISA address goes here!"; // Create a connection (session) to the instrument IMessageBasedSession session; try { session = GlobalResourceManager.Open(VISA_ADDRESS) as IMessageBasedSession; } catch (NativeVisaException visaException) { Console.WriteLine("Couldn't connect. Error is:\r\n{0}\r\nPress any key to exit...", visaException); Console.ReadKey(); return; } // Create a formatted I/O object which will help us format the data we want to send/receive to/from the instrument MessageBasedFormattedIO formattedIO = new MessageBasedFormattedIO(session); // For Serial and TCP/IP socket connections enable the read Termination Character, or read's will timeout if (session.ResourceName.Contains("ASRL") || session.ResourceName.Contains("SOCKET")) { session.TerminationCharacterEnabled = true; } // Send the *IDN? and read the response as strings formattedIO.WriteLine("*IDN?"); string idnResponse = formattedIO.ReadLine(); Console.WriteLine("*IDN? returned: {0}", idnResponse); // Close the connection to the instrument session.Dispose(); Console.WriteLine("Press any key to exit..."); Console.ReadKey(); }
private string[] sendCommand(string comm, float value = 1000) { string Comm; string idnResponse; string[] normalreturn = { DateTime.Now.ToString(), ":success" }; try { switch (comm) { case "APER": Comm = comm + " " + Inter() + txtAveRate.Text; formattedIOLRC.WriteLine(Comm); formattedIOLRC.WriteLine("APER?"); idnResponse = formattedIOLRC.ReadLine().Replace("\n", ""); return(normalreturn); case "Volt": Comm = comm + " " + txtOscVoltage.Text + "mV"; formattedIOLRC.WriteLine(Comm); formattedIOLRC.WriteLine("VOLT?"); idnResponse = formattedIOLRC.ReadLine().Replace("\n", ""); return(normalreturn); case "Freq": Comm = comm + " " + Convert.ToString(value) + "Hz"; formattedIOLRC.WriteLine(Comm); formattedIOLRC.WriteLine("Freq?"); idnResponse = formattedIOLRC.ReadLine().Replace("\n", ""); return(normalreturn); case "Func:IMP CPG": Comm = comm; formattedIOLRC.WriteLine(Comm); formattedIOLRC.WriteLine("Func:IMP?"); idnResponse = formattedIOLRC.ReadLine().Replace("\n", ""); return(normalreturn); case "Func:IMP CPRP": Comm = comm; formattedIOLRC.WriteLine(Comm); formattedIOLRC.WriteLine("Func:IMP?"); idnResponse = formattedIOLRC.ReadLine().Replace("\n", ""); return(normalreturn); case "Func:IMP CSRS": Comm = comm; formattedIOLRC.WriteLine(Comm); formattedIOLRC.WriteLine("Func:IMP?"); idnResponse = formattedIOLRC.ReadLine().Replace("\n", ""); return(normalreturn); case "BIAS:STAT 1": Comm = comm; formattedIOLRC.WriteLine(Comm); formattedIOLRC.WriteLine("BIAS:STAT?"); idnResponse = formattedIOLRC.ReadLine().Replace("\n", ""); return(normalreturn); case "BIAS:STAT 0": Comm = comm; formattedIOLRC.WriteLine(Comm); formattedIOLRC.WriteLine("BIAS:STAT?"); idnResponse = formattedIOLRC.ReadLine().Replace("\n", ""); return(normalreturn); case "Fetch?": string[] idnResponseFetch; Comm = comm; formattedIOLRC.WriteLine(Comm); idnResponse = formattedIOLRC.ReadLine().Replace("\n", ""); idnResponseFetch = idnResponse.Split(new string[] { "," }, StringSplitOptions.None); return(idnResponseFetch); //Special Fetch case "Fetch?[Special]": string[] idnResponseFetchSpecial; Comm = "Fetch?"; System.Threading.Thread.Sleep(1000); formattedIOLRC.WriteLine(Comm); while (1 == 1) { try { System.Threading.Thread.Sleep(1000); idnResponse = formattedIOLRC.ReadLine().Replace("\n", ""); break; } catch { } } idnResponseFetchSpecial = idnResponse.Split(new string[] { "," }, StringSplitOptions.None); return(idnResponseFetchSpecial); case "Fetch?[Special2]": string[] idnResponseFetchSpecial2; Comm = "Fetch?"; System.Threading.Thread.Sleep(100); formattedIOLRC.WriteLine(Comm); System.Threading.Thread.Sleep(100); idnResponse = formattedIOLRC.ReadLine().Replace("\n", ""); idnResponseFetchSpecial2 = idnResponse.Split(new string[] { "," }, StringSplitOptions.None); return(idnResponseFetchSpecial2); } } catch (NativeVisaException visaException) { return(null); } return(null); }
static void Main(string[] args) { // Change this variable to the address of your instrument string VISA_ADDRESS = "Your instrument's VISA address goes here!"; IMessageBasedSession session = null; MessageBasedFormattedIO formattedIO; // Part 1: // // Shows the mechanics of how to catch an exception (an error) in VISA.NET when it occurs. // To stimulate an error, the code will try to open a connection t an instrument at an invalid address... try { // First we'll provide an invalid address and see what error we get session = GlobalResourceManager.Open("BAD ADDRESS") as IMessageBasedSession; } catch (Exception ex) { Console.WriteLine("An exception has occurred!\r\n\r\n{0}\r\n", ex.ToString()); // To get more specific information about the exception, we can check what kind of exception it is and add specific error handling code // In this example, that is done in the ExceptionHandler method ExceptionHandler(ex); } // Part 2: // // Stimulate another error by sending an invalid query and trying to read its response. // // Before running this part, don't forget to set your instrument address in the 'VISA_ADDRESS' variable at the top of this method session = GlobalResourceManager.Open(VISA_ADDRESS) as IMessageBasedSession; formattedIO = new MessageBasedFormattedIO(session); // Misspell the *IDN? query as *IND? try { formattedIO.WriteLine("*IND?"); } catch (Exception ex) { Console.WriteLine("You'll never get here, because the *IND? data will get sent to the instrument successfully, it's the instrument that won't like it."); } // Try to read the response (*IND ?) try { string idnResponse = formattedIO.ReadLine(); Console.WriteLine("*IDN? returned: {0}", idnResponse); } catch (IOTimeoutException timeoutException) { Console.WriteLine("The ReadLine call will timeout, because the instrument doesn't know what to do with the command that we sent it."); // Check the instrument to see if it has any errors in its queue string rawError = ""; int errorCode = -1; string errorString = ""; while (errorCode != 0) { formattedIO.WriteLine("SYST:ERR?"); rawError = formattedIO.ReadLine(); errorCode = int.Parse(rawError.Split(',')[0]); errorString = rawError.Split(',')[1]; Console.WriteLine("Error code: {0}, error message: {1}", errorCode, errorString.Trim()); } } session.Dispose(); Console.WriteLine("\r\nPress any key to exit..."); Console.ReadKey(); }