Tutorial
Introduction
This guide helps you to create your first MB-Tester script. The example will use MP communication. The IDE used in this example is the Visual Studio Community 2019 edition.
Important: This tutorial assumes that you completed the MB-Tester installation, please read the Installation Guide.
Step 1: Create Visual Studio Project
Type of project: Python Application
Step 2: Select Python Environment
Each installed python environments has its own packages, so it is important to use the python environment for your project, where MB-Tester has been installed.
Checking The Actual Environment
If you do not see the MBTester package at the used environment, you have to check your installation or you have to select the correct python environment. If you followed the Installation Guide, you will see the following picture:
Select The Python Environment
Use the “Add Environment” button in “Python Environments” (sub) window or use the “Add Environment…” item from the right click menu of the project in “Solution Explorer”.
Choose the correct environment from the existing environments
Check that the MBTester package is available.
Write The First Script
We will use the GetSeriesNo MP command as an example command because it is working with all MP devices, so the type of the device is not relevant for this new script.
The python development tool can help during the script writing with quick info generated by Visual Studio IntelliSense.
Note: When using Visual Studio 2019 the quick info text is shown truncated if is too long. If you use Visual Studio Code it shows long quick info as scrollable text.
A few examples for the quick info:
Now write the following simple script. You may need to change the COM port to match your system. See the comments below for an explanation.
from mb_tester.MBTester import MBTester, LogLevels;
from mb_tester.MPBus import MPBus, CommunicationTypes;
from mb_tester.MPSerialPort import MPSerialPort, MPBaudRates;
# Create the MBTester object that will conduct the test
tester = MBTester( aDefaultLogLevel = [ LogLevels.ERROR
, LogLevels.WARNING
, LogLevels.INFO
, LogLevels.INPUT
, LogLevels.OUTPUT ] );
# Create a serial port for the MP Bus to use
mpserial = MPSerialPort( aTester = tester
, aName = "MPPort1"
, aDeviceId = "COM25"
, aCommunicationType = CommunicationTypes.MP3
, aBaudRate = MPBaudRates.B1200
, aIsDefault = True);
# Create the MP Bus object that implements the MP stack to send
# MP commands and receive the answers.
mpbus = MPBus(tester, aDefaultPort = mpserial);
# Execute the MP Command. The call will return after the answer is
# received. The Answer bytes are returned in the buffer.
(result, buffer) = mpbus.GetSeriesNo();
if result == 0:
tester.LogBytes(LogLevels.INFO, "Received data : ", buffer);
else:
tester.Log(LogLevels.ERROR, "Could not get series no. Error code: (" + str(result) + ")");
Now you can run your script, you should see an output similar to the following.
Epilogue
Now, you are done with your first script. We hope, it was easy.