ขอความช่วยเหลือจากเซียนvb ครับท่าน การติดต่อhardware ด้วย RS232
พอดีผมกำลังทำโปรเจคการติดต่อhardwareด้วยvb6 ซึ่งผมมีhardware ที่จะinterfaceแล้วแต่ไม่รู้เขียนโค้ดยังงัยให้มันตอบกลับมา ผมลองส่งด้วยhyper ก็ตอบกลับได้ โค้ดส่งคือ *01:2,01=00 และตอบกลับเป็น *01:E2,01=01 แต่พอเขียนด้วยvb ไม่สามารถนำโค้ดมาโชว์บนtext box ได้ ขอความช่วยเหลือด้วยครับ ตัวอย่างโค้ดผมครับ
Private Sub Form_Load() MSComm1.CommPort = 1 MSComm1.InputLen = 1 MSComm1.PortOpen = True MSComm1.RThreshold = 1 MSComm1.Settings = "38400,n,8,1"
Private Sub TXTsend_keypress(keyascii As Integer) Dim strOut As Variant If keyascii = 13 Then strOut = TXTSend.Text MSComm1.Output = strOut End If End Sub
Private Sub Form_Unload(Cancel As Integer) MSComm1.PortOpen = False End Sub
Private Sub MSComm1_OnComm() If (MSComm1.CommEvent = comEvReceive) Then TxtRec.Text = MSComm1.Input End If ShowData TxtRec, (StrConv((strIn), vbUnicode)) End Sub
Public Static Sub ShowData(TextShow As Control, Data As String) Const SpeedBaud = 16000 Dim LngSize As Long, X
LngSize = Len(TextShow.Text) If TermSize > SpeedBaud Then TextShow.Text = Mid$(TextShow.Text, 4097) LngSize = Len(TextShow.Text) End If TextShow.SelStart = SpeedBaud Do X = InStr(Data, Chr$(8)) If X Then If X = 2 Then TextShow.SelStart = SpeedBaud - 1 TextShow.SelLength = 1 Data = Mid$(Data, X + 1) Else Data = Left$(Data, X - 2) & Mid$(Data, X + 1) End If End If Loop While X Do X = InStr(Data, Chr$(10)) If X Then Data = Left$(Data, X - 1) & Mid$(Data, X + 1) End If Loop While X X = 1 Do X = InStr(X, Data, Chr$(13)) If X Then Data = Left$(Data, X) & Chr$(10) & Mid$(Data, X + 1) X = X + 1 End If Loop While X TextShow.SelText = Data TextShow.SelStart = Len(TextShow.Text) End Sub
|