Visual Basic Communications Demonstration Program: COMMDEMO The Visual Basic COMMDEMO program was designed to demonstrate how to call the communications related Windows API functions to establish basic serial communications. The COMMDEMO sample program is an example of a simple terminal emulation program. This program was designed mainly for educational purposes and therefore is not an example of a full featured terminal emulation program. The concepts presented in this sample program should provide a solid foundation from which you can implement serial communications into your own Visual Basic application. The sample program contains comments where needed indicating other possible values for variable settings to allow you to easily modify the sample program. For example you could modify the sample program to open any communications port; or change the line and event settings to your own custom settings. The COMMDEMO sample program demonstrates the following: o How to open and connect to the communications port o How to read from the communications port o How to write to the communications port o How to change the port settings such as the baud rate o How to change the event settings such as detecting when a character is received. o How to trap and process errors after calling the communications related API functions. Cross-Reference of the Main Features --------------------------------------------------------------------------- The following list provides references to the main functional areas of the program. The following reference should make it easier for you to find a desired feature related to communications. All of the procedures listed below are contained in the main form called COMMDEMO.FRM. o How to open and connect to the communications port: The code to initialize the communications port line settings is located in the Form_Load event procedure. The communications port line settings are initialized by calling the Windows API function BuildCommDCB. The code to open the communications port is located in function CommConnect. Function CommConnect calls the functions SetLineSettings and SetEventSettings which demonstrate how to set the communications line (e.g., baud rate, stop bits, hardware handshaking) and event (e.g., ring detect) settings respectively. The function CommConnect calls the Windows API function OpenComm to open COM1. The function CommConnect contains a call to the Windows API function SetCommState to set the communications line settings. The function SetEventSettings contains a call to the windows API function SetCommEventMask to enable communications event trapping. Function CommConnect is called from the MNU_Connect_Click event procedure for the Connect! menu. o How to read from the communications port The code that reads in all characters waiting in the communications receive buffer is located in the Timer_CheckInBuf_Timer event procedure. If a successful connection is made, the timer is initiated from the CommConnect function . The timer is disabled in the CommDisconnect function. The Timer_CheckInBuf_Timer event procedure contains a call to the Windows API function ReadComm to read the characters waiting in the receive buffer. o How to write to the communications port The Text_TransmitWin_KeyPress event procedure contains the code which writes characters to the communications port as they are entered in the transmit window. This event procedure contains a call to the Windows API function WriteComm to write characters to the transmit buffer. o How to change the port settings such as the baud rate. The MNU_BaudRate_Click event procedure for the Baud menu demonstrates how to change the baud rate. The baud rate can be changed while the communications port is opened or closed. COMMDEMO demonstrates how to change between baud rates of 1200, 4800 and 9600. The MNU_BaudRate_Click event procedure demonstrates how to call the Windows API function SetCommState to change the baud rate line setting. o How to change the event settings such as detecting when a character is received. The MNU_Events_CharDetect_Click event procedure for the Events menu demonstrates how to enable or disable character received events. When character detect event trapping is enabled, a message is displayed in the Communications Status window each time a character is received. The character detect event trapping can only be enabled or disabled when the communications port is opened. The MNU_Events_CharDetect_Click event procedure contains a call to the Windows API function SetCommEventMask to change the character detect event setting. o How to trap and process errors after calling the communications related API functions. The function GetOpenCommError demonstrates how to process errors related to opening the communications port. This function simply interprets the return code from the Windows API function OpenComm. The GetOpenCommError function is called from the CommConnect function. The function ProcessCommError demonstrates how to call the Windows API function GetCommError to process general communications errors. This function is called from the procedures Timer_CheckInBuf_Timer and Text_TransmitWin_KeyPress when reading from and writing to the communications port respectively. Cross-Reference to the Windows API functions called by COMMDEMO --------------------------------------------------------------------------- Below is a reference to the location(s) in the code where the communications related Windows API functions are called. All calls to the Windows API functions are located in the main form called COMMDEMO.FRM. The declaration for the Windows API functions and related Type ... End Type structures is located in the global module called COMMDEMO.GLB. Windows API function Location in COMMDEMO.FRM -------------------- -------------------------------------------- BuildCommDCB...............Form_Load event procedure OpenComm...................CommConnect CloseComm..................CommConnect ..................CommDisconnect FlushComm..................CommDisconnect ReadComm...................Timer_CheckInBuf_Timer event procedure WriteComm..................Text_TransmitWin_KeyPress event procedure SetCommState...............SetLineSettings ...............MNU_BaudRate_Click event procedure SetCommEventMask...........SetEventSettings ...........MNU_Events_CharDetect_Click event procedure GetCommError...............ProcessCommError ...............Timer_CheckInBuf_Timer event procedure