LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Getting Connection Lost Everytime whenever i am trying to use sleep in my c++ 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 defaultRMinstr;
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_NULLViUInt8(5000), &instr);
if (status < VI_SUCCESS) {
std::cerr << "Failed to open instrument." << std::endl;
viClose(defaultRM);
return;
}
status = viSetAttribute(instrVI_ATTR_TERMCHAR, (ViChar)'\n');
if (status != VI_SUCCESS) {
std::cerr << "Could not set termination character!" << std::endl;
viClose(instr);
viClose(defaultRM);
return ;
}

 

status = viSetAttribute(instrVI_ATTR_TERMCHAR_ENVI_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(instrVI_ATTR_TMO_VALUEtimeoutValue);
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)responsesizeof(response- 1&bytesRead);
if (status < VI_SUCCESS) {
char desc[256];
viStatusDesc(instrstatusdesc);
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 5
(423 Views)

Hi sanjeev,

 


@sanjeev553 wrote:
Using sleep in the below code gives me error  Connection lost , why , help me in this , i have mentioned where i am getting error

Which device are you communicating with?

What is the connection timeout setting of that device?

 

(Why do you ask in a LabVIEW board? 🙂 )

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 2 of 5
(415 Views)

Thanks for  your reply.
I am communicating with https://www.bkprecision.com/products/power-supplies/9803  BKPrecision PowerSupply 9803 . 
I don't know about the connection timeout settings of the device . Can you suggest me why it is happening . What are the possible reasons . Why i am getting connection lost when any one VISA API hits.

(i have posted in other board as well but didnt got any response there.)

0 Kudos
Message 3 of 5
(370 Views)

Hi sanjeev,

 


@sanjeev553 wrote:

I don't know about the connection timeout settings of the device . Can you suggest me why it is happening . What are the possible reasons . Why i am getting connection lost when any one VISA API hits.


Many devices I worked with keep their own "connection timeout" setting: they expect some messages within a certain time frame and when they don't receive a message they end the TCP connection on their side.

So you have to read the manual of your device to learn what this timeout is set to and how to change that setting (if there is an option to)…

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 4 of 5
(364 Views)

Thanks for the reply .

Will check for the same as you mentioned and get back to you. Thanks for the suggestion.

0 Kudos
Message 5 of 5
(354 Views)