From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

TCPIP communication with instrument worked only once, failed for the second time ...

Solved!
Go to solution

Hi

 

I have a programmable power supply, and I'd like to use TCPIP protocol to communicate with that instrument.

I can send command and get the response at the first time, but failed when I try to send the second command and get the response.

For simplicity, I demonstrate my question by querying "*IDN?" command two times.

20190709011.png

It failed as the following error message.

20190709012.png

 

However, I can do it successfully by enabling one of the querying process and disabling the other.

20190709007.png

20190709008.png

20190709009.png

20190709010.png

 

I'm very confused why it failed to communicate with the instrument by the second querying, something like the TCPIP session is closed.

But when I tried it by Putty, it works to query twice.

20190710001.png

 

Does someone has any suggestion about this issue?

I just do not want to use the workaround to open and close the TCPIP session for every querying.

20190710002.png

20190710003.png

0 Kudos
Message 1 of 17
(2,719 Views)

You're reading 50 characters, and the string could be more then 50 characters.

 

It could be that the 51-52th character are \r\n. You'd read those the 2nd time, and the string would be there, but on the 2nd line that is hidden. A probe would clear that up as well...

 

I'd check if the reply strings are \r\n terminated. It would be very hard to communicate with if it isn't, as you don't know for sure how long the reply will be. So usually, it's \r\n terminated. Then, read (much) more characters, and wire a CRLF to the mode of the TCP read function.

 

It might be required to also send a \r\n after the commands you are sending...

Message 2 of 17
(2,704 Views)
Solution
Accepted by topic author William1225

Try adding a wait between your queries.  I have seen plenty of instruments "puke" when they get commands/requests too quickly together.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
Message 3 of 17
(2,685 Views)

Hi wiebe@CARYA,

 

Very thanks to your reply.

I think it is correct that the "bytes to read" for my response is 50 bytes.

I've tested by two ways:

1. Set the "bytes to read" to 51 instead of 50.

It will turns out to be "Timeout" and the response is still the 50 bytes of data.

20190710007.png

20190710006.png

 

2. Set the "bytes to read" to 25 in two successive querying process.

It will turns out to be half data separated in the two response.

20190710004.png

20190710005.png

 

So... if there are some characters, such as \r\n, are not read, I can read them in the second time.

In my case, the second querying process has no response at all and timed out.

It is wield but the Putty works.

 

0 Kudos
Message 4 of 17
(2,683 Views)

@William1225 wrote:

So... if there are some characters, such as \r\n, are not read, I can read them in the second time.

In my case, the second querying process has no response at all and timed out.


If the 2nd line started with a \r\n, all characters would have 'fallen off' the 2nd indicator, and might well go unnoticed. 

 

Reading 2X25 characters doesn't prove this, as a \r\n might still be send after the 2nd read.

 

As long as you're sure (the other tests seem solid), I'm OK with it (also if you're not sure btw Smiley Tongue).

 

What does the manual have to say about this? I think \r\n termination is pretty common. Have you tried sending "*IDN?\r\n" (\r\n in escape mode of course)? How would this work if the device doesn't have ethernet? You'd still be expecting 50 characters, and they won't come in...

0 Kudos
Message 5 of 17
(2,678 Views)

@William1225 wrote:

1. Set the "bytes to read" to 51 instead of 50.

It will turns out to be "Timeout" and the response is still the 50 bytes of data.


This convinces me (along with counting the characters you have read and the Putty output) that a Carriage Return (\r) and a Line Feed (\n) are being send by your instrument.  So I very highly recommend you update your TCP Read to read more like 100 bytes and set the mode to "CRLF" (enum input on top of the function).  This way you will be covered if an extra byte is sent or a byte is dropped for whatever reason.

 


@William1225 wrote:

In my case, the second querying process has no response at all and timed out.

It is wield but the Putty works.


You are doing things manually in Putty.  You are relatively slow compared to compiled code.  So I am 99% certain your instrument just doesn't like getting multiple messages within X ms of each other.  Again, add a wait between your queries.  I would use 50 or 100ms.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
0 Kudos
Message 6 of 17
(2,664 Views)

@crossrulz wrote:

@William1225 wrote:

1. Set the "bytes to read" to 51 instead of 50.

It will turns out to be "Timeout" and the response is still the 50 bytes of data.


This convinces me (along with counting the characters you have read and the Putty output) that a Carriage Return (\r) and a Line Feed (\n) are being send by your instrument.


There are 48 readable characters, to the other two making 50, are very likely \r and \n...

 

(Just to make this very, very clear)

 

Put those string indicators to show escaped characters to actually show this.

 

I'd try to add \r\n to the "*IDN?". That might be a deal breaker too. Putty ,might (optionally) add this automatically. Or add it because you actually press "enter".

0 Kudos
Message 7 of 17
(2,657 Views)

I'm not sure if anyone else mentioned this, but the default mode for the TCP Read is "STANDARD" mode i.e. No termination character. Try changing it to CRLF

 

EDIT- Oops Crossrulz already mentioned this

CRLF.PNG

HELP.PNG

 

 

0 Kudos
Message 8 of 17
(2,648 Views)

Can you communicate with it using VISA?  That would probably be a whole lot easier (not to mention it would be more scalable, too).  Try communicating with it in NI-MAX.  I'd try something like "TCPIP::192.168.1.8".

Bill
CLD
(Mid-Level minion.)
My support system ensures that I don't look totally incompetent.
Proud to say that I've progressed beyond knowing just enough to be dangerous. I now know enough to know that I have no clue about anything at all.
Humble author of the CLAD Nugget.
Message 9 of 17
(2,636 Views)

Hi crossrulz,

 

Actually, I had tried to add waiting time between two querying process, but it didn't work "that time".

Since I read the manual and found it mentioning waiting time is 50ms, so that I added 50ms and 100ms that time and failed.

20190710009.PNG

 

After I read wiebe@CARYA's message, I was wondering what if I sending "/r/n" between the two querying process(just like the processes in the Putty, I sent the first command and got the response, and pressed "Enter" key and sent the second command...??), it somehow works.

20190710010.png

 

And when I started to reply your message, I'd like to exerggerate the waiting time(1000ms) to demonstrate the waiting time might be no help but it worked surprisingly.

After some trials, I found 250ms is safe waiting time to ensure the second querying process work.

20190710008.png

 

Very appreciated to your advice.

0 Kudos
Message 10 of 17
(2,630 Views)