My ChipCenter Knowledge Center Guides & Experts Product Reviews Ask Us Online Tools Circuit Cellar Resources Webcasts Careers


Data Sheets App Notes Ask Us
Search Type:
Search for:






Email this Article   |  Printer Friendly Page

COM Port Programming
by Robert Ashby

I wrote an article a while ago on communications via RS-232. It has been a simple method of serial communication that has had widespread application. Even with the popularity of the TCP/IP and the quick growth of USB, RS-232 still remains a simple quick link for devices without much overhead or excess hardware. I hope that by now you have tried a project using RS-232.

My article before showed you the mechanics of how the transfer occurs. I figured it would be good to give you a way to show off your RS-232 compatible device. COM port programming can be found in nearly every flavor of computer programming that I know about. However, you don't need to spend a lot of time or expense buying a programming software package (Though, you might want to hide this fact from your boss). You can build a neat little application that looks great quickly, and easily.

The secret is to use Internet Explorer. It's present on most Windows-based computers and allows you to present a plethora of neat visual content to the user with very little development. People get bored with the gray buttons and simple square boxes that make up a fair part of Windows usage. Internet Explorer will let you create the square boxes and simple buttons if that is your desire. It's also built to easily create colorful and interesting interfaces with a combination of pictures and media content.

Internet Explorer also will let you embed any Active-X component within a page. It shouldn't be a surprise that a Microsoft COMM control is a standard Active-X component available for use. The component needs to be properly registered on your machine for it to work correctly. You will also need the necessary .dll files required by the Active-X component to function properly. If you don't have everything set-up right, then your web page won't run the COM port as expected.

I won't spend any time talking about web page building. There are hundreds of sites on the web to help you out with that. Go to some that will give you a little help on javascript also. You can use VBscript to control your COM port object, but it will require a few more .dll files.

Add the following to the body of your page and you will have a COM port at your disposal.

<OBJECT classid=clsid:648A5600-2C6E-101B-82B6-000000000014 id=MSComm1>
<PARAM NAME="_ExtentX" VALUE="1005">
<PARAM NAME="_ExtentY" VALUE="1005">
<PARAM NAME="_Version" VALUE="393216">
<PARAM NAME="CommPort" VALUE="1">
<PARAM NAME="DTREnable" VALUE="1">
<PARAM NAME="Handshaking" VALUE="0">
<PARAM NAME="InBufferSize" VALUE="1024">
<PARAM NAME="InputLen" VALUE="0">
<PARAM NAME="NullDiscard" VALUE="0">
<PARAM NAME="OutBufferSize" VALUE="512">
<PARAM NAME="ParityReplace" VALUE="63">
<PARAM NAME="RThreshold" VALUE="0">
<PARAM NAME="RTSEnable" VALUE="0">
<PARAM NAME="BaudRate" VALUE="9600">
<PARAM NAME="ParitySetting" VALUE="0">
<PARAM NAME="DataBits" VALUE="8">
<PARAM NAME="StopBits" VALUE="0">
<PARAM NAME="SThreshold" VALUE="0">
<PARAM NAME="EOFEnable" VALUE="0">
<PARAM NAME="InputMode" VALUE="0">
</OBJECT>

Most of the parameters should be self-explanatory. I would recommend that you review the datasheet for the 16550 uart chip for a refresher if needed at National's website. It would also pay to search around Microsoft's website for information on the Microsoft Communications Control.

Javascript will give you an easy way to control the characteristics of the COM port control giving you quick access to the methods and properties of the COM port, e.g. MSCOMM1.PortOpen = true. Input and output of the COM port is handled as strings.

Here are a few little interesting tidbits of information that I would have like handed to me in the beginning.

The default mode of the COM control is text mode. That means that the control will add on return and line feed bytes that your embedded system might not be expecting. The extra characters can be avoided by setting the InputMode property to "1" which is binary mode. You should also be aware that NullDiscard will prevent the reception of null characters if set to "true."

If you need to send nonprintable characters to the output, you can use the escape character, i.e. "\x41" is the same as "A." This will allow you to specify any character from 0x00 to 0xFF. The input might come back as nonprintable characters also. The easiest way that I have found to seperate the value of the characters is to use the method charCodeAt(n). It returns the unicode value of character at index 'n' within a string.

The best teacher has always been example, so I have included an example of making a COM Port work. Make sure that the Active-X object is properly registered and that the security settings in Internet Explorer are set to allow Active-X objects to run.


<html>
<head>
<title>COM Port Communications</title>
<SCRIPT ID=clientEventHandlersJS LANGUAGE=javascript>
<!--
function ComPortButton_onclick(){
if(MSComm1.PortOpen==false){
  MSComm1.PortOpen=true;
  ComPortButton.value="Close";
}
else{
  MSComm1.PortOpen=false;
  ComPortButton.value="Open";
  }
}

function SendButton_onclick() {
if(MSComm1.PortOpen==true){
  MSComm1.Output="Here is an ASCII character"+"\x41";
  }
else{
  alert("You need to open the COM port first");
  }
}

function MSComm1_OnComm() {
var InputString;
var Number;
InputString=MSComm1.Input;
number = InputString.charCodeAt(0);
alert(Number);
}

//-->
</SCRIPT>
<SCRIPT LANGUAGE=javascript FOR=MSComm1 EVENT=OnComm>
<!--
MSComm1_OnComm()
//-->
</SCRIPT>
</headv>
<body>
<P>This web page communicates over a COM port.</P>
<P>
<OBJECT classid=clsid:648A5600-2C6E-101B-82B6-000000000014
id=MSComm1>
<PARAM NAME="_ExtentX" VALUE="1005">
<PARAM NAME="_ExtentY" VALUE="1005">
<PARAM NAME="_Version" VALUE="393216">
<PARAM NAME="CommPort" VALUE="1">
<PARAM NAME="DTREnable" VALUE="-1">
<PARAM NAME="Handshaking" VALUE="0">
<PARAM NAME="InBufferSize" VALUE="1024">
<PARAM NAME="InputLen" VALUE="0">
<PARAM NAME="NullDiscard" VALUE="0">
<PARAM NAME="OutBufferSize" VALUE="512">
<PARAM NAME="ParityReplace" VALUE="63">
<PARAM NAME="RThreshold" VALUE="0">
<PARAM NAME="RTSEnable" VALUE="0">
<PARAM NAME="BaudRate" VALUE="9600">
<PARAM NAME="ParitySetting" VALUE="0">
<PARAM NAME="DataBits" VALUE="8">
<PARAM NAME="StopBits" VALUE="0">
<PARAM NAME="SThreshold" VALUE="0">
<PARAM NAME="EOFEnable" VALUE="0">
<PARAM NAME="InputMode" VALUE="0">
</OBJECT>
</P>
<P>
<INPUT id=ComPortButton name=ComPortButton type=button value=Open LANGUAGE=javascript onclick="return ComPortButton_onclick()" style="LEFT: 340px; TOP: 33px"> <INPUT id=SendButton lowsrc="" name=SendButton style="LEFT: 68px; TOP: 110px" type=button value="Send Message" LANGUAGE=javascript onclick="return SendButton_onclick()"> </P>
</body>
</html>

It's really not that difficult even for the meekest of programmers. It's easy to add to the page to get some flashy content that will really add some shine to your project. It is even fast enough that you could make a side derivation for the kids at home or even your significant other if it's someone a lot more "techy" than my wife.

 

Embedded Engineering

Guides and Experts   Analog Avenue   EDA Tools   PLD   DSP   EDA   Embedded Systems   Power   Test


Email this Article   |  Printer Friendly Page

Copyright ©2001 eChips
About ChipCenter  Contact Us  Privacy Statement   Advertising Information