asp.net使用SerialPort的方法(VB简单版)[原创]

        最近有个项目,是在原来J2ee开发的进销存系统,收购页面中与称重机相连,取得称重机的数据,显示在文本框中.而这个系统是在互联网上使用的,称重机是与本地的一台机器相联.电子称是XK3190-A9型电子秤型.

         现在领导需要设计一个方案,所以就想到利用.net创建WebService 然后在页面中利用JavaScript获取WebService数据返回到页面中.

下面是实现代码:

下面的代码效率不是很高,而且每次进行获取数据都要开关端口,希望哪位大侠能够帮忙完善和改进.谢谢

Js代码,JavaScript 获取WebService数据:

function  getService() {
    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这个文件需要到网上找一找,不太好找,我找了半天时间.

< html  xmlns ="http://www.w3.org/1999/xhtml"   >
< 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
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, 
0len - 1)
        
'ASCII码转换开始
        Call gws.InputManage(arrBytes, len)
        Incoming 
= gws.GetDisplayText()
        
'ASCII码转换结束
        Return Incoming
    
End Function


End Class

ASCII代码转换类GetWeightString.vb类

Option   Strict   Off
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 StringAs 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 + 16)
        
'UPGRADE_WARNING: 未能解析对象 FSecond 的默认属性。 单击以获得更多信息:“ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"”
        OriStr2 = Mid(RedData, FSecond + 16)
        
'UPGRADE_WARNING: 未能解析对象 Fthird 的默认属性。 单击以获得更多信息:“ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"”
        OriStr3 = Mid(RedData, Fthird + 16)

        
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 StringAs 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 + 1Len(s) - PointPostion)) = 1 Then
            AddZero 
= Mid(s, 1, PointPostion) & Mid(s, PointPostion + 1Len(s) - PointPostion) & "0"

        
Else
            AddZero 
= s
        
End If

    
End Function



    
'**********************************
    '输入处理
    '处理接收到的字节流,并保存在全局变量
    'bytReceiveRyte()
    '**********************************
    Public Sub InputManage(ByRef bytInput() As ByteByRef 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(0As Byte

        
ReDim bytReceiveByte(0)

        intReceiveLen 
= 0

        
Call InputManage(bytTemp, 0)

    
End Sub

End Class

代码到这里就已经结束了.

JERRY 2008.3.20

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值