abap調vb寫的dll實現電子天平的讀數(帶控件版)

廢話很少說,直接上。java

鑑於abap調研的dll文件須要在wins註冊,本身嘗試過delphi和C#感受不是很好,最後毅然選擇了VB來寫函數

由於須要用到MScomm控件,因此對於將要寫的dll須要帶form的,貌似這樣才能將控件加到dll中來。工具

步驟:測試

1,新建dll程序,添加一個窗體spa

2,在from_load中初始化com口參數code

1
2
3
4
5
6
7
8
With  MSComm1
         .CommPort = 1  '設置Com1爲通訊端口
         .Settings = "1200,n,7,1"   '設置通訊端口參數 9600赫茲、偶校驗、7個數據位、1箇中止位.(這裏須要進一步說明的是:.Setting=」BBBB,P,D,S」。
         .InBufferSize = 16  '設置緩衝區接收數據爲40字
         .InputLen = 1  '設置Input一次從接收緩衝讀取字節數爲1
         .RThreshold = 1  '設置接收一個字節就產生OnComm事件
         .PortOpen = True
  End  With

3,寫對應的端口數據接受(由於以前用端口測試工具測試過傳出的數據流,因此下面代碼只是針對特定數據流的截取)orm

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
On  Error  Resume  Next
If  MSComm1.CommEvent = comEvReceive Then
   If  MSComm1.InBufferCount > 0 Then
    Rx_buff = MSComm1.Input
    If  Rx_buff = "N"  Then  start = "S"
    Constop = False
    For  i = 0 To  UBound(Rx_buff)
    If  start = "S"  Then
    send = send & Rx_buff
    If  Len(send) > 17 Then
    Text1.Text = CDbl (Mid(send, 8, 10))
    start = "E"
    MSComm1.PortOpen = False
    End  If
    End  If
    Next  i
   End  If
End  If

 4,在dll的class1中寫函數啦blog

1
2
3
4
5
6
7
8
9
Public  Function  show()
Form1.str = "s"
Form1.Caption = "鏈接狀態"
Form1.show vbModal
End  Function
 
Public  Function  sget() As  String
sget = Form1.str & Form1.send
End  Function

先調出窗口,再獲取端口值事件

由於能力有限,在測試的時候沒法將窗口隱藏而不影響到form_load的執行,因此,纔有這個必須出現的窗口ci

好了,至此,一個帶from的調用Mscomm控件的dll文件就寫好了,

5,將dll文件保存到system32/syswow64下

運行cmd註冊dll文件

6,abap調用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
REPORT ZLYTEST_DLLTEST001.
include ole2incl.
data win32 type ole2_object.
DATA SUM TYPE I.
data label type string.
create object win32 'lytest10.class1' .
*create object win32 'TESTDLL' .
call METHOD of win32 'show' .
call method of win32 'sget'  = label.
 
 
* 0  Successful processing of the method meth.
* 1  Communication Error to SAP GUI.
* 2  Error when calling method meth.
* 3  Error when setting a property.
* 4  Error when reading a property.
*
 
 
write label.

  至此,就將數據帶回abap來了,而後怎麼操做這個數,就看需求啦

相關文章
相關標籤/搜索