![]() ![]() |
|
|||||
Email this Article | Printer Friendly Page |
||||||
|
How to
Communicate Via a COM Port with Web Pages
I was asked some time ago by a reader to give a more detailed
example of how to use COM ports in web pages. He had read my
article
on how to communicate via a COM port with web pages. His task
was the testing of modems to see whether they could send and
receive information and how to log the data response.
Javascript and ActiveX objects are easy to add to your web page and dont require any tools fancier than a simple text editor. However, the use of these tools can be greatly facilitated with additional tools such as the Microsoft Development Environment that comes with Visual Studio. Visual J++ is another great tool. If you are cheap (like me) then you might just try your hand first with Notepad or any other favorite text editor. Not an authority on modems, I created a small page that would query the modem attached to the COM port for some simple information and log that information to a file. Copy this file onto your computer to try it out.
I have only tested this page with computers running Windows 2000 and 98 and Internet Explorer 5.01. If the program isnt working for you, check to make sure that you have the MSComm ActiveX object registered on your computer. If its still not working and youre ready to pull your hair out, drop me an e-mail and Ill try to help. The property about the MSComm ActiveX object that seems to trip me up most often is the Input Mode property. If it is set to 0 (text) then the information is retrieved from the port as a string. If it is set to 1 (binary data) then the information is structured as a byte array. If you are just dealing with ASCII characters and dont care much about possible appending returns and line feeds, then leave it at 0. Ive left the file simple so you dont have to wade through a bunch of bells and whistles to figure out what is going on. When the file is first loaded you will probably get a prompt like this:
Select yes or the web page wont work. You are getting this prompt because the ActiveX MSComm object is embedded in the page. When the file opens, it will create the file "C:\testfile.txt" on your hard-drive. Any text that is received from the COM Port while this page is open and the file has not been closed will be recorded into this file. The file object is set up to overwrite any existing files. Here is a quick shot of what the page looks like:
The COM Port Number should be set to the port that your modem resides on. Or if you are using this file to check your own design, then set it to the appropriate port number. If you are using ports greater than 4, then you can edit that object on lines 71 74 to match the desired port number. Once you have opened the port you can type a line in the left-hand text area. This is the SendWindow. When you press the SendButton, the text in the SendWindow is sent to your respective COM Port. I have set the COM Port up to be 9600 baud, 8 bits of data, 1 stop bit, no parity. Any text that is received from the COM Port is echoed in the right-hand text area (ReceiveWindow). This information is also written to the file that was opened when the page was first initialized. If you want to stop recording to the file, you can press the Close File button, which will close the file and prevent any further writing to that file. If you refresh the page, then the file is reinitialized and the old information is gone. You can edit the file as an ordinary text document. If you are sending an AT type command to your modem be sure to follow the command with a return so that the command is recognized by the modem. If you want to send several commands in series, you will most likely need to add some timing to your web page to allow the modem time to respond to your first command before proceeding to any additional commands. Here is an example of what my computer looks like after sending the command ATI5:
The modems response has been logged to a file on my hard drive. Pretty cool! Theres not that much to it, but it can be a very powerful tool. E-mail me if you would like some more in-depth articles in this area.
|
||||||
|
Copyright ©2001 eChips About ChipCenter
|
||||||