Simple to use COM's

There are many options for controlling an I2C device form a PC using the BV4221 but probably the easiest way is from a script, a simple text file of commands. This works very well for displays and read only devices.

Resources: i2com - exe file and C source code can be found in the downloads section

NOTE for bVT100 devices there is an equivalent program in www.asi.byvac.com

What is It

i2com is a small program written in C that will input a script file and output to the specified COM port. It has been kept as simple as possible so that C programmers can modify the content. The most difficult part of this program was finding a suitable method of outputting to the COM port without resorting to complex or commercial DLL's.

The solution was in fact on Microsoft's own MSN site but none the less it was very difficult to track down and very little sensible articles exist on writing to COM ports via Windows. This program has been tested on Winnows XP.

Using

If all you want to do is use the program then unzip the i2com file and take out the 'exe' file. Connect up a BV4221 and create a script that matches your com port. There may be a problem with higher numbered COM ports, I had a COM403 which didn't work so I do not know where the limit is (probably 5).

The example script file works when connected to a 2 line LCD display and the BV4221 is on COM2. The program slightly enhances the BV4221 by ignoring comments and expanding text inside quotes. A script file with "ABC" for example will be expanded to 41 42 43 and that text will be sent to the COM port.

The program does not return any values other than 0 on success which can be monitored in a batch file. There is a facility within the program to return values and these could be used somehow, perhaps placed into an environment varaible? System time and other system attributes could easily be displayed if required, even contents of files, the possibilities are quite wide.

Step by Step

1) extract the zip file into a suitable directory

2) open a command box:

run popup

3) Navigate to where the script and exe are.

4) type i2com i2scr1.txt

This assumes that you want to run the script file provided.

Alternative

Another method would be to create a batch or command file:

1) extract the zip file into a suitable directory

2) create a new text file in the same directory, edit it so that it has just " i2com i2scr1.txt" (without the quotes)

screen shot of batch file

3) rename the text file so that it ends in '.bat' or better '.cmd'. You should have a file that is named something like 'test.cmd'

4) double click on the file.

How it Works

As previously mentioned the most difficult part was tracking down a sensible way to use the COM ports within Windows. Although the method is quite straight forward it is not very public knowledge. Basically once the keyword 'CreateFile' can be associated with COM ports the rest is searchable on the net. It is discovering this in the first place. The relevant information about create file is here:

http://msdn.microsoft.com/en-us/library/aa363858%28VS.85%29.aspx

The COM port is opened using a copy (almost) of the function provided in MSN, this is called by an initialise routine that simply sends CR to the BV4221 to establish the Baud rate. There are only two COM functions used, PUTS (put string) and GETS (get string). As the program does not return any values you may be thinking why have a GETS, actually called com_gets? This is because the BV4221 does not have a hardware handshake and so it must be implemented by software.

The purpose of a handshake is to tell 'i2com.exe' when to send the next line of data, if it is sent before the BV4221 is ready then the BV4221 will ignore it and data will be missed. The software handshake relies on the fact that the BV4221 always sends a prompt (e.g. 0x42>) after each command and the last thing it sends is '>'. The purpose of com_gets is to look for the '>' and return when it is found. A delay may required after the '>' and this can be adjusted in the script. This delay is not for the BV4221 but for the device it is connected to. An improvement here could be incorporate the delay into the script at various points so that it could be adjusted.

The two main text parsers are get_next and send_com_text. The function 'get_next' will get a line of text from the script ignoring any lines with '#' and only returning the left hand side of '#' if one exists, effectively removing comments from the input. It will also look for ';' and treat this as a new line.

The COM port, Baud rate and Delay are obtained using this function to establish a handle to the COM port via bvt_init. The rest of the script is handled by 'send_com_text'. The main purpose of this function is to expand characters in double quotes and wait for the handshake.