Forum Discussion

francisd84's avatar
francisd84
Contributor
8 years ago
Solved

TCP/IP client example in JScript; how to send HEX values in bytearray?

Hi,

 

I really appreciate the TCP/IP Client script example for JScript found on the TestComplete site but I face to some difficulites to change the type of value to be sent.

 

My purpose of this script is to connect to an IP relay module and activate some relays on different test states (Start of tests, Error(s) or warning(s) in logs, etc), which they activate "production line " style  lights to have a real-time satus of test runs.

 

As I see here:

 

binaryData = dotNET.System_Text.Encoding.ASCII.GetBytes_2("TestMessage");

The binaryData variable is an "object" containing the bytearray of the string "TestMessage"

 

In my use case, I want to be able to send a byte array of HEX values. Ex: [58, 01, 11, 00, 00, 00, 01, 69]

 

I tried some ways to convert string into Hex and then into a bytearray but it never works....

 

Since I code in VB.net, I have some difficulties to figure how to do it in Jscript.  And since I cannot change the scripting language of project (already in JScript), I need to make it works using JScript.

 

Thanks a lot for your help!

 

 

 

 

 

 

 

7 Replies

  • I'm not familiar with Jscript...but you're calling .NET functions anyway so it doesn't really matter what the language is. I think its a logic problem not a language problem. I found this on stackoverflow which is doing something similar....

     

    http://stackoverflow.com/questions/25129402/how-to-convert-ascii-to-hexdecimal-in-vb-net

     

    byteArray = System.Text.ASCIIEncoding.ASCII.GetBytes(str)
    For i As Integer = 0 To byteArray.Length - 1
        hexNumbers.Append(byteArray(i).ToString("x"))
    Next

    ^The lines after GetBytes are the interesting part. I think your code isn't working because the output from GetBytes is not formatted in hexadecimal which is likely what the function you are passing it too to send the data through the socket is expecting. Try formatting the data like they do above with ToString("x"). You'll have to research that to find an equivalent in Jscript. Something to follow up anyway.

     

    One more thing...you are familiar with VB.NET...if you can't get JScript to do what you want DIRECTLY you can always write your code to do this in VB.NET, compile it into a DLL, then call the DLL to do what you want from JScript in TestComplete.

    • francisd84's avatar
      francisd84
      Contributor

      John_Laird wrote:

       

      One more thing...you are familiar with VB.NET...if you can't get JScript to do what you want DIRECTLY you can always write your code to do this in VB.NET, compile it into a DLL, then call the DLL to do what you want from JScript in TestComplete.


      Thanks a lot John.  You're right, it is a logic problem.

       

      After looking to your last quote, I started to think about that calling a DLL function will be more easier since I will just need to call the function I need.

       

      So I built a DLL with only one simple function that returns True or False in VB.net

       

      I tried, using the "Calling DLL functions for Tests" tutorial, to load and call the function in my DLL but it always fails...

       

      I'm pretty sure that I am doing it the wrong way in the TC script but I am not able to figure what is wrong.

       

      Here's my DLL code:

       

      Imports System.Net
      Public Class Class1
          Public Function Connect_Relay_Board(IP As String, Port As Integer)
              Try
                  TCPClient1 = New Sockets.TcpClient
                  TCPClient1.Connect(IP, Port)
              Catch ex As Exception
                  Return False
              End Try
      Return True
      End Function End Class

      And here's my script code:

       

      function Test()
      {
      var Ipaddress,port,iptest;
      var def_DLL, lib, i;
      
      Ipaddress = "192.168.46.12";
      port = 3000;
      
         def_DLL = DLL.DefineDLL("Class1");
         def_DLL.DefineProc("Connect_Relay_Board",VT_LPSTR,VT_INT,VT_BOOL);
         def_DLL.DefineAlias("Connect","Connect_Relay_Board");
         lib = DLL.Load("C:\\relayboard.dll","Class1");
         
       i = lib.Connect(Ipaddress ,port);
       Log.Message(i)
          
      }

       

      I wanted to make it simple to test it but apparently, it doesn't work..

      .

       

      i = lib.Connect(Ipaddress ,port);

      This line returns: Object doesn't support this property or method

       

       

      If you or someone else can, according to the DLL example, give me the right way to load and call DLL function in TC, I will really appreciate.

       

      Thank you very much for your help!

       

      Francis