Quantcast
Channel: Measurement Studio for VC++ topics
Viewing all articles
Browse latest Browse all 654

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

$
0
0
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>
usingnamespacestd;

classvisa{
public:
ViSessiondefaultRM, instr;
ViStatusstatus;
ViUInt32retCount;
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)
ViUInt32timeoutValue=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 ;
}

}
voidWrite(std ::stringscpiComm){
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 ;
}
}
voidRead(){
charresponse[1024] = {0};
ViUInt32bytesRead=0;
status=viRead(instr, (ViBuf)response, sizeof(response) -1, &bytesRead);
if (status<VI_SUCCESS) {
chardesc[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
floatvoltage=atof(response); // Convert string to float
std::cerr<<"AC Voltage: "<<voltage<<" V"<<std::endl;
}

}


};


intmain(){
visaobj;
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();
 
}

Viewing all articles
Browse latest Browse all 654

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>