Instrument Control (GPIB, Serial, VISA, IVI)

cancel
Showing results for 
Search instead for 
Did you mean: 

Writing raw strings of HEX Bytes over TCPIP. (Modbus commands) using VisaComLib. in Excel VBA.

Solved!
Go to solution

Hi all.

 

Problem:

Lately i have been forced to work with instruments that mainly communicate via MODBUS.

The instrument of present headache is a "Network Analyzer" ABB M4M 30 ETHERNET.

I need to send a string of hex bytes to get the answers i need. That has been strait forward using a terminal program eg. Hercules in this example.

A little les easy with VISA Test Panel but in the end it worked. But i have not been able to communicate using the VisaComLib. in VBA Excel.

 

Hex Byte String to be send, could be:

0x00 0x01 0x00 0x00 0x00 0x06 0x01 0x03 0x5B 0x02 0x00 0x02

                                                                       |              |

Number 9 Byte specify what register to start return.     |

                                                          how many registers to return = 2

 

Instrument should reply with the voltage of L1 in register 0x5B02 and 0x5B03

0x00 0x01 0x00 0x00 0x00 0x07 0x01 0x03 0x04 0x00 0x00 0x09 0x1A

The last two bytes holding the result 0x91A = 0d2330 computes to 233V on Phase L1.

 

What have i tried:

With Hercules i just send 00 01 00 00 00 06 01 03 5B 02 00 02 with check in HEX and everything works perfect:

PowerKore_1-1767624129911.png

 

Using VISA Test Panel:

PowerKore_3-1767624710752.png

Worked after some google searches, added a "/" in front of every byte, and remove the “suppress end on reads” tic, then it also works.

 

But i cannot get VisaComLib.FormattedIO488 to write that same string in Excel VBA.

 

If i monitor the port in question with NI I/O Trace during above communications I get these lines:

With VISA Test Panel or Hercules:

PowerKore_2-1767624190915.png

12 bytes write registered but no information on what they are, you have to look at the buffers to see if the hex bytes has been send correct and if the answer is correct. (All OK)

 

But if i try to send with Excel VBA i cannot get VisaComLib.FormattedIO488 to write clean HEX. And especially not the vbNullChar

 

Lines of interest in my code (test code to get the VISA driver write something i can use not all lines are run at once in the end of cause):

Private MbusDevice As VisaComLib.FormattedIO488
Private MbusManager As VisaComLib.ResourceManager

Dim Query(12) As Byte                                                                      ' used with Byte and Integer with the same result

 

' These variables has been written as integers and Bytes, no change

Query(0) = 0
Query(1) = 22      ' dont mind the 01 is changed to 22 it dont matter. 
Query(2) = 0
Query(3) = 0
Query(4) = 0
Query(5) = 6
Query(6) = 1
Query(7) = 3
Query(8) = 91
Query(9) = 2
Query(10) = 0
Query(11) = 2

 

MbusDevice.WriteNumber &H0                                                                                                                                              ' to test if i can write 0 as number, it is send as asci 0

MbusDevice.WriteNumber &H0 & &H22 & &H0 & &H0 & &H0 & &H6 & &H1 & &H3 & &H5B & &H2 & &H0 & &H2            ' Is Converted by VISA to a string of single characters excluding the first &H0

MbusDevice.WriteString (RHS)                                                                                                                                              ' RHS containing the ascii string as i normally send to any other SCSI ready instrument, is bound to fail i know.

MbusDevice.WriteString Chr.(1) & Chr.(91) & vbNullChar & Chr.(22), True                                                                           ' This line works until a Chr.(0) or a vbNullChar (both tried) arrives after that nothing is send.

MbusDevice.WriteList Query , ASCIIType_R8, ",", True                                                                                                         ' This WriteList may have worked but is is written as text and must have a list seperator that ruins the hex command line.

 

NI I/O Trace output of the last five lines:

PowerKore_4-1767624791789.png

 

Using .WriteNumber:

Hex Bytes are send as ASCI characters

 

Using .WriteString:

Chr.(1) is showed as a "." (all hex Bytes under 20ish is showed as a ".")

Chr.(91) is correctly showed as a "["

Chr.(0) or vb.Null.Char is not showed and any other character after this is void, only the final LF that is also send and showed as a "." .

 

When i send HEX via a terminal, NI I/O Trace is not reporting any characters just how many i have send, Where when i use Excel VBA, every write command will show "." for any characters under Chr.(31) and end with a "." most likely representing a LF

 

It seems that anything written by VisaComLib.FormattedIO488 is considered text no matter what function i use and list separated by a "." unless i specify something else with the .WriteList.

The VISA Test Panel can send HEX with success, so what command line under VisaComLib.FormattedIO488 can do the same???

 

I could start writing a completely different code using kernel32 or some third party program but i would very much like to keep my code within VISA-COM.

 

Thanks in advance if anyone can send me in the correct direction.

 

My setup:

Windows 11

Microsoft® Excel® for Microsoft 365 MSO (Version 2511 Build 16.0.19426.20118) 32-bit

NI-MAX Version 2024 Q2

ABB M4M 30 Ethernet on "TCPIP0::192.168.1.208::502::SOCKET"

 

0 Kudos
Message 1 of 2
(181 Views)
Solution
Accepted by topic author PowerKore

Problem solved.

 

For HEX and other data that should be unprocessed. Use VisaComLib.IMessage instaed of VisaComLib.FormattedIO488

 

I have previously used VisaComLib as follows:

 

Private MbusDevice As VisaComLib.FormattedIO488
Private MbusManager As VisaComLib.ResourceManager

And then write with

Dim RHS As String

MbusDevice.WriteString (RHS)

 

This has worked perfectly for any communication that utilise ascii string commands.

 

But for Raw Hex strings there were always something extra in the strings send by the MbusDevice that ruined the HEX message.

 

I found that if i used:

 

Private MbusDevice As VisaComLib.IMessage                         ' Instead of "FormattedIO488"
Private MbusManager As VisaComLib.ResourceManager

And then write with an array of my bytes:

Dim RHS As String

Dim BytesToSend() As Byte
BytesToSend() = ByteArrayFromHexString(RHS)                      ' A little function that converts the string to Hex array if nessesary

MbusDevice.Write BytesToSend, (how may bytes to send as Long)

 

Then the hex strings is send as true HEX with no extra CR, LF, spacers etc. all the way from h00 to hFF

 

 

Note:

The initial intention was to write a macro tool for Excel, that can communicate with any instrument without 3rd party program (or very limited, I do use the VisaComLib), drivers or tools. No expensive communications packages, often limited to a narrow set of instruments, that limits the use of other programs and needs yearly updates and subscription fees.

A tool that will work with any Excel installation today, and tomorrow on a different installation at a sub suppler or test department without a huge list of programs and licenses to buy.

 

Perhaps this question nor this answer should have been posted in this forum, i should have used KEYSIGHT forum instead. Sorry if that is the case.

0 Kudos
Message 2 of 2
(59 Views)