Forum Discussion

rancan's avatar
rancan
Occasional Contributor
5 years ago
Solved

Serial Port Communication

Hi,

 

I am trying to create a connection with port com, and I am using JavaScript.

The goal is to send the HEX sequence and read the HEX back from the port. 

I copied from this page https://support.smartbear.com/testcomplete/docs/scripting/working-with/com-ports.html and I modified a bit, here's what I have:

 

function Test()
{
  var Port, s;

  Port = dotNET.System_IO_Ports.SerialPort.zctor_4("COM6", 9600);
  Port.Open();

  // Writing data to the port
  Port.Write("1A030688");
  // Waiting for response
  aqUtils.Delay(1000);
  // Processing response
  if (Port.BytesToRead != 0)
  {
    s = Port.ReadExisting;
    Log.Message(s);
  }
  else
    Log.Warning("No data");

  Port.Close();
}

It gave me "No Data", I believe it's because I was sending HEX instead of Bytes, but I am not sure how to fix it. 

When i tried to remove the 'if' clause, it gave me a success, but it's blank. 

 

It's been a few days but I still don't know how to solve it. I would really appreciate it if anyone could help me with this. 

Thank you very much. 

  • Hi,

     

    The goal is to send the HEX sequence

    > Port.Write("1A030688");

    The above .Write() call does not write expected hex sequence, but writes the ASCII string (with relevant hex codes), which is not correct as I think.

    Try to prefix every hex with \x prefix (like in the article you've referenced) and check if it helps. I.e.:

    Port.Write("\x1A");

    Port.Write("\x03");

    Port.Write("\x06");

    Port.Write("\x88");

     

4 Replies

  • AlexKaras's avatar
    AlexKaras
    Champion Level 3

    Hi,

     

    The goal is to send the HEX sequence

    > Port.Write("1A030688");

    The above .Write() call does not write expected hex sequence, but writes the ASCII string (with relevant hex codes), which is not correct as I think.

    Try to prefix every hex with \x prefix (like in the article you've referenced) and check if it helps. I.e.:

    Port.Write("\x1A");

    Port.Write("\x03");

    Port.Write("\x06");

    Port.Write("\x88");

     

    • rancan's avatar
      rancan
      Occasional Contributor

      Hi, 

       

      Thanks for the response. 

      I just tried to modify the Port.Write () as you mentioned, however, it still gave the blank reading of the port, or maybe is it because of the Port.BytesToRead? 

       

      Thanks in advance. 

      • AlexKaras's avatar
        AlexKaras
        Champion Level 3

        Hi,

         

        maybe is it because of the Port.BytesToRead?

        No, I don't think so...

        (Though I expect that you are sending correct sequence and that you know what your device must reply with.)

         

        I probably mentioned not correct code example from the article.

        Have you tried this (parameter's syntax might need to be adjusted):

        Port.Write(String.fromCharCode(1A));

        Port.Write(String.fromCharCode(03));

        Port.Write(String.fromCharCode(06));

        Port.Write(String.fromCharCode(88));

         

        P.S. One more think to check - is the given COM-port enabled in your system? (In BIOS, OS, etc.)