最近有個項目,是在原來J2ee開發的進銷存系統,收購頁面中與稱重機相連,取得稱重機的數據,顯示在文本框中.而這個系統是在互聯網上使用的,稱重機是與本地的一臺機器相聯.電子稱是XK3190-A9型電子秤型.
現在領導需要設計一個方案,所以就想到利用.net創建WebService 然後在頁面中利用JavaScript獲取WebService數據返回到頁面中.
下面是實現代碼:
下面的代碼效率不是很高,而且每次進行獲取數據都要開關端口,希望哪位大俠能夠幫忙完善和改進.謝謝
Js代碼,JavaScript 獲取WebService數據:
GetService.useService("../Service.asmx?WSDL","GetEquipment");
intCallID= GetService.GetEquipment.callService(Service_result,"GetEquipment",GetService);
}
function Service_result(result){
if(result.error){
document.getElementById("ProductNumber").value = "數據讀取有誤!";
}
else{
if(result.value == null || result.value == ""){
document.getElementById("ProductNumber").value = "數據讀取中!";
getService();
}
else{
document.getElementById("ProductNumber").value = result.value;
}
}
}
需要獲取數據的HTML頁面:
這裏需要注意的是<div id="GetService" style="behavior:url(webservice.htc)">
這個webservice.htc這個文件需要到網上找一找,不太好找,我找了半天時間.
<head id="Head1" runat="server">
<title>測試JS使用WebService</title>
<script src="JScript.js" type="text/javascript"></script>
</head>
<body>
<form id="form1" runat="server">
<div id="GetService" style="behavior:url(webservice.htc)">
<input name="ProductNumber" type="text" />
<input id="Button2" type="button" value="獲取數據" onclick="getService()" />
</div>
</form>
</body>
</html>
下面是WebService的Service.asmx頁(VB.NET) 想用C#寫一套,但是沒有太多時間進行處理
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.IO.Ports.SerialPort
Imports System.Threading.Thread
<WebService(Namespace:="http://tempuri.org/")> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Public Class Service
Inherits System.Web.Services.WebService
Public WithEvents mySerialPort As System.IO.Ports.SerialPort = New System.IO.Ports.SerialPort("COM1", 1200, IO.Ports.Parity.None, 8, IO.Ports.StopBits.One)
Public Incoming
Public gws As GetWeightString = New GetWeightString
Public arrBytes() As Byte
<WebMethod()> _
Public Function GetEquipment() As String
myspOpen()
Dim strTemp = ""
strTemp = gdata()
myspClose()
Return strTemp
End Function
Public Sub myspOpen()
If mySerialPort.IsOpen = True Then
mySerialPort.Close()
Else
mySerialPort.Open()
End If
End Sub
Public Sub myspClose()
If mySerialPort.IsOpen = True Then
mySerialPort.Close()
Else
mySerialPort.Open()
End If
End Sub
Public Function gdata() As String
Dim len
len = mySerialPort.BytesToRead
Do While len < 96
len = mySerialPort.BytesToRead
Loop
ReDim arrBytes(len)
mySerialPort.Read(arrBytes, 0, len - 1)
'ASCII碼轉換開始
Call gws.InputManage(arrBytes, len)
Incoming = gws.GetDisplayText()
'ASCII碼轉換結束
Return Incoming
End Function
End Class
ASCII代碼轉換類GetWeightString.vb類
Option Explicit On
Imports Microsoft.VisualBasic.Compatibility
Public Class GetWeightString
Public WeightStr As String
'**********************************
'接收模塊
'**********************************
Public bytReceiveByte() As Byte '接收到的字節
Public intReceiveLen As Short '接收到的字節數
Public intHexWidth As Short '顯示列數(電子秤的輸出位數小循環)
Public strAscii As String '電子秤輸出的ASCII碼
Public Function GetWeightA1(ByRef RedData As String) As String
'RedData取值“+004038216..+004038216..+004038216..+00403”表示爲40.38(XK3190-A9型電子秤)
'RedData取值“+00025011D..+00025011D..+00025011D..+00025011”表示爲25.01(XK3190-A1+型電子秤)
'實際上500kg量程的XK3190-A1+電子秤的分辨率設爲0.1kg所以讀數爲25.0kg
Dim Fthird, FFirst, FSecond As Object
'Dim FLength As Short
Dim OriStr1 As String
Dim OriStr2 As String
Dim OriStr3 As String
If RedData = "" Then
GetWeightA1 = ""
Exit Function
End If
'UPGRADE_WARNING: 未能解析對象 FFirst 的默認屬性。 單擊以獲得更多信息:“ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"”
FFirst = InStr(1, RedData, "+")
'UPGRADE_WARNING: 未能解析對象 FFirst 的默認屬性。 單擊以獲得更多信息:“ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"”
'UPGRADE_WARNING: 未能解析對象 FSecond 的默認屬性。 單擊以獲得更多信息:“ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"”
FSecond = InStr(FFirst + 1, RedData, "+")
'UPGRADE_WARNING: 未能解析對象 FSecond 的默認屬性。 單擊以獲得更多信息:“ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"”
'UPGRADE_WARNING: 未能解析對象 Fthird 的默認屬性。 單擊以獲得更多信息:“ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"”
Fthird = InStr(FSecond + 1, RedData, "+")
'UPGRADE_WARNING: 未能解析對象 FSecond 的默認屬性。 單擊以獲得更多信息:“ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"”
'UPGRADE_WARNING: 未能解析對象 FFirst 的默認屬性。 單擊以獲得更多信息:“ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"”
If FFirst >= FSecond Then
GetWeightA1 = ""
Exit Function
End If
'UPGRADE_WARNING: 未能解析對象 FFirst 的默認屬性。 單擊以獲得更多信息:“ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"”
OriStr1 = Mid(RedData, FFirst + 1, 6)
'UPGRADE_WARNING: 未能解析對象 FSecond 的默認屬性。 單擊以獲得更多信息:“ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"”
OriStr2 = Mid(RedData, FSecond + 1, 6)
'UPGRADE_WARNING: 未能解析對象 Fthird 的默認屬性。 單擊以獲得更多信息:“ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"”
OriStr3 = Mid(RedData, Fthird + 1, 6)
If OriStr1 = OriStr2 And OriStr2 = OriStr3 Then
'GetWeightA9 = AddZero(Str(Val(OriStr1) / 100))
'XK3190-A1電子秤實際情況爲:
GetWeightA1 = AddZero(Str(Val(OriStr1) / 10))
Else
GetWeightA1 = ""
End If
End Function
Public Function AddZero(ByRef FOriStr As String) As String
'爲重量加上二位有效數字
Dim PointPostion As Short
Dim s As String
s = Trim(FOriStr)
PointPostion = InStr(s, ".")
If PointPostion = 0 Then
AddZero = s & ".00"
ElseIf Len(Mid(s, PointPostion + 1, Len(s) - PointPostion)) = 1 Then
AddZero = Mid(s, 1, PointPostion) & Mid(s, PointPostion + 1, Len(s) - PointPostion) & "0"
Else
AddZero = s
End If
End Function
'**********************************
'輸入處理
'處理接收到的字節流,並保存在全局變量
'bytReceiveRyte()
'**********************************
Public Sub InputManage(ByRef bytInput() As Byte, ByRef intInputLenth As Short)
Dim n As Short '定義變量及初始化
ReDim Preserve bytReceiveByte(intReceiveLen + intInputLenth)
For n = 1 To intInputLenth Step 1
bytReceiveByte(intReceiveLen + n - 1) = bytInput(n - 1)
Next n
intReceiveLen = intReceiveLen + intInputLenth
End Sub
'***********************************
'爲輸出準備文本
'保存在全局變量
'strText
'總行數保存在
'intLine
'***********************************
Public Function GetDisplayText() As String
Dim n As Short
Dim intValue As Short
Dim intHighHex As Short
Dim intLowHex As Short
Dim strSingleChr As New VB6.FixedLengthString(1)
intHexWidth = 9
strAscii = "" '設置初值
Dim a = ""
'*****************************************
'獲得16進制碼和ASCII碼的字符串
'*****************************************
For n = 1 To intReceiveLen
intValue = bytReceiveByte(n - 1)
If intValue < 32 Or intValue > 128 Then '處理非法字符
strSingleChr.Value = Chr(46) '對於不能顯示的ASCII碼,
Else '用"."表示
strSingleChr.Value = Chr(intValue)
End If
strAscii = strAscii & strSingleChr.Value
intHighHex = intValue 16
intLowHex = intValue - intHighHex * 16
If intHighHex < 10 Then
intHighHex = intHighHex + 48
Else
intHighHex = intHighHex + 55
End If
If intLowHex < 10 Then
intLowHex = intLowHex + 48
Else
intLowHex = intLowHex + 55
End If
If (n Mod intHexWidth) = 0 Then '設置換行
'strAscii = strAscii + Chr$(13) + Chr$(10)
If Len(strAscii) > 60 Then
WeightStr = Right(strAscii, 60)
a = GetWeightA1(WeightStr)
End If
'------------------------------------------------
End If
If intReceiveLen > 524 Then
Call ClearWeight()
GetDisplayText = a
Exit Function
End If
Next n
GetDisplayText = a
End Function
Private Sub ClearWeight()
Dim bytTemp(0) As Byte
ReDim bytReceiveByte(0)
intReceiveLen = 0
Call InputManage(bytTemp, 0)
End Sub
End Class
代碼到這裏就已經結束了.
JERRY 2008.3.20