Measurement Studio for VC++

cancel
Showing results for 
Search instead for 
Did you mean: 

Getting Connection Lost Everytime whenever i am trying to use sleep in my cpp code

Using sleep in the below code gives me error  Connection lost , why , help me in this , i have mentioned where i am getting error
*************************************8
#include <iostream>
#include <visa.h>
#include <unistd.h>
using namespace std;

class visa{
public:
ViSession defaultRM, instr;
ViStatus status;
ViUInt32 retCount;
visa(){
status = viOpenDefaultRM(&defaultRM);
if (status < VI_SUCCESS) {
std::cerr << "Failed to open default resource manager." << std::endl;
return;
}

std::cerr << "Before TCP connection" << std::endl;

// Open the instrument with its VISA resource name
status = viOpen(defaultRM, "TCPIP0::192.168.2.2::30000::SOCKET", VI_NULL, ViUInt8(5000), &instr);
if (status < VI_SUCCESS) {
std::cerr << "Failed to open instrument." << std::endl;
viClose(defaultRM);
return;
}
status = viSetAttribute(instr, VI_ATTR_TERMCHAR, (ViChar)'\n');
if (status != VI_SUCCESS) {
std::cerr << "Could not set termination character!" << std::endl;
viClose(instr);
viClose(defaultRM);
return ;
}

status = viSetAttribute(instr, VI_ATTR_TERMCHAR_EN, VI_TRUE);
if (status != VI_SUCCESS) {
std::cerr << "Could not enable termination character!" << std::endl;
viClose(instr);
viClose(defaultRM);
return ;
}
//Set the timeout value to 5000 milliseconds (5 seconds)
ViUInt32 timeoutValue = 5000;
status = viSetAttribute(instr, VI_ATTR_TMO_VALUE, timeoutValue);
if (status < VI_SUCCESS) {
std::cerr << "Failed to set timeout value." << std::endl;
viClose(instr);
viClose(defaultRM);
return ;
}

}
void Write(std ::string scpiComm){
status = viWrite(instr, (ViBuf)scpiComm.c_str(), scpiComm.size(), VI_NULL);
if (status < VI_SUCCESS) {
std::cerr << "Failed to send SYSTem:REMote command." << std::endl;
viClose(instr);
viClose(defaultRM);
return ;
}
}
void Read(){
char response[1024] = {0};
ViUInt32 bytesRead=0;
status = viRead(instr, (ViBuf)response, sizeof(response) - 1, &bytesRead);
if (status < VI_SUCCESS) {
char desc[256];
viStatusDesc(instr, status, desc);
std::cerr << "Failed to read response, status: " << status << ", desc: " << desc << std::endl;
} else {
response[bytesRead] = '\0'; // Null-terminate the response

// Convert the response to a floating-point number
float voltage = atof(response); // Convert string to float
std::cerr << "AC Voltage: " << voltage << " V" << std::endl;
}

}


};


int main(){
visa obj;
obj.Write("SYST:REM\n");
obj.Write("SOUR:VOLT 10\n");
obj.Write("MEAS:VOLT:AC?\n");
obj.Read();
sleep(1); //commenting this works fine and uncommenting this getting connection lost/or some errors
obj.Write("SOUR:VOLT 20\n");
obj.Write("MEAS:VOLT:AC?\n");
obj.Read();
 
}
0 Kudos
Message 1 of 1
(392 Views)