Simple VB source code for Receive and Transmit data to RS232.
At Form1
Dim startfra As Byte
Dim header As Byte
Dim checksum As Integer
Dim outvalue As String
Dim invalue As String
Dim InByte As Byte
Dim OutByte As Byte
Dim instring As String
Dim buff As Byte
Dim i As Byte
Dim tcount As Integer
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Public Sub Pause(milli As Long)
Sleep (milli)
End Sub
Private Sub Form_Load()
tcount = 0
header = &H0 'Define Output
MSComm1.Settings = "19200,N,8,1"
MSComm1.InputLen = 1
MSComm1.RTSEnable = True
MSComm1.CommPort = 1
MSComm1.PortOpen = True
startfra = &H55
OutByte = &H0
checksum = checksumOut(header, OutByte)
MSComm1.Output = Chr(startfra) & Chr(header) & Chr(OutByte) & Chr(InByte) & ChrW(checksum) 'Output to RS232
Timer1.Interval = 1
End Sub
Private Sub Form_Unload(Cancel As Integer)
OutByte = &H0
checksum = checksumOut(header, OutByte)
MSComm1.Output = Chr(startfra) & Chr(header) & Chr(OutByte) & Chr(InByte) & ChrW(checksum) 'Output to RS232
If MSComm1.PortOpen = True Then
MSComm1.PortOpen = False
End If
End
End Sub
Private Sub MSComm1_OnComm()
instring = ""
For i = 0 To 4
If MSComm1.CommEvent = 2 Then
instring = MSComm1.Input
If MSComm1.InBufferCount = 2 Then
buff = Asc(instring)
Select Case (buff)
Case 7
OutByte = &H1
checksum = checksumOut(header, OutByte)
MSComm1.Output = Chr(startfra) & Chr(header) & Chr(OutByte) & Chr(InByte) & ChrW(checksum) 'Output to RS232
Case 11
OutByte = &H2
checksum = checksumOut(header, OutByte)
MSComm1.Output = Chr(startfra) & Chr(header) & Chr(OutByte) & Chr(InByte) & ChrW(checksum) 'Output to RS232
Case 13
OutByte = &H4
checksum = checksumOut(header, OutByte)
MSComm1.Output = Chr(startfra) & Chr(header) & Chr(OutByte) & Chr(InByte) & ChrW(checksum) 'Output to RS232
Case 14
OutByte = &H8
checksum = checksumOut(header, OutByte)
MSComm1.Output = Chr(startfra) & Chr(header) & Chr(OutByte) & Chr(InByte) & ChrW(checksum) 'Output to RS232
Case 15
OutByte = &H0
checksum = checksumOut(header, OutByte)
If tcount = 2 Then
MSComm1.Output = Chr(startfra) & Chr(header) & Chr(OutByte) & Chr(InByte) & ChrW(checksum) 'Output to RS232
End If
End Select
End If
End If
Next
End Sub
Private Sub Timer1_Timer()
MSComm1.Output = Chr(setval1) & Chr(setval2) & Chr(setval3) & Chr(setval4) & Chr(setval5) End Sub
-----------------------
At Module
Public Enum packet
setval1 = &H55
setval2 = &H80
setval3 = &H0
setval4 = &HF
setval5 = &H71
End Enum
Public Function checksumOut(header As Byte, OutB As Byte) As Integer
If OutB = 0 Then checksumOut = &H0 Else checksumOut = &H100 - ((header + OutB) And &HFF)
End Function
Tuesday, 26 June 2007
Subscribe to:
Post Comments (Atom)
How you define from data hyperterminal :120018617E15, and i want to get this data to text1.text in visual basic.how you define this program?is it the as above?
ReplyDelete