?? spcp.bas
字號:
Attribute VB_Name = "spcp"
'*************************************************
'消息幀數據格式:
' 1 0 A B X X
'其中 10 為消息標識,
' AB表示文本長度,L=A*100+B
' XX為配位字符,任意
'控制幀數據格式
' 0 1 A B M N
'其中 01為控制標識,
' AB為請求標識
' MN為附加標識
' 11表示請求對方接收文件,M表示描述字串中文件名子串的長度
' N表示描述字串中文件大小子串的長度
' 10通知對方放棄傳輸
' 00通知文件傳輸完畢
' 01請求對方發送數據, MN為10請求發送下一個
' MN為00請求重發
'數據幀數據格式
' 0 0 A B M N
'其中 00 為數據標識,
' AB表示數據長度,L=A*100+B
' MN為校驗,M*100+N=A+B
'*************************************************
'*************************************************
Public intCommFlag As Long
'intCommFlag 是comm控件工作的標志,其意義如下:
'0 已經關閉
'1 打開,等待
'2 正在發送二進制數據
'4 正在發送文本
'8 正在發送控制符
'16 正在接收數據
'*************************************************
Public blnFileTransFlag As Boolean '文件傳輸方式標志
'true 為發送
'false 為接收
Public strFileDirectary As String '文件路徑
Public strFileName As String '文件名
Public intFileLenth As Long '文件長度
Dim intFileNumber As Long '文件號
'*************************************************
Dim intDataLenth As Long '數據幀長度
Dim intDataCount As Long '數據幀總計數
Dim intDataNumber As Long '已經發送數據幀數
'*************************************************
Dim strSendFile As String '"請求接收新文件"幀負載
Dim bytSendFile() As Byte '數據幀負載
Dim bytFileBuffer() As Byte '文件緩沖區
Dim intFileReceiveLenth As Long '已經接收文件的字節數
Dim intReceiveLen As Long '每次讀入數據的字節數
Dim bytReceive() As Byte '接收到的數據幀負載
'*************************************************
'****************************************************
'串口輸入管理
'串行口接收到數據后即調用該過程進行處理
'****************************************************
Public Sub InputManager()
intCommFlag = 16
Dim bytTest() As Byte
Dim strReceive As String
Dim intTest As Long
Dim strTemp As String
frmMain.ctrMSComm.InputMode = comInputModeBinary
frmMain.ctrMSComm.InputLen = 6
bytTest = frmMain.ctrMSComm.Input
ReDim Preserve bytTest(5) '獲取幀頭
If bytTest(0) = 1 And bytTest(1) = 0 Then '收到信息幀
intReceiveLen = bytTest(2) * 100 + bytTest(3)
frmMain.ctrMSComm.InputMode = comInputModeText
frmMain.ctrMSComm.InputLen = 0
For n = 1 To 1000000 '延時
Next n
strReceive = frmMain.ctrMSComm.Input
strTemp = "接收:" + strReceive
Call frmMain.ReceiveDisplay(strTemp, 2) '調用信息顯示
ElseIf bytTest(0) = 0 And bytTest(1) = 1 Then '收到控制幀
intTest = bytTest(2) * 100 + bytTest(3) '控制幀分析
Select Case intTest
Case 101 '對方請求接收新文件
intReceiveLen = bytTest(4)
frmMain.ctrMSComm.InputMode = comInputModeText
frmMain.ctrMSComm.InputLen = intReceiveLen
Do
Loop While frmMain.ctrMSComm.InBufferCount < intReceiveLen
strFileName = frmMain.ctrMSComm.Input '接收文件名信息
intReceiveLen = bytTest(5)
frmMain.ctrMSComm.InputLen = intReceiveLen
Do
Loop While frmMain.ctrMSComm.InBufferCount < intReceiveLen
strReceive = frmMain.ctrMSComm.Input '接收文件長度信息
strReceive = LTrim(strReceive)
intFileLenth = Val(strReceive) '文件長度
If MsgBox("對方發送文件" + strFileName + "," + Chr(13) + "文件大小為 " + Str(intFileLenth) + "字節" + Chr(13) + "是否接收?", vbOKCancel) = vbOK Then
Call FileReceiveManager(0)
End If
intCommFlag = 1
Case 100 '對方通知放棄傳輸
Case 1 '對方請求發送數據
If bytTest(4) = 1 Then '請求發送數據包
intDataNumber = intDataNumber + 1
If intDataNumber > intDataCount Then
Call FileSendManager(4) '發送完成信號
Else
Call FileSendManager(1) '發送新一幀數據
End If
Else
Call FileSendManager(2) '重發上一幀數據
End If
intCommFlag = 1
Case 0 '接收到傳輸完畢信號
Call FileReceiveManager(4)
intCommFlag = 1
Case Else
End Select
ElseIf bytTest(0) = 0 And bytTest(1) = 0 Then '收到數據幀
intCommFlag = 16
intTest = bytTest(4) * 100 + bytTest(5)
intReceiveLen = bytTest(2) * 100 + bytTest(3) '數據字節數
frmMain.ctrMSComm.InputMode = comInputModeBinary
frmMain.ctrMSComm.InputLen = intReceiveLen
ReDim bytReceive(intReceiveLen - 1)
Do
Loop While frmMain.ctrMSComm.InBufferCount < intReceiveLen
bytReceive = frmMain.ctrMSComm.Input
If intTest = bytTest(2) + bytTest(3) Then '根據校驗和作出響應
Call FileReceiveManager(1) '校驗和正確請求發送新數據
Else
Call FileReceiveManager(2) '校驗和錯請求重新發送
End If
intCommFlag = 1
Else
intCommFlag = 1
End If
intCommFlag = 1
End Sub
'************************************************************
'文件發送管理
'根據接收端的請求發送文件
'************************************************************
Public Sub FileSendManager(intFlag As Long)
Dim bytTest(5) As Byte
Dim bytSend() As Byte
Dim intSendLen As Long
Dim intProgress
Select Case intFlag
Case 0 '發送新文件
intCommFlag = 8
intFileNumber = FreeFile
Open strFileDirectary For Binary As #intFileNumber '打開文件
intFileLenth = LOF(intFileNumber) '獲得文件長度
ReDim bytFileBuffer(intFileLenth - 1)
Get #intFileNumber, , bytFileBuffer '將文件讀入緩沖區
bytTest(0) = 0 '組織幀頭
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -