|
-
Aug 14th, 2005, 03:29 PM
#12
Thread Starter
Need-a-life Member
Re: Serial Port
 Originally Posted by David.Poundall
OK - just a couple of points. I am referring to a comprehensive PLC comms routine that I have been successfully running for the past 5 years. But as you may imagine it was written five years ago. Anyway - enough of my problem and on to yours.
Here are a few snippets of my code that works. Unlike yours it works in binary mode though.
My receive code just unloads the Rxbuff array. The data received event fires when I receive the correct number of incoming bytes as PLC comms is quite predictable.
VB Code:
' ---------------------------------------------------------------------
' 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
Something in that lot may jog your thinking.
Yes, the key was the "RThreshold" and I had found it (almost at the same time you posted it) at http://www.programmers-corner.com/sourcecode/111.
VB Code:
'enable the oncomm event for every reveived character
.RThreshold = 1
Emiliano F. Martín
If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
Encourage the person who helped you to keep doing it, and give him the points he deserves.
MP3 Organizer: Freeware to logically organize all your MP3s.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|