|
There are a lot of third party tools that perform the same function as Microsofts MSComm ActiveX Control. One of the reasons for this is the difficulty to get working and lack of documentation that most people experience when they attempt to use the control.
In my experience with MSComm I have developed a few simple proceedures for successfully using the control. I have never needed a third party tool, although I’m sure there are some that offer additional features and are better from a performance point of view.
Here is a sample function that works very well for me. Read through the function and then I’ll explain each of the lines below.
Setup port function
Private Function SetupPort(ByRef objCom As MSComm) With objCom .Break = False .DTREnable = True .EOFEnable = False .Handshaking = comNone .InBufferCount = 0 .InBufferSize = MAX_PACKETSIZE .InputLen = MAX_PACKETSIZE .InputMode = comInputModeText .NullDiscard = False .OutBufferCount = 0 .OutBufferSize = MAX_PACKETSIZE .ParityReplace = Chr(0) .RThreshold = 1 .SThreshold = 0 .Settings = "115200,N,8,1" 'default settings End With End Function
Function description
1. Line one is the function declaration, you will notice that I have used the ByRef option in the function argument list, this tells the function that we want to work with MSComm object passed to the function and not a new instance of the object. 2. Working with the passed object reference, for you C programmers think of the ByRef objCom as a pointer to the single instance of the object. 3. In some cases (old style communications protocols) we may want to communicate a pause or break in communications. This is rarely used due to it’s simplicity. So in this instance I’ve set the .Break property to false. 4. I’ve set the DTREnable to true. This indicates to any connected equipment that the terminal (your PC) is alive and well (and ready to recieve data so make sure you are). This actually causes a physical pin on the serial port to be driven to -12VDC for RS-232C. 5. .EOFEnable will cause the OnComm event to fire if the received character is an EOF character, this is a specially designated character in the ASCII table. I’ve disabled it because I have the recieve event OnComm fire when each character is received (not the best performance configuration but it works in this simple example and I’ve never had any issues). Also if you rely on the EOF character to terminate your packets and it gets corrupted then you may not see the end of the packet correctly. Also ASCII comms sucks, you want to be able to transfer binary data and ecapsulate it within packets that you know the structure of. If you do use EOF then you’ll be limited to 7bits per byte. 6. This is perhaps the most important setting, i have disable handshaking, because that’s how germs are spread, no seriously we want to use a simple three wire cable (GND, TX and RX) we don’t want the hassle of wiring in those other wires especially when our software communications protocol will handle the start and stop of communications, similar to the MAC level in ethernet. 7. This sounds ominous a buffer set to zero! but in fact all it means is that your clearing any characters sitting in the objects receive buffer, although I’m sure Microsoft would have coded this into the ActiveX controls initialisation :P 8. If you expect your PC to be under 100% load then it may be possible that Windows will miss a character at the serial port (although I’ve never seen it). In my configuration when a single character arrives the OnComm event fires and I retrieve the character. So the buffer theoretically never gets any fuller than 1 character, but to be safe I set the buffer to be slightly larger than the largest packet I expect to recieve. 9. I set the .InputLen to be the same as the .InBufferSize this is a personal preference that I can’t remember why I do it I just know it’s good. Maybe it’s a karma thing I dunno. Specifically it’s the number of characters that are attempted to be retrieved when you use the objects Input method to get all the recieved characters. No matter you’ll only get what characters are in the buffer, no more no less. 10. ComInputModeText works for me ComInputModeBinary doesn’t. If I use a hex editor program to look at the serial data all I’ve got is a bunch of 20h coming in. 11. Let us not throw away 00h (00000000b) data instead let us keep it so that our protocol survives and prospers in a land where nothing can be something and indeed is. 12. Use this to clear any character in the output buffer, as we arn’t using handshaking there should be no chars in the buffer but do you trust that your not sending “LAUNCH” to that nuclear submarine your controlling. 13. Make the output buffer as large as you biggest packet so when you want to send your nicely structured data packet you can just plonk it into the Output method. 14. Unless your using parity (FOR GODS SAKE DON’T USE IT) sorry uhm use CRC if your data integrity is important. Me I don’t care STOP/GO it’s all the same when controlling large machinery, things have to break sometime you may as well be there to fix em. Set this property to whatever you want. CHR(0) may do the least damage if you forget or somehow enable parity. 15. One is what you want here, now when a character is received you’ll know about it. Set it to zero if you want a full receive buffer and are planning on missing characters. 16. Zero is cool here we know when chars are sent cause we aren’t using handshaking so they skedaddle as soon as we output em. 17. This line is not really relevant unless your serial communications are relevant, duh it’s everything important in one line formatted as a string, what was Microsoft thinking? 18. Get a VB manual to understand this line 19. Like </BODY> in a web page, so are the ends of our functions.
Ok you got all that? How you’d use the function
MSComm1.CommPort = 1 ‘I am using COM 1 on my PC SetupPort MSComm1 ‘Now I have setup the port MSComm1.PortOpen = True ‘Now I have opened the port and can output
You should trap for errors immediately after the PortOpen method incase the port is opened by another application or does not exist, here is how I do it.
If Err.Number = 0 Then msgbox "Hey it’s open!" Else msgbox "Doh it’s NOT open!” End If
Now lets actually send a some data over the serial port
MSComm1.Output = "AT" & vbCrLf
Disclaimer
All the information on this page is provided for learning only, I’d expect anyone actually interfacing to equipment to ensure that they are acting in accordance with relevant Occupational Health and Safety regulations, and the recomendations of the equipment supplier/vender/manufacturer.
|