|
|
|||||||||
|
|||||||||
|
|||||||||
| |
||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
i have problem in converting one form code into a function and
multiple subroutines using the com1 port which my actual form code is as follows Option Explicit Private Sub cmdCall_Click() On Error GoTo trap Dim timeout As Variant Dim sende As String Dim sende2 As String Dim checksum As String Dim i MSCmm1.PortOpen = True 'Command1.Enabled = False sende = "" empfang = "" 'sende = Chr$(&H10) + Chr$(&H5B) + Chr$(&HFE) + Chr$(&H59) + Chr$(&H16) 'Direct communication for the CF-ECHO sende = Chr$(&H10) + Chr$(&H5B) + Chr$(&H1) + Chr$(&H5C) + Chr$(&H16) 'First external Communication attached to CF-ECHO MSCmm1.Output = sende timeout = Now() + 2 / 86400 Do DoEvents Loop Until Now() > timeout Print Len(empfang) frmTemp1.txtTemp1.Text = "" frmTemp2.txtTemp2.Text = "" For i = 1 To Len(empfang) frmTemp1.txtTemp1.Text = frmTemp1.txtTemp1.Text + " " + HexByte(Asc (Mid(empfang, i, 1))) frmTemp2.txtTemp2.Text = frmTemp2.txtTemp2.Text + " " + HexByte(Asc (Mid(empfang, i, 1))) Next frmTemp1.txtTempDisp1 = Val(HexByte(Asc(Mid(empfang, 51, 1))) + HexByte(Asc(Mid(empfang, 50, 1)))) / 10 frmTemp1.txtReturnTemp1 = Val(HexByte(Asc(Mid(empfang, 55, 1))) + HexByte(Asc(Mid(empfang, 54, 1)))) / 10 MSCmm1.PortOpen = False MSCmm1.PortOpen = True sende2 = "" empfang = "" sende2 = Chr$(&H10) + Chr$(&H5B) + Chr$(&H2) + Chr$(&H5D) + Chr$(&H16) 'Second external Communication attached to CF-ECHO MSCmm1.Output = sende2 timeout = Now() + 2 / 86400 Do DoEvents Loop Until Now() > timeout frmTemp2.txtTempDisp2 = Val(HexByte(Asc(Mid(empfang, 51, 1))) + HexByte(Asc(Mid(empfang, 50, 1)))) / 10 frmTemp2.txtReturnTemp2 = Val(HexByte(Asc(Mid(empfang, 55, 1))) + HexByte(Asc(Mid(empfang, 54, 1)))) / 10 frmTemp2.txtTemp2.Text = "" For i = 1 To Len(empfang) frmTemp2.txtTemp2.Text = frmTemp2.txtTemp2.Text + " " + HexByte(Asc (Mid(empfang, i, 1))) Next 'Print "Tv=", Val(HexByte(Asc(Mid(empfang, 51, 1))) + HexByte(Asc(Mid (empfang, 50, 1)))) / 10 'Print "Return Temparature =", Val(HexByte(Asc(Mid(empfang, 55, 1))) + HexByte(Asc(Mid(empfang, 54, 1)))) / 10 'Command1.Enabled = True MSCmm1.PortOpen = False trap: End Sub Private Sub cmdproperties_Click() ' Anzeigen des Formulars COM- ' Anschluß-Einstellungen. frmProperties.Show vbModal End Sub Private Sub cmdTemparature2_Click() On Error GoTo trap Dim timeout As Variant Dim sende As String Dim checksum As String Dim i MSCmm1.PortOpen = True cmdTemparature2.Enabled = False sende = "" empfang = "" sende = Chr$(&H10) + Chr$(&H5B) + Chr$(&H2) + Chr$(&H5D) + Chr$(&H16) 'Second external Communication attached to CF-ECHO MSCmm1.Output = sende timeout = Now() + 2 / 86400 Do DoEvents Loop Until Now() > timeout Print Len(empfang) Text1.Text = "" For i = 1 To Len(empfang) Text1.Text = Text1.Text + " " + HexByte(Asc(Mid(empfang, i, 1))) Next Print "Tv=", Val(HexByte(Asc(Mid(empfang, 51, 1))) + HexByte(Asc(Mid (empfang, 50, 1)))) / 10 Print "Return Temparature =", Val(HexByte(Asc(Mid(empfang, 55, 1))) + HexByte(Asc(Mid(empfang, 54, 1)))) / 10 cmdTemparature2.Enabled = True MSCmm1.PortOpen = False trap: End Sub Private Sub Command1_Click() On Error GoTo trap Dim timeout As Variant Dim sende As String Dim checksum As String Dim i MSCmm1.PortOpen = True Command1.Enabled = False sende = "" empfang = "" 'sende = Chr$(&H10) + Chr$(&H5B) + Chr$(&HFE) + Chr$(&H59) + Chr$(&H16) 'Direct communication for the CF-ECHO sende = Chr$(&H10) + Chr$(&H5B) + Chr$(&H1) + Chr$(&H5C) + Chr$(&H16) 'First external Communication attached to CF-ECHO MSCmm1.Output = sende timeout = Now() + 2 / 86400 Do DoEvents Loop Until Now() > timeout Print Len(empfang) Text1.Text = "" For i = 1 To Len(empfang) Text1.Text = Text1.Text + " " + HexByte(Asc(Mid(empfang, i, 1))) Next Print "Tv=", Val(HexByte(Asc(Mid(empfang, 51, 1))) + HexByte(Asc(Mid (empfang, 50, 1)))) / 10 Print "Return Temparature =", Val(HexByte(Asc(Mid(empfang, 55, 1))) + HexByte(Asc(Mid(empfang, 54, 1)))) / 10 Command1.Enabled = True MSCmm1.PortOpen = False trap: End Sub Private Sub Command2_Click() Dim checksum As String Text2.Text = Hex(AddByte(5, 81)) checksum = Mid(Text2.Text, 2, 2) Print checksum If checksum <> HexByte(Asc(Mid(empfang, 82, 1))) Then ' MsgBox "Do you want to continue checking temparature again ?", vbYesNo ' If vbYes Then Command1_Click ' Else ' cmdExit.SetFocus ' End If End If End Sub Private Sub Form_Load() Call ZuweisungVariablen MSCmm1.Settings = "9600, e, 8, 1" MSCmm1.CommPort = 1 End Sub Private Sub MSCmm1_OnComm() empfang = empfang + MSCmm1.Input End Sub Private Function HexByte(b As Byte) As String 'This function a byte converts into Hex representation Const HexChar = "0123456789ABCDEF" Dim upbit As Byte 'Upper Nibble Dim lowbit As Byte 'Lower Nibble upbit = b \ 16 lowbit = b Mod 16 HexByte = Mid(HexChar, upbit + 1, 1) + Mid(HexChar, lowbit + 1, 1) 'Hex for representation for byte produce End Function Private Function AddByte(b As Byte, c As Byte) As String 'This function for adding bytes from 5 to 81 to find 'out check sum of the received bytes from the device Dim i As Integer Dim byteN As Integer Dim byteSt As String Dim str As String byteN = 0 For i = b To c byteSt = HexByte(Asc(Mid(empfang, i, 1))) byteN = byteN + CDecimal(byteSt) Next i AddByte = byteN End Function Public Function CDecimal(ByVal HexWert As String) As Variant 'Konvertiert einen hexadezimalen Wert in einen dezimalen Wert Dim t As Integer Dim tt As Integer Dim pos As Integer HexWert = Trim(HexWert) t = InStr(HexWert, "%%") If t > 0 Then HexWert = Left(HexWert, t - 1) End If If HexWert = "" Then CDecimal = 0 Exit Function End If If Left(HexWert, 1) = "%" Then HexWert = Mid(HexWert, 2) End If HexWert = UCase(HexWert) CDecimal = 0 pos = 0 For t = Len(HexWert) To 1 Step -1 tt = Asc(Mid(HexWert, t, 1)) Select Case tt Case 48 To 57 ' 0-9 tt = tt - 48 Case 65 To 70 'A-F tt = tt - 55 Case Else Exit Function End Select CDecimal = CDecimal + ((16 ^ pos) * tt) pos = pos + 1 Next t End Function The above code is working form for calculation of temparature from a device attached to com1 port and another two objects are connected,which delivers the required temparature. Now my problem is i must do the following 1st step Empfang function writing 2nd step sende code for 1 to the empfang 3rd step data receiving for sende code1 from the empfang function 4th step sende code for 2 to the empfang 5th step data receiving for sende code2 from the emfang function 6th step sende code for either 1 or 2 to use the empfang function to receive 1 & 2 sende together For which i do wrote the code like the following but i could not able to receive the output as i required Option Explicit Dim sende As String Dim i Private Function empfang(str As String) Dim timeout As Variant ' empfang = "" str = "" For i = 1 To Len(str) str = str + " " + HexByte(Asc(Mid(empfang, i, 1))) Next timeout = Now() + 2 / 86400 Do DoEvents Loop Until Now() > timeout Print Len(str) 'timeout = Now() + 2 / 86400 ' Do ' DoEvents ' Loop Until Now() > timeout ' Print Len(str) empfang = str End Function Private Function HexByte(b As Byte) As String 'This function a byte converts into Hex representation Const HexChar = "0123456789ABCDEF" Dim upbit As Byte 'Upper Nibble Dim lowbit As Byte 'Lower Nibble upbit = b \ 16 lowbit = b Mod 16 HexByte = Mid(HexChar, upbit + 1, 1) + Mid(HexChar, lowbit + 1, 1) 'Hex for representation for byte produce End Function Private Sub Command1_Click() Dim checksum As String Dim s As String MSCmm1.PortOpen = True Command1.Enabled = False sende = "" s = "" 'sende = Chr$(&H10) + Chr$(&H5B) + Chr$(&HFE) + Chr$(&H59) + Chr$(&H16) 'Direct communication for the CF-ECHO sende = Chr$(&H10) + Chr$(&H5B) + Chr$(&H1) + Chr$(&H5C) + Chr$(&H16) 'First external Communication attached to CF-ECHO Call empfang(sende) 'MSCmm1.Output = sende ''s = Val(HexByte(Asc(Mid(s, 51, 1))) + HexByte(Asc(Mid(s, 50, 1)))) / 10 's = 33 'Print HexByte(Asc(Mid(s, 51, 1))) ''Print "Tv=", Val(HexByte(Asc(Mid(s, 51, 1))) + HexByte(Asc(Mid(s, 50, 1)))) / 10 'Print "Return Temparature =", Val(HexByte(Asc(Mid(s, 55, 1))) + HexByte(Asc(Mid(s, 54, 1)))) / 10 Command1.Enabled = True MSCmm1.PortOpen = False 'trap: End Sub Private Sub Form_Load() MSCmm1.Settings = "9600, e, 8, 1" MSCmm1.CommPort = 1 End Sub . I think i could able to explain my problem correctly, i am attaching my forms too frmsteuerelemente is the worked form frmGlobalempfang is the work i am trying to do globalize the previous form functionality. Please help me, Thanks in advance |
|
#2
|
|||
|
|||
|
Basic Problem
Hi The basic probelm that you have is on the On_Comm Event.
empfang = empfang + MSCmm1.Input in the On_Comm Event you really need to do a select case on the event. Since this get's triggered when you send, receive, port errors etc. So just placing the above with no test for the signal typew could give you incorrect data if the input buffer isn't completely empty if say a port error has occured or another send. Check for case evReceive. Also throughout your code you've got timers and doevents etc all to slow down your code waiting for empfang to fill. You should be testing the length in the On_Comm event and deal with it there and then continue. Look for length if that can be gaurenteed, but the device sending data must be sending a stopbit or somthing that you cna test for. Unless you code this way, you are leaving yourself open to data leaks on the line. |
![]() |
| Viewing: ASP Free Forums > Programming > Visual Basic Programming > help me please |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|