品牌:GE | 規格:全新 | 材質:原裝 |
產地:國外 |
IC697MDL251RR
設置MSComm控件的接收數據模式採用二進制方式,即 InputMode=comInputModeBinary,但用Input屬性讀取數據時,不能直接賦值給 Byte 類型變量,只能通過先賦值給一個 Variant 類型變量,返回一個二進制數據的數組,再轉換保存到Byte類型數變量中。
3.2.5 VB中有 Byte類型變量,但沒有字節的位處理語句。通道狀態字節的位處理要通過對該字節的值運算進行判斷,符號字節的位處理則要判斷符號字節的值是否大於127,大於127則為負數;壓縮的BCD碼存入Byte類型變量,VB係統只按十進制數處理,這要通過一個簡單算法換算,解壓BCD碼才能還原成十進制表示數值。假如a是Byte類型變量,W是Single類型變量,將一個壓縮的BCD碼存入 a中,則算法是:
W=(a\16)*10 + a-(a\16)*16
則W=a-(a\16)*6
3.3 程序代碼
在(通用)(聲明)中定義程序所用變量:
Dim ab(4) As Byte '字節數據類型數組,用來存貯接收到的一組字節數據
Dim av As Variant '用來從接收緩衝區讀取數據
Dim i As Integer
Dim j As Integer
Dim w As Integer '接收數據個數計數器
Dim b1 As Single
Dim b2 As Single
Dim WW As Single '十進制檢測值
Dim TD(8) as Boolean '通道狀態數組
Dim Wmax(8) As Single '大值數組
Dim Wmin(8) As Single '小值數組
在窗體中添加名為Command1的[開始]按鈕和名為MSComm1的MSComm控件。
[開始]按鈕的Click事件處理程序主要是對MSComm1控制的參數初始化設置,程序中大部分參數在設計時可在MSComm1控制的屬性窗口中設置:
Private Sub Command1_Click() '開始按鈕
With MSComm1
mPort=2 '使用COM2
.Setting="9600,N,8,1" '設置通信口參數
.InBufferSize=40 '設置MSComm1接收緩衝區為40字節
.OutBufferSize=2 '設置MSComm1發送緩衝區為2字節
.InputMode = comInputModeBinary '設置接收數據模式為二進制形式
.InputLen = 1 '設置Input 一次從接收緩衝讀取字節數為1
.SThreshold = 1 '設置Output 一次從發送緩衝讀取字節數為1
.InBufferCount = 0 '清除接收緩衝區
.OutBufferCount = 0 '清除發送緩衝區
For i=1 to 8
Wmax(i) = -99 '大值賦初值
Wmin(i) = 99 '小值賦初值
Next i
w = 0 '數據個數計數器清零
.RThreshold = 1 '設置接收一個字節產生OnComm事件
On Error Resume Next '改變錯誤處理的方式。
Err.Clear
If .PortOpen = False Then '判斷通信口是否打開
.PortOpen = True '打開通信口
If Err Then '錯誤處理
MsgBox "串口通信無效"
Exit Sub
End If
End If
End With
End Sub
為了達到實時數據採集目的,實時數據採集處理程序採用MSComm事件驅動方式。MSComm1_OnComm 的事件處理程序只處理comEvReceive事件,首先判斷幀數據的開始字節,關閉OnComm接收事件,然後接收數據字節,將壓縮BCD進行還原轉換,再接收符號字節,判斷數據符號,判斷數據大小值,後打開OnComm接收事件,等待下一次OnComm事件產生:
Private Sub MSComm1_OnComm()
With MSComm1
Select Case mEvent '判斷MSComm1通訊事件
Case comEvReceive '收到Rthreshold個字節產生的接收事件
av = .Input '讀取一個接收字節
ab(1) = av(0) '轉換保存到字節數據類型數組
If ab(1) = 170 Then '判斷是否為數據開始標志,0FAH=170
.RThreshold = 0 '關閉OnComm事件接收
W=W+1 '計數器加1
av = .Input '讀取通道狀態字節
ab(0) = av(0) '轉換保存到字節數據類型數
For i = 1 To 8 '通道狀態數組復位
TD(i) = False
Next i
cn = 2 '幀長度賦初值,一個開始字,一個狀態字
If ab(0) >= 128 Then '判斷第八通狀態
TD(8) = True '第八道是真
ab(0) = ab(0) - 128 '第7位置零
cn = cn + 3 '幀長度加3個字節
End If
If ab(0) >= 64 Then '判斷第七通狀態
TD(7) = True '第七道是真
ab(0) = ab(0) - 64 '第6位置零
cn = cn + 3 '幀長度加3個字節
End If
If ab(0) >= 32 Then '判斷第六通狀態
TD(6) = True '第六道是真
ab(0) = ab(0) - 32 '第5位置零
cn = cn + 3 '幀長度加3個字節
End If
If ab(0) >= 16 Then '判斷第五通狀態
TD(5) = True '第五道是真
ab(0) = ab(0) - 16 '第4位置零
cn = cn + 3 '幀長度加3個字節
End If
If ab(0) >= 8 Then '判斷第四通狀態
TD(4) = True '第四道是真
ab(0) = ab(0) - 8 '第3位置零
cn = cn + 3 '幀長度加3個字節
End If
If ab(0) >= 4 Then '判斷第三通狀態
TD(3) = True '第三道是真
ab(0) = ab(0) - 4 '第2位置零
cn = cn + 3 '幀長度加3個字節
End If
If ab(0) >= 2 Then '判斷第二通狀態
TD(2) = True '第二道是真
ab(0) = ab(0) - 2 '第0位置零
cn = cn + 3 '幀長度加3個字節
End If
If ab(0) >= 1 Then '判斷第一通狀態
TD(1) = True '第一道是真
cn = cn + 3 '幀長度加3個字節
End If
For i = 1 To 8 '
If TD(i) = True Then
av = .Input '讀取第一個數據字節(BCD碼高位字節)
ab(2) = av(0) '轉換保存到字節數據類型數組
av = .Input '讀取第二個數據字節(BCD碼低位字節)
ab(3) = av(0) '轉換保存到字節數據類型數組
av = .Input '讀取第三個接收字節(符號位字節)
ab(4) = av(0) '轉換保存到字節數據類型數組
b1 = ab(2) - 6 * (ab(2) \ 16) '高位字節壓縮BCD碼轉換為實數
b2 = ab(3) - 6 * (ab(3) \ 16) '低位字節壓縮BCD碼轉換為實數
WW = b2 + b1 / 100 '數值組合,標定小數點
If ab(4) > 127 Then WW = -WW '判斷數據符號位
Label1(i-1) = Format(WW,"0.00") '顯示毫米單位數值,2位小數
If WW>Wmax(i) And WW<51 Then '判斷大值,儀表在剛開始工作時有幹擾,會傳導一些亂碼,位移傳感器有參數偏差,大值一般都略大於50毫米,所以取51為極限大值,取-51為極限小值。
SICK OPTIC ELECTRONIC 1012276 SENDER, 60M (196) RANGE,
SICK OPTIC ELECTRONIC 1012295 2 Beam Sender 500 mm Beam Spacing; Total Height 70
SICK OPTIC ELECTRONIC 1012296 2 Beam Receiver 500 mm Beam Spacing; Total Height
SICK OPTIC ELECTRONIC 1012463 PROXIMITY PHOTOELECTRIC,
SICK OPTIC ELECTRONIC 1012502 SENSOR
SICK OPTIC ELECTRONIC 1012503 SENSOR
SICK OPTIC ELECTRONIC 1012506 LIGHT CURTAIN
SICK OPTIC ELECTRONIC 1012507 LIGHT CURTAIN
SICK OPTIC ELECTRONIC 1012510 CONTROL
SICK OPTIC ELECTRONIC 1012512 LGHT CTN SENDR
SICK OPTIC ELECTRONIC 1012513 LGHT CTN RECVR
SICK OPTIC ELECTRONIC 1012523 CONTROL UNIT FOR LIGHT CURTAIN W/MUTING 24VDC RLY
SICK OPTIC ELECTRONIC 1012569 RS 232 INTERFACE, YELLOW
SICK OPTIC ELECTRONIC 1012570 RS 422 INTERFACE, YELLOW
SICK OPTIC ELECTRONIC 1012571 RS 232 INTERFACE, YELLOW
SICK OPTIC ELECTRONIC 1012572 RS 422 INTERFACE, YELLOW
SICK OPTIC ELECTRONIC 1012576 FGS Sender, 300mm, 14mm Resolution
SICK OPTIC ELECTRONIC 1012577 FGS Receiver, 300mm, 14mm Resolution
SICK OPTIC ELECTRONIC 1012578 FGS Sender, 450mm, 14mm Resolution
SICK OPTIC ELECTRONIC 1012579 FGS Receiver, 450mm, 14mm Resolution
SICK OPTIC ELECTRONIC 1012580 FGS Sender, 600mm, 14mm Resolution
SICK OPTIC ELECTRONIC 1012581 FGS Receiver, 600mm, 14mm Resolution
SICK OPTIC ELECTRONIC 1012582 FGS Sender, 750mm, 14mm Resolution
SICK OPTIC ELECTRONIC 1012583 FGS Receiver, 750mm, 14mm Resolution
SICK OPTIC ELECTRONIC 1012584 FGS Sender, 900mm, 14mm Resolution
SICK OPTIC ELECTRONIC 1012585 FGS Receiver, 900mm, 14mm Resolution
SICK OPTIC ELECTRONIC 1012586 FGS Sender, 300mm, 30mm Resolution
SICK OPTIC ELECTRONIC 1012587 FGS Receiver, 300mm, 30mm Resolution
SICK OPTIC ELECTRONIC 1012588 FGS Sender, 450mm, 30mm Resolution
SICK OPTIC ELECTRONIC 1012589 FGS Receiver, 450mm, 30mm Resolution
SICK OPTIC ELECTRONIC 1012590 FGS Sender, 600mm, 30mm Resolution
SICK OPTIC ELECTRONIC 1012591 FGS Receiver, 600mm, 30mm Resolution
SICK OPTIC ELECTRONIC 1012592 FGS Sender, 750mm, 30mm Resolution
SICK OPTIC ELECTRONIC 1012593 FGS Receiver, 750mm, 30mm Resolution
SICK OPTIC ELECTRONIC 1012594 FGS Sender, 900mm, 30mm Resolution
SICK OPTIC ELECTRONIC 1012595 FGS Receiver, 900mm, 30mm Resolution
SICK OPTIC ELECTRONIC 1012606 LIGHT CURTAIN
SICK OPTIC ELECTRONIC 1012607 LIGHT CURTAIN
SICK OPTIC ELECTRONIC 1012609 PART
SICK OPTIC ELECTRONIC 1012610 unknown
SICK OPTIC ELECTRONIC 1012611 unknown
SICK OPTIC ELECTRONIC 1012612 LIGHT CURTAIN
SICK OPTIC ELECTRONIC 1012613 LIGHT CURTAIN
SICK OPTIC ELECTRONIC 1012708 SCANNER
SICK OPTIC ELECTRONIC 1012711 Fiber Photoelectric, NAMUR, Red, 4-pin M12 connect
SICK OPTIC ELECTRONIC 1012753 FGS Sender, 1050mm, 30mm Resolution
SICK OPTIC ELECTRONIC 1012754 FGS Receiver, 1050mm, 30mm Resolution
SICK OPTIC ELECTRONIC 1012756 FGS Receiver, 300mm, 14mm Resolution
SICK OPTIC ELECTRONIC 1012757 FGS Sender, 450mm, 14mm Resolution
SICK OPTIC ELECTRONIC 1012758 LIGHT CURTAIN RECEIVER 450MM 14MM RESOLUTION
SICK OPTIC ELECTRONIC 1012759 FGS Sender, 600mm, 14mm Resolution
SICK OPTIC ELECTRONIC 1012760 FGS Receiver, 600mm, 14mm Resolution
SICK OPTIC ELECTRONIC 1012761 FGS Sender, 750mm, 14mm Resolution
SICK OPTIC ELECTRONIC 1012762 FGS Receiver, 750mm, 14mm Resolution
SICK OPTIC ELECTRONIC 1012763 FGS Sender, 900mm, 14mm Resolution
SICK OPTIC ELECTRONIC 1012764 FGS Receiver, 900mm, 14mm Resolution
SICK OPTIC ELECTRONIC 1012765 FGS Sender, 1050mm, 14mm Resolution
SICK OPTIC ELECTRONIC 1012766 FGS Receiver, 1050mm, 14mm Resolution
SICK OPTIC ELECTRONIC 1012767 LIGHT CURTAIN SENDER 1200MM 0-6MRANGE 24VDC
SICK OPTIC ELECTRONIC 1012768 LIGHT CURTAIN RECEIVER 1200MM 0-6MRANGE 24VDC
SICK OPTIC ELECTRONIC 1012769 LIGHT CURTAIN SENDER 1350MM 0-6MRANGE 24VDC
SICK OPTIC ELECTRONIC 1012770 LIGHT CURTAIN RECEIVER 1350MM 0-6MRANGE 24VDC
SICK OPTIC ELECTRONIC 1012771 LIGHT CURTAIN
SICK OPTIC ELECTRONIC 1012772 FGS Receiver, 1500mm, 14mm Resolution
SICK OPTIC ELECTRONIC 1012773 FGS Sender, 1650mm, 14mm Resolution
SICK OPTIC ELECTRONIC 1012774 FGS Receiver, 1650mm, 14mm Resolution
SICK OPTIC ELECTRONIC 1012775 LIGHT CURTAIN
SICK OPTIC ELECTRONIC 1012776 FGS Receiver, 1800mm, 14mm Resolution
SICK OPTIC ELECTRONIC 1012777 SAFETY LIGHT CURTAIN SENDER
SICK OPTIC ELECTRONIC 1012778 SAFETY LIGHT CURTAIN RECEIVER
SICK OPTIC ELECTRONIC 1012779 LIGHT CURTAIN SENDER 450MM 0-6MRANGE 24VDC
SICK OPTIC ELECTRONIC 1012780 LIGHT CURTAIN RECEIVER 450MM 0-6MRANGE 24VDC
SICK OPTIC ELECTRONIC 1012781 LGHT CURTAIN SENDER 600MM 30MM RESOLUTION
SICK OPTIC ELECTRONIC 1012782 FGS Receiver, 600mm, 30mm Resolution
SICK OPTIC ELECTRONIC 1012783 FGS Sender, 750mm, 30mm Resolution
SICK OPTIC ELECTRONIC 1012784 FGS Receiver, 750mm, 30mm Resolution
SICK OPTIC ELECTRONIC 1012785 LIGHT CURTAIN TRANSMITTER 932MM
SICK OPTIC ELECTRONIC 1012786 FGS Receiver, 900mm, 30mm Resolution
SICK OPTIC ELECTRONIC 1012787 FGS Sender, 1050mm, 30mm Resolution
SICK OPTIC ELECTRONIC 1012788 FGS Receiver, 1050mm, 30mm Resolution