Properties of Communication Port Control
The Comm component is added to a form whenever serial communications are required. By default, the first created object is named MSComm1 (the second is named MSComm2, and so on). It can be seen that the main properties of the object are: CommPort, DTREnable, EOFEnable, Handshaking, InBufferSize, Index, InputLen, InputMode, Left, Name, NullDiscard, OutBufferSize, ParityReplace, RThreshold, RTSEnable, Settings, SThreshold, Tag and Top.Settings
The Settings property sets and returns the RS-232 parameters, such as baud rate, parity, the number of data bit, and the number of stop bits. Its syntax is:
[form.]MSComm.Settings = setStr[$]where the strStr is a string which contains the RS-232 settings. This string takes the form:
"BBBB,P,D,S"
where
BBBBdefines the baud rate,
P the parity,
D the number of data bits, and
S the number of stop bits.
The following lists the valid baud rates (default is 9600Baud):
110, 300, 600, 1200, 2400, 9600, 14400, 19200, 38400, 56000, 128000, 256000.
The valid parity values are (default is N): E (Even), M (Mark), N (None), O (Odd), S (Space).
The valid data bit values are (default is 8): 4, 5, 6, 7 or 8.
The valid stop bit values are (default is 1). 1, 1.5 or 2.
An example of setting a control port to 4800Baud, even parity, 7 data bits and 1 stop bit is:
Com1.Settings = "4800,E,7,1"CommPort
The CommPort property sets and returns the communication port number. Its syntax is:
[form.]MSComm.CommPort = portNumber[%]which defines the portNumber from a value between 1 and 99. A value of 68 is returnedif the port does not exist.
PortOpen
The PortOpen property sets and returns the state of the communications port. Its syntax is:
[form.]MSComm.PortOpen = [{True | False}]A True setting opens the port, while a False closes the port and clears the receive andtransmit buffers (this automatically happens when an application is closed).
The following example opens communications port number 1 (COM1:) at 4800 Baud with even parity, 7 data bits and 1 stop bit:
Inputting data
The three main properties used to read data from the receive buffer are Input, InBuffer Count and InBufferSize.
Input
The Input property returns and removes a string of characters from the receive buffer. Its
syntax is:
[form.]MSComm.InputTo determine the number of characters in the buffer the InBufferCount property is tested (to be covered in the next section). Setting InputLen to 0 causes the Input property to read the entire contents of the receive buffer.
InBufferSize
The InBufferSize property sets and returns the maximum number of characters that can be received in the receive buffer (by default it is 1024 bytes). Its syntax is:
[form.]MSCommInBufferSize = [numBytes%]The size of the buffer should be set so that it can store the maximum number of characters that will be received before the application program can read them from the buffer.
The InBufferCount property returns the number of characters in the receive buffer. It can also be used to clear the buffer by setting the number of characters to 0. Its syntax is:
[form.]MSCommInBufferCount= [count%]Outputting data
The three main properties used to write data to the transmit buffer are Output, OutBufferCount and OutBufferSize.
The Output property writes a string of characters to the transmit buffer. Its syntax is:
[form.]MSComm. output= [outString$]OutBufferSize
The OutBufferSize property sets and returns the number of characters in the transmit buffer (default size is 512 characters). Its syntax is:
[form.]MSCommOutBuffer size = [NumBytes%]OutBufferCount
The OutBufferCount property returns the number of characters in the transmit buffer.The transmit buffer can also be cleared by setting it to 0. Its syntax is:
[form.]MSCommOutBufferCount. = [0]