cancel
Showing results for 
Search instead for 
Did you mean: 

Serial Port Communication

SOLVED
Highlighted
Occasional Contributor

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. 

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Community Hero

Re: Serial Port Communication

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");

 

Regards,
Alex
[Community Expert Group]
____
[Community Expert Group] members are not employed by SmartBear Software but
are just volunteers who have some experience with the tools by SmartBear Software
and a desire to help others. Postings made by [Community Expert Group] members
may differ from the official policies of SmartBear Software and should be treated
as the own private opinion of their authors and under no circumstances as an
official answer from SmartBear Software.
[Community Expert Group] signature is used with permission by SmartBear Software.
http://smartbear.com/forums/f83/t86934/community-experts/
================================

View solution in original post

4 REPLIES 4
Highlighted
Community Hero

Re: Serial Port Communication

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");

 

Regards,
Alex
[Community Expert Group]
____
[Community Expert Group] members are not employed by SmartBear Software but
are just volunteers who have some experience with the tools by SmartBear Software
and a desire to help others. Postings made by [Community Expert Group] members
may differ from the official policies of SmartBear Software and should be treated
as the own private opinion of their authors and under no circumstances as an
official answer from SmartBear Software.
[Community Expert Group] signature is used with permission by SmartBear Software.
http://smartbear.com/forums/f83/t86934/community-experts/
================================

View solution in original post

Highlighted
Occasional Contributor

Re: Serial Port Communication

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. 

Highlighted
Community Hero

Re: Serial Port Communication

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.)

 

Regards,
Alex
[Community Expert Group]
____
[Community Expert Group] members are not employed by SmartBear Software but
are just volunteers who have some experience with the tools by SmartBear Software
and a desire to help others. Postings made by [Community Expert Group] members
may differ from the official policies of SmartBear Software and should be treated
as the own private opinion of their authors and under no circumstances as an
official answer from SmartBear Software.
[Community Expert Group] signature is used with permission by SmartBear Software.
http://smartbear.com/forums/f83/t86934/community-experts/
================================
Occasional Contributor

Re: Serial Port Communication

Hi Alex, Thanks a lot for your help.

 

I tried to do it in Python (not in TestComplete), as I am more comfortable using Python, by converting the hex sequence into bytes and sending it to the port (I apologise I didn't know that it has to be in bytes and I might make mistake in JavaScript). 

 

I decided to import the external Python code into the TestComplete in JavaScript and run it with WScript.

Thank you!