Instrument Control (GPIB, Serial, VISA, IVI)

cancel
Showing results for 
Search instead for 
Did you mean: 

why "viWrite(instr,"ISET 3.0",8,&m_retCount)" is error in vc++6.0?

i write "viWrite(instr,"ISET 3.0",8,&m_retCount)" in vc++6.0,but when i build this project,some error occur.the error message is cannot convert parameter 2 from 'char *' to 'unsigned char *' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast. Why? VC++6.0 with sp5, winXP, NI-VISA 2.5.
0 Kudos
Message 1 of 6
(5,486 Views)
Hello,

Thanks for contacting National Instruments.

I tried to reproduce the error you are seeing and I was unsuccessful at reproducing it. I used the same line of code you have above and I did not see the error you are referring to. Have you properly included the header file and the VISA32.lib? Also, are you able to successfully run one of the examples programs found here:

C:\VXIpnp\WinNT\NIvisa\Examples\C\Gpib

Regards,

Steven B.
Applications Engineering
National Instruments
0 Kudos
Message 2 of 6
(5,485 Views)
Hey xxddniao,

I copied an pasted your code with the addition of a \n "viWrite(instr,"ISET 3.0\n",9,&m_retCount)" into one of the visa shipping examples. The only reason I added the termination character was so that the application would read the data back.

C:\VXIpnp\WinNT\NIvisa\Examples\C\Serial

Then I tied pins 2 and 3 together on my serial port to create a loop back and the code ran excellent. No compile errors at all. I even left the code "viWrite(instr,"ISET 3.0",8,&m_retCount)" and it compile without any problems. So, I am not sure why you are getting the errors. I am also using VC++ 6.0. You can try to type cast the function similar to "viWrite(instr,(ViBuf)"ISET 3.0\n",9,&writeCount);".

I hope this helps out. I have attached the pr
oject that I was using.

JoshuaP
National Instruments
0 Kudos
Message 3 of 6
(5,486 Views)
Hi,
 
I have the same problem as "xxddniao",  and I already tried the program provided by JoshuaP. The compilation is OK.
 
The problem is the same code "viWrite(instr,"ISET 3.0\n",9,&m_retCount)" , compilation failed in my program, but OK in JoshuaP's program.
 
Anybody can help?
Thanks a lot!
 
Shen Rui
0 Kudos
Message 4 of 6
(5,327 Views)

It looks like you are compiling C++ code, not C.  The 2nd param of viWrite() function is declared as ViBuf type in visa.h, which is also TYPEDEFed as unsigned char*.  For VC++ compiler, the signed char* and unsigned char* are much different types therefore no implicit conversion is applied.  Instead you must explicitly typecast for the parameter.  Also a string constant such as "*IDN?\n" is considered as const char* type, which also requires typecast if converting non-const type.

vs = viWrite( vi, reinterpret_cast<ViBuf>("ISET 3.0\n"), 9, &dwRet);  // C++ style typecast
or
vs = viWrite( vi, (ViBuf)"ISET 3.0\n", 9, &dwRet);  // legacy C style typecast

Also, if you just send an ASCII string, consider using viPrintf() function.

vs = viSetAttribute( vi, VI_ATTR_WR_BUF_OPER_MODE, VI_FLUSH_ON_ACCESS);
...
vs = viPrintf( vi, "ISET %0.1f\n", 3.0);

The viPrintf() is much simpler than viWrite()  for ASCII write, because it does not require the size params, and you can use printf() style syntax.  Mind that you should set Wrtite-Buffer-Operation mode to Flush-On-Access, because some VISA implementation may not flush out (= write to instrument) immediately when viPrintf() has been called as default.  (No need to call it everytime, but only once is enough.)  Plus, do not forget to add "\n" termination on the string to write.  viPrintf() explicitly requires a terrmination chracter.

Makoto

このメッセージは 04-21-2006 05:36 PMに Makoto が編集しています。

0 Kudos
Message 5 of 6
(5,322 Views)
Hi,

brilliant dear makoto,

I agree totally in the subtle differences between C++ and C you
mentionned,

Regards,

FG

0 Kudos
Message 6 of 6
(5,290 Views)