' ---------------------------------------------------------------------
' Globals
Public Rxbuff() As Byte
' ---------------------------------------------------------------------
' Code behind the Form containing the comm control
Private Sub MSComm1_OnComm()
Select Case MSComm1.CommEvent
Case comEvReceive ' Received RThreshold # of chars.
Rxbuff() = MSComm1.Input
Call HandleReceivedData
End Select
End Sub
' ---------------------------------------------------------------------
' called at comms kickoff
Sub InitialiseComms()
frmPlcComms.MSComm1.CommPort = ComPort
frmPlcComms.MSComm1.Settings = "9600,E,7,1"
frmPlcComms.MSComm1.PortOpen = True
frmPlcComms.MSComm1.DTREnable = True
frmPlcComms.MSComm1.RTSEnable = True
frmPlcComms.MSComm1.InputMode = comInputModeBinary
frmPlcComms.MSComm1.InputLen = 0 'Forces entire buffer read
End Sub
' ---------------------------------------------------------------------
' send code
Sub Transmit()
Dim txbuff() As Byte, k as long
Dim SendBuffer(1) As Variant ' VIP for sending Binary
frmPlcComms.MSComm1.InBufferCount = 0 'Flush the RX Buffer
' Dim the TX buffer
redim txbuff(0 to 100)
k = 0
' Send pre-amble
txbuff(k) = &H2 : k = k+1
txbuff(k) = &H31 : k = k+1
txbuff(k) = &H31 : k = k+1
' Add further text to the byte buffer as byte...
' Trim the TXbuffer
ReDim txbuff(0 to k-1)
SendBuffer(1) = txbuff()
frmPlcComms.MSComm1.Output = SendBuffer(1)
' Set the RX buffer length to the value expected from the PLC
frmPlcComms.MSComm1.RThreshold = 1 ' Note - Not typical.
End Sub