Instrument Control (GPIB, Serial, VISA, IVI)

cancel
Showing results for 
Search instead for 
Did you mean: 

VISA COM, ReadString returns junk chars

Hi,
I posted the same quiestion on this discusssion forum but the reflector did not reflect my posting so let me again post the same one here.

I am using NI-VISA COM software from VB6 client to control a GPIB instrument. When receiving a query response string from the instrument by using IMessage.ReadString(nnn) method, sometimes the returned string has a junk character immediately after the 0x0A code. For example, the instrument accepts "CURR:RANG?" query that generates a response message "HIGH".

session.WriteString( "CURR:RANG?")
Dim strRd As String
strRd = session.ReadString(256)
// strRd is now equivalent to like "HIGH\nP"


In this example "P" is the junk. As I see NI SPY, there is no such junk shown but it just says *retCnt = 5.


I debuged into NiVisaCom.DLL to watch what is happening using VC++ debugger. Coming to a conclusion, definitely the legacy NI-488.2M API still has a sick that adds unneccesary junk character after termination when the client calls ibrd() function. Therefore the legacy VISA also has the same problem. As I debugged the situation when VisaCom.ReadString() calls the legacy viRead, the string buffer (char array) are shown as below on my debugger:


H I G H . P . . . .
48 49 47 48 0A 50 00 00 00 00 etc...
(The junk "P" may be different case by case.
This symptom may or may not occur depending on if the length is odd number or even number.)


I believe the NiVisaCom converts this buffer to a BSTR by using WideCharToMultiByte() and/or SysAllocStringLen. I think this conversion has a mistake on how to decide the length of valid part of the response string. I think the conversion is relying on NULL termination though it should be considered using dwLen reference passed to viRead call. Therefore the unneccesary junk character is definitely appended in the allocated BSTR.


I confirmed this symptom on the following environment:
GPIB = AT-GPIB/TNT
NI-488.2M version = 1.7
VISA Version = 2.6 Full
OS = Win2k Pro


I also checked a combination of NI-VISA 2.6 and Agilent I/O Lib L02.01 (using Agilent PCI-GPIB). Because the Agilent GPIB and the SICL I/O Library did not have the junk character problem, the NI-VISA COM did not show any strange behaviour.


Is this a bug of NI-VISA 2.6? If so can you fix the problem in the next version?


Thanks,
Makoto Kondo, Kikusui Electronics Corp.
0 Kudos
Message 1 of 2
(3,721 Views)
Hello Makoto,

Have you been able to reproduce this same behavior using the NI-488.2 communicator, VISA interactive control, or the VISA example program?

NI-488.2 communicator
  1. Open MAX
  2. Scan for instruments
  3. Right click on the instrument and select Communicate..

VISA interactive control
  1. Open MAX
  2. Scan for instruments
  3. Go to Tools»NI-VISA»Interactive Control
  4. Double click on your instrument
  5. Select the Basic I/O tab

Measurement Studio VB Example Program
C:\Program Files\National Instruments\MeasurementStudio\VB\Samples\Visa\Basic Visa

You mentioned that you have both NI and HP installed. Make sure you are configure the cards as outlined in KB 0KG1C7Z

Ray K
NI Applications Engineer
0 Kudos
Message 2 of 2
(3,721 Views)