台灣最大程式設計社群網站
線上人數
838
 
會員總數:246038
討論主題:189625
歡迎您免費加入會員
討論區列表 >> VB6 >> 想請教用VB6取代終端機的功能
[]  
[我要回覆]
回應主題 加入我的關注話題 檢舉此篇討論 將提問者加入個人黑名單
想請教用VB6取代終端機的功能
價值 : 10 QP  點閱數:985 回應數:0
樓主

弗拉迪米爾
門外漢
0 2
101 4
發送站內信


目前完成RS232的相關傳輸指令,我想請問
要在DefaltString = "This is Testing String" 這段更改成 ESC鍵 & ESC(鍵) & "D" ,但似乎不可行嗎?

附上程式碼:





Dim DefaltString, DelayString, DefaltValue As String
Dim Get_data() As Byte



Private Sub Form_Load()
Dim i As Integer

DefaltString = "This is Testing String" 這段更改成 ESC鍵 & ESC(鍵) & "D" DelayString = "0.3"
DefaltValue = "AA AA 01 00 00 00 00 04 00 01 59"

Combo1.Text = "2400"

For i = 1 To 20
Combo2.AddItem "COM" & i, i - 1
Next

Combo2.ListIndex = 0

Text2.Text = DefaltString
Text1.Text = DelayString

End Sub

Private Sub Command1_Click()
Dim BoudRate As String
Dim ComPort As Integer

If Option4.Value = True And Text2.Text = "" Then
MsgBox "PLEASE INPUT THE DATA "
Exit Sub
End If

BoudRate = Combo1.Text

ComPort = Combo2.ListIndex + 1

Label4.Caption = ""
Text3.Text = ""
Delay 0.1

If Option4.Value = True Then '數值模式

Label4.Caption = Text2.Text
Text3.Text = GetRS232(Text2.Text, True, Text1.Text, ComPort, BoudRate)

ElseIf Option3.Value = True Then '字串模式
Label4.Caption = Text2.Text
Text3.Text = StringModeRS232(BoudRate, ComPort, Text2.Text, Text1.Text)
End If

End Sub


Public Function StringModeRS232(ByVal BoudRate As String, ByVal ComPort As Integer, ByVal SendString As String, ByVal DelayString As String) As String

With Form1.MSComm1
.CommPort = ComPort
.Settings = BoudRate & ",N,8,1"
.InputMode = comInputModeText
.PortOpen = True
.Output = SendString & Chr(13) '下達指令Q1.....
'.Output = SendString '純字串輸入
Delay DelayString
StringModeRS232 = .Input
.PortOpen = False
End With


End Function

Public Function GetRS232(ByVal Sendstr As String, blnRead As Boolean, ByVal DELAYTIME As Single, ByVal ComPort As Integer, ByVal BoudRate As String) As String
'MScomm1是串列通信控件,Sendstr是待傳輸的命令
'blnRead表示讀取指令回應標誌︰True表示讀取指令回應,False表示放棄讀取回應,即只發送指令,不接收該指令的回應。例如,"AS,+,9" 屬于無返回值指令,則設置blnRead為False
'DelayTime表示延遲等待時間,當回應訊息較長時使用,單位︰秒
Dim S As String, strT() As String
Dim N As Integer
Dim i As Integer
Dim GetTEMP As Variant
Dim GetData As String
Dim J As Variant
Dim Send_Data() As Byte
Dim GetEcho As String
Const timeout = 1 '延遲設置

With Form1.MSComm1
.CommPort = ComPort
.Settings = BoudRate & ",N,8,1"
.InputMode = comInputModeBinary
.PortOpen = True
End With

strT = Split(Sendstr, " ")
N = UBound(strT)
ReDim Send_Data(N)
For i = 0 To N
S = "&H" & strT(i)
Send_Data(i) = Val(S)
Next

GetTEMP = Send_Data

With Form1.MSComm1
.InBufferCount = 0 '清空接收緩衝區
.Output = GetTEMP '發送指令
If Not blnRead Then
GetRS232 = ""
.PortOpen = False
Exit Function 'blnRead為true 時讀取附應值
End If
'等待接收第一個位元組,如果在TimeOut時間內未收到第一個位元組,則發出通信超時錯誤
T1 = Timer
Do
DoEvents
Loop Until .InBufferCount > 0 Or Timer > T1 + timeout
'如果回傳位元組序列較長,則延時等待后續位元組的到達。
If DELAYTIME > 0 Then Call Delay(DELAYTIME)

If .InBufferCount = 0 Then
GetEcho = ""
If Not blnPortStatus Then Form1.MSComm1.PortOpen = False

Exit Function
End If

ReDim Get_data(.InBufferCount)
Get_data = .Input

For Each J In Get_data
GetData = "0" & Hex(J)
GetData = Right(GetData, 2)
GetEcho = GetEcho + GetData
Next J
GetRS232 = GetEcho
.PortOpen = False
End With

End Function

Public Sub Delay(DelayString)
Dim T1 As Single
'12/02/98
T1 = Timer
Do
DoEvents
If Timer < T1 Then T1 = 0 '12/02/98
Loop Until Timer > T1 + DelayString - 0.02

End Sub

Private Sub Option3_Click()
Text2.Text = DefaltString
Form1.Height = 2400
Text3.Height = 330
End Sub

Private Sub Option4_Click()
Text2.Text = DefaltValue
Form1.Height = 4560
Text3.Height = 2460

End Sub

搜尋相關Tags的文章: [ RS232 ] ,
本篇文章發表於2015-11-18 17:11
目前尚無任何回覆
   

回覆
如要回應,請先登入.