TestComplete does not include special features for sending data to or receiving them from COM ports. One possible solution is to call the Windows Script Host to create an object associated with the desired port and then work with the port using methods and properties of this object:
JavaScript
function TestCOMPort()
{
let ForWriting = 2;
let TriStateFalse = 0;
let fso = getActiveXObject("Scripting.FileSystemObject");
let f = fso.OpenTextFile("COM1:", ForWriting, false, TriStateFalse);
// Write data to the port
f.Write("A");
f.Write(" ");
f.Write("\x1B");
f.Close();
}
JScript
function TestCOMPort()
{
var ForWriting = 2;
var TriStateFalse = 0;
var fso = new ActiveXObject("Scripting.FileSystemObject");
var f = fso.OpenTextFile("COM1:", ForWriting, false, TriStateFalse);
// Write data to the port
f.Write("A");
f.Write(" ");
f.Write("\x1B");
f.Close();
}
Python
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()
VBScript
Sub TestCOMPort
Const ForWriting = 2, TriStateFalse = 0
Dim fso, f
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile("COM1:", ForWriting, False, TriStateFalse)
' Write data to the port
f.Write Chr(26)
f.Write Chr(32)
f.Write Chr(27)
f.Close
End Sub
DelphiScript
procedure TestCOMPort;
var
fso, f, ForWriting, TriStateFalse : OleVariant;
begin
ForWriting := 2;
TriStateFalse := 0;
fso := Sys.OleObject['Scripting.FileSystemObject'];
f := fso.OpenTextFile('COM1:', ForWriting, false, TriStateFalse);
// Write data to the port
f.Write(26);
f.Write(' ');
f.Write(Chr(27));
f.Close();
end;
C++Script, C#Script
function TestCOMPort()
{
var ForWriting = 2;
var TriStateFalse = 0;
var fso = new ActiveXObject("Scripting.FileSystemObject");
var f = fso["OpenTextFile"]("COM1:", ForWriting, false, TriStateFalse);
// Write data to the port
f["Write"]("A");
f["Write"](" ");
f["Write"]("\x1B");
f["Close"]();
}
If you have Microsoft .NET Framework 2.0 installed, you can try another solution: the Framework includes the namespace System.IO.Ports
that contains classes for controlling COM ports. You can access this namespace in scripts through the dotNET
object. The following code snippet demonstrates how you can send data to and read them from COM port using this object.
The System.IO.Ports namespace is included in the system.dll assembly. This assembly is registered in the Global Assembly Cache (GAC). In order to use the namespace in scripts, register this assembly in the CLR Bridge Options page of the Project Properties editor:
|
JavaScript, JScript
function Test()
{
var Port, s;
Port = dotNET.System_IO_Ports.SerialPort.zctor_4("COM1", 9600);
Port.Open();
// Writing data to the port
Port.Write("A " + String.fromCharCode(27));
// 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();
}
Python
def Test():
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:
s = Port.ReadExisting()
Log.Message(s)
else:
Log.Warning("No data")
Port.Close()
VBScript
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
DelphiScript
procedure Test;
var
Port, s;
begin
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
begin
s := Port.ReadExisting;
Log.Message(s);
end
else
Log.Warning('No data');
Port.Close();
end;
C++Script, C#Script
function Test()
{
var Port, s;
Port = dotNET["System_IO_Ports"]["SerialPort"]["zctor_4"]("COM1", 9600);
Port["Open"]();
// Writing data to the port
Port["Write"]("A " + String["fromCharCode"](27));
// 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"]();
}