Ask a Question

Serial port communication

rlent
Contributor

Serial port communication

We do some testing involving serial communications with a device. Right now, we are using Tera Term to communicate. I'd like to bypass the terminal program entirely and just have TestComplete send strings to the device and receive responses. Does anyone have any sample code of how I can do this?

6 REPLIES 6
AlexKaras
Community Hero

Re: Serial port communication

Hi,

 

I don't have ready code samples, but I am pretty sure such communication is possible with the help of the CLR Bridge from TestComplete.

Try to search for C# / VB.Net code samples and port them to your scripting language (e.g. http://stackoverflow.com/questions/1243070/how-to-read-and-write-from-the-serial-port and http://stackoverflow.com/questions/13754694/what-is-the-correct-way-to-read-a-serial-port-using-net-...).

Code from https://support.smartbear.com/viewarticle/9003/ section can be used as an example.

In the worst case you may create a regular .Net class library that implements port communication that you need (with the assistance from your developers?) and just call methods of this library from your TestComplete code via the mentioned CLR Bridge.

Regards,
  /Alex [Community Hero]
____
[Community Heroes] 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. Posts made by [Community Heroes]
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.
The [Community Hero] signature is used with permission by SmartBear Software.
https://community.smartbear.com/t5/custom/page/page-id/hall-of-fame
================================
sanjay0288
Frequent Contributor

Re: Serial port communication

Hi,

   Wanted to know how would you read the response? Will you be reading the response from the port directly or is there any change/ data coming to your application UI

sanjay0288
Frequent Contributor

Re: Serial port communication

Hi @rlent

 

  The below smartbear link might be of some help.

 

https://support.smartbear.com/testcomplete/docs/scripting/working-with/com-ports.html?q=Serial+port#...

 

Below is the sample VB Script code which is taken from the link

 

Sub Test
  Dim Port, i, s

  Set Port = dotNET.System_IO_Ports.SerialPort.zctor_4("COM1", 9600)
  Port.Open

  ' Writing data to the port
  Port.Write "A " & Chr(27)
  ' Waiting for response
  aqUtils.Delay 1000
  ' Processing response
  If Port.BytesToRead <> 0 Then
    s = Port.ReadExisting
    Log.Message s
  Else
    Log.Warning "No data"
  End If

  Port.Close
End Sub

jonan
Occasional Contributor

Re: Serial port communication

Hi,

 

I am trying to send data to a COM port and found the following solution in TestComplete website

https://support.smartbear.com/testcomplete/docs/scripting/working-with/com-ports.html

 

I used the following solution as described in the web page (Python). However, when I run my test

it gave an error Permission denied. Any idea on how to solve this error?

 

def TestCOMPort():
  ForWriting = 2;
  TriStateFalse = 0;
  
  fso = Sys.OleObject['Scripting.FileSystemObject']
  f = fso.OpenTextFile("COM1:", ForWriting, False, TriStateFalse)
  # Write data to the port
  f.Write("A")
  f.Write(" ")
  f.Write("\x1B")
  f.Close()

 

AlexKaras
Community Hero

Re: Serial port communication

Hi,

 

a) What line of code produces the error?

b) Does this help: https://stackoverflow.com/questions/1153547/access-to-the-port-com1-is-denied ?

Regards,
  /Alex [Community Hero]
____
[Community Heroes] 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. Posts made by [Community Heroes]
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.
The [Community Hero] signature is used with permission by SmartBear Software.
https://community.smartbear.com/t5/custom/page/page-id/hall-of-fame
================================
jonan
Occasional Contributor

Re: Serial port communication

Hi Alex,

 

The error was permission denied.

 

Anyways, I solved this problem by just issuing a command on window cmd line to write character to a certain port by defining the following in my scripts.

 

def connectDevice():

cmd = "echo d > COM7"
os.system(cmd)
# Delay 10s
Delay(10000)

return

 

Thanks for the reply.

 

 

cancel
Showing results for 
Search instead for 
Did you mean: