基于Windows的应用程序 [英] windows based applications

查看:77
本文介绍了基于Windows的应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
我有用于访问仪器的vb脚本(通过使用仪器的ip地址和端口号).当我尝试使用基于窗口的应用程序连接仪器时,错误消息为:
与服务器的连接失败,返回以下代码:由于目标计算机主动拒绝它,因此无法建立连接:192.168.1.250:10000".
任何人都可以帮助我如何解决此错误...
我的vb脚本是

Hi to all,
I have vb script for accessing instruments (by using of ip address and port number of instrument).when i trying connect the instrument using of window based application it gives error message as:
"Connection with the server failed the following code was returned:No connection could be made because the target machine actively refused it:192.168.1.250:10000".
Any one help me how to solve this error please...
my vb script is

Option Explicit On
Option Strict On
Imports System.Net.Sockets
Public Class Form 1
Inherits System.Windows.Forms.Form
#Region " Windows Code created by Form Designer "
 ublic Sub New()
MyBase.New()
' This call is necessary for Windows Form Designer.
InitializeComponent()
' InitializeComponent() Initialization is added after the call.
End Sub
' The Form overwrites dispose to execute post-processing in the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
' Necessary for Windows Form Designer.
Private components As System.ComponentModel.IContainer
'Memo: The following procedure is necessary for Windows Form Designer.
'Change by using Windows Form Designer.
'Do not use code editor to change.
'Friend WithEvents components As System.ComponentModel.IContainer


Friend WithEvents Start As System.Windows.Forms.Button
Friend WithEvents lstOutput As System.Windows.Forms.ListBox
<system.diagnostics.debuggerstepthrough()>
Private Sub InitializeComponent()
Me.Start() = New System.Windows.Forms.Button
Me.lstOutput = New System.Windows.Forms.ListBox
Me.SuspendLayout()
'
'Start
'
Me.Start.Location = New System.Drawing.Point(16, 16)
Me.Start.Name = "Start"
Me.Start.Size = New System.Drawing.Size(88, 32)
Me.Start.TabIndex = 0
Me.Start.Text = "Start"
'lstOutput
'
Me.lstOutput.ItemHeight = 12
Me.lstOutput.Location = New System.Drawing.Point(16, 64)
Me.lstOutput.Name = "lstOutput"
Me.lstOutput.Size = New System.Drawing.Size(264, 196)
Me.lstOutput.TabIndex = 1
'
'Form 1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 12)
Me.ClientSize = New System.Drawing.Size(296, 273)
Me.Controls.Add(Me.lstOutput)
Me.Controls.Add(Me.Start)
Me.Name = "Form 1"
Me.Text = "Form 1"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub Start_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Start.Click

Dim IpAddress As String
Dim PortNum As Integer
Dim Client As TcpClient
Dim Buffer() As Byte
Dim InBuff(1532) As Byte
Dim TxCommand As String
Dim RxResponse As String
Dim Temp As String
Dim j As Integer
Dim Dreg(5) As Double
Dim DregStr$
Dim SubHeader$
' IP Address specification
IpAddress = "192.168.1.250"
' Port Number specification
PortNum = 10000
Client = New TcpClient
'Line connection processing
Try
Client.Connect(IpAddress, PortNum)
Catch ex As Exception
MsgBox("Connection with the server failed, and the following code was returned:" & ex.Message, 0, "connection error")
Exit Sub
End Try
'Read D0 to D4 (5 points) with the A-compatible 1E frame command.
TxCommand = "01ff000a4420000000000500"
Buffer = System.Text.Encoding.Default.GetBytes(TxCommand.ToCharArray)
'Sending a read command
Client.GetStream().Write(Buffer, 0, Buffer.Length)
'Waiting for a response from an Ethernet adapter
While Not Client.GetStream().DataAvailable()
Application.DoEvents()
End While
If Client.GetStream().DataAvailable() Then
Client.GetStream().Read(InBuff, 0, InBuff.Length)
RxResponse = System.Text.Encoding.Default.GetString(InBuff)
SubHeader = Mid$(RxResponse, 3, 2)
If SubHeader = "00" Then 'Normal response
Temp = "" 'Initialization of an output character string
For j = 0 To 4
DregStr$ = Mid(RxResponse, j * 4 + 5, 4)
Dreg(j) = Val("&H" + DregStr$)
Temp = Temp + Format(Dreg(j), "#####0") + " "
Next
lstOutput.Items.Insert(lstOutput.Items.Count, Temp)
ElseIf SubHeader = "5B" Then ' In an abnormal response, an abnormal code is added.
Temp = "Terminate Code = " & SubHeader & " Error Code = " & Mid$(RxResponse, 5, 2)
lstOutput.Items.Insert(lstOutput.Items.Count, Temp)
Else
Temp = "Terminate Code = " & SubHeader
lstOutput.Items.Insert(lstOutput.Items.Count, Temp)
End If
lstOutput.SelectedIndex = lstOutput.Items.Count - 1
End If
' Line disconnection processing
Client.GetStream().Close()
Client.Close()
End Sub
End Class



添加了代码块[/编辑]



Code block added[/Edit]

推荐答案

Dim SubHeader
Dim SubHeader


' IP地址规范 IpAddress = " ' 端口号规范 PortNum = 10000 客户端= 新建 TcpClient ' 线路连接处理 尝试 Client.Connect(IpAddress,PortNum) 捕获,例如 As 异常 MsgBox(" & ex.Message , 0 " ) 退出 结束 尝试 ' 使用兼容A的1E帧命令读取D0至D4(5点). TxCommand = " Buffer = System.Text.Encoding.默认 .GetBytes(TxCommand.ToCharArray) ' 发送读取命令 Client.GetStream().Write(Buffer, 0 ,Buffer.Length) ' 等待来自以太网适配器的响应 同时 不是 Client.GetStream().DataAvailable() Application.DoEvents() 结束 同时 如果 Client.GetStream().DataAvailable()然后 Client.GetStream().Read(InBuff, 0 ,InBuff.Length) RxResponse = System.Text.Encoding.默认 .GetString(InBuff) SubHeader = Mid
' IP Address specification IpAddress = "192.168.1.250" ' Port Number specification PortNum = 10000 Client = New TcpClient 'Line connection processing Try Client.Connect(IpAddress, PortNum) Catch ex As Exception MsgBox("Connection with the server failed, and the following code was returned:" & ex.Message, 0, "connection error") Exit Sub End Try 'Read D0 to D4 (5 points) with the A-compatible 1E frame command. TxCommand = "01ff000a4420000000000500" Buffer = System.Text.Encoding.Default.GetBytes(TxCommand.ToCharArray) 'Sending a read command Client.GetStream().Write(Buffer, 0, Buffer.Length) 'Waiting for a response from an Ethernet adapter While Not Client.GetStream().DataAvailable() Application.DoEvents() End While If Client.GetStream().DataAvailable() Then Client.GetStream().Read(InBuff, 0, InBuff.Length) RxResponse = System.Text.Encoding.Default.GetString(InBuff) SubHeader = Mid


(RxResponse, 3 2 ) 如果 SubHeader = " 然后 ' 正常响应 Temp = " ' 输出字符串的初始化 对于 j = 0 >到 4 DregStr
(RxResponse, 3, 2) If SubHeader = "00" Then 'Normal response Temp = "" 'Initialization of an output character string For j = 0 To 4 DregStr


这篇关于基于Windows的应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆