Generate a Serial Stream using Logic Source with MBScript

The very first step to do when making any script is to look at the list of available commands. Open the Five Wire main system window, select 'Help' > 'Open MBScript Reference'. Scroll down to 'Reserved Words' section.

Reserved Words section

One of the main ones to use would be mbCommand() function, which sends commands to the Five Wire utility window itself. However, there are wrapper libraries in place which can make it easier for us to type these commands. Five Wire creates a directory in the 'Documents' folder where it places its libraries in. The full path is:

"C:\<user>\Documents\Anewin\FiveWire\userScripts"

This directory contains some example scripts as well as library scripts which we want to use to make our lives easier. You can open each with a simple text editor, such as Notepad++ and look through what each '-Lib.mbs' file has to offer. Each library contains simple comments explaining what each function does and how to use it. To make '.mbs' scripts easier to read in Notepad++, you can select 'Language' > 'C' > 'C' to color the text accordingly to the C programming language. Alternatively, you can use a custom made language interpreter file included in the 'userScripts' folder specifically for MBScript: Select 'Language' > 'Define your language...' > 'Import' > 'NoptepadPP-syntaxHightlight.xml', then close the 'User Defined Language' window and select it under the 'Language' list.

Create a new text file in the directory and name it 'LS_SerialStream.mbs'. The 'LS_' prefix makes it easier to see that it's a script specifically for the Logic Source module, however, it is important to note that each script has access to each module at the same time.

The first thing we do in the script is to add the libraries which we will use. 'stdLib.mbs' is a generic command wrapper for all of the modules, and 'lsLib.mbs' is a comprehensive command wrapper specifically for the Logic Source module.

Including libraries

The 'addLibrary' command expects the full file directory string right after it, however, it can be shortened with the 'userScriptDirectory' string, which contains the full directory path up to the 'userScripts' folder. Using this string at the beginning, simply add the remaining path string right after it.

Let's establish how serial transmission works and which variables we need to set. Looking at this page, we will need to know the baud rate, as well as the string of characters which we want to send. Let's set these variables:

Setting the first variables

There are only two types of variables in MBScript: String and Number. The number can be a float or an integer. To force a float to round down to an integer, use 'toInt()' function. To display a window and ask the user to type in a string, we use 'readInput()' function:

Ask the user for text, baud and channel

Next, let's open the Logic Source window and set it to single mode.

Open the Logic Source window

Now, let's disassemble the string to an array of characters, and then to an array of integers representing these characters.

Convert string to array of characters

Note that each variable must be created in the scope it is used. Since we will be using the 'charIntArray' outside of this 'for' loop, we create it by setting the first element of 'charIntArray' right before the 'for' loop. 

As we analyzed earlier, we will be sending 12 bits of data for each character: Idle bit, Start bit, 8bit character, Stop bit, Idle bit.

RS232 transmission

For this, we will have to deconstruct each number in 'charIntArray' into individual bits. Each line in the Logic Source pattern editor is called a vector. We will use the baud rate to set the duration of each vector and the deconstructed bits to set the channel 0 on and off.

The full script can be found here: https://pastebin.com/nTuhZczz. Here's the rest of the script:

The rest of the script

Once you run the script, you will get three prompt windows asking to input text, channel and baud frequency:

Enter TextEnter ChannelEnter Baud

Once the prompts receive proper inputs, the script will generate a vector pattern in the Logic Source pattern editor:

Resulting Pattern

If we loop-back the logic source to the logic analyser, we can use an RS232 decoder to verify the resulting waveform:

Resulting Waveform