vb.net串口源码 vb串口程序
能给我发个VB与三菱PLC通讯的源代码实例吗,非常感谢啊
得说明是什么型号的PLC啊,串口还是网口,VB6还是VB.NET?
创新互联从2013年创立,是专业互联网技术服务公司,拥有项目成都网站设计、做网站网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元全椒做网站,已为上家服务,为全椒各地企业和个人服务,联系电话:13518219792
算了,写段代码,VB.NET与Q系列以太网通讯的:
Imports System.Net
Imports System.Runtime.InteropServices
Public Class Form1
Dim Handle1 As Int32
Dim EntLink1 As Boolean
Dim ScanCount1 As Long
Dim PLC As New EntQsPlc_Asc.PlcClient'EntQsPlc_ASC.DLL是Q系列以太网通讯组件
Public Declare Function timeGetTime Lib "winmm.dll" () As UInt32
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.CenterToScreen()
cmbReadMry.Items.Clear()
cmbReadMry.Items.Add("X")
cmbReadMry.Items.Add("Y")
cmbReadMry.Items.Add("M")
cmbReadMry.Items.Add("D")
cmbReadMry.Items.Add("R")
cmbReadMry.Items.Add("Z")
'
cmbWriteMry.Items.Clear()
cmbWriteMry.Items.Add("X")
cmbWriteMry.Items.Add("Y")
cmbWriteMry.Items.Add("M")
cmbWriteMry.Items.Add("D")
cmbWriteMry.Items.Add("R")
cmbWriteMry.Items.Add("Z")
'
cmbBitMry.Items.Clear()
cmbBitMry.Items.Add("X")
cmbBitMry.Items.Add("Y")
cmbBitMry.Items.Add("M")
'
cmbReadType.Items.Clear()
cmbReadType.Items.Add("INT16")
cmbReadType.Items.Add("UINT16")
cmbReadType.Items.Add("DINT32")
cmbReadType.Items.Add("HEX32")
cmbReadType.Items.Add("REAL32")
cmbReadType.Items.Add("BIN16")
'
cmbWriteType.Items.Clear()
cmbWriteType.Items.Add("INT16")
cmbWriteType.Items.Add("UINT16")
cmbWriteType.Items.Add("DINT32")
cmbWriteType.Items.Add("HEX32")
cmbWriteType.Items.Add("REAL32")
cmbWriteType.Items.Add("BIN16")
cmbReadMry.SelectedIndex = 3
cmbWriteMry.SelectedIndex = 3
cmbBitMry.SelectedIndex = 2
cmbReadType.SelectedIndex = 0
cmbWriteType.SelectedIndex = 0
lstRead.Items.Clear()
txtWrite.Text = ""
'
cmbCmdType.SelectedIndex = 0
End Sub
Private Sub butLink_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butLink.Click
Dim re As Short
Dim restr As String = ""
re = PLC.EntLink(Trim(txtLocalIP.Text), Val(txtLocalPort.Text), Trim(txtRemoteIP.Text), Val(txtRemotePort.Text), "DEMO", Handle1, 1000, CBool(cmbCmdType.SelectedIndex))
txtReLink.Text = re.ToString
If re = 0 Then
EntLink1 = True
MsgBox("PLC联接成功! ")
Else
EntLink1 = False
MsgBox("PLC联接失败: " restr)
End If
End Sub
Private Sub butClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butClose.Click
Dim re As Short
If Not EntLink1 Then
MsgBox("还未与PLC建立联接!")
Exit Sub
End If
re = PLC.DeLink(Handle1)
txtReClose.Text = re.ToString
End Sub
Private Sub butRead_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butRead.Click
Dim re As Short
Dim i As Short
Dim RD() As Object
ReDim RD(Val(txtReadCnt.Text - 1))
If Not EntLink1 Then
MsgBox("还未与PLC建立联接!")
' Exit Sub
End If
Dim mry As EntQsPlc_Asc.PlcClient.PlcMemory = cmbReadMry.SelectedIndex + 1
Dim typ As EntQsPlc_Asc.PlcClient.DataType = cmbReadType.SelectedIndex + 1
re = PLC.CmdRead(Handle1, mry, typ, CInt(txtReadAdd.Text), CUShort(txtReadCnt.Text), RD)
txtReRead.Text = re.ToString
lstRead.Items.Clear()
For i = 0 To UBound(RD) Step 1
If Not IsNothing(RD(i)) Then lstRead.Items.Add(RD(i))
Next i
If re 0 Then
Timer1.Enabled = False
butScan.Text = "Cycle R/W"
End If
End Sub
Private Sub butWrite_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butWrite.Click
Dim re As Short
Dim i As Short
Dim temp() As String
Dim WD() As Object
If Not EntLink1 Then
MsgBox("还未与PLC建立联接!")
Exit Sub
End If
ReDim WD(Val(txtWriteCnt.Text) - 1)
temp = Split(txtWrite.Text, vbCrLf)
For i = 0 To UBound(WD) Step 1
If i UBound(temp) Then
WD(i) = 0
Else
WD(i) = Trim(temp(i))
End If
Next i
Dim mry As EntQsPlc_Asc.PlcClient.PlcMemory = cmbWriteMry.SelectedIndex + 1
Dim typ As EntQsPlc_Asc.PlcClient.DataType = cmbWriteType.SelectedIndex + 1
re = PLC.CmdWrite(Handle1, mry, typ, CInt(txtWriteAdd.Text), CUShort(txtWriteCnt.Text), WD)
txtReWrite.Text = re.ToString
If re 0 Then
Timer1.Enabled = False
butScan.Text = "Cycle R/W"
End If
End Sub
Private Sub butScan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butScan.Click
If Not EntLink1 Then
MsgBox("还未与PLC建立联接!")
Exit Sub
End If
Timer1.Enabled = Not Timer1.Enabled
If Timer1.Enabled Then
ScanCount1 = 0
butScan.Text = "Stop R/W"
Else
butScan.Text = "Cycle R/W"
End If
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Timer1.Enabled = False
Dim tim As Integer = timeGetTime
If Not EntLink1 Then
MsgBox("还未与PLC建立联接!")
Exit Sub
End If
'
Call butRead_Click(Nothing, Nothing)
Call butWrite_Click(Nothing, Nothing)
'
If (Val(txtReRead.Text) 0) Or (Val(txtReWrite.Text) 0) Then
butScan.Text = "Cycle R/W"
Exit Sub
Else
ScanCount1 += 1
txtScanCnt.Text = ScanCount1
txtScanPrd.Text = (timeGetTime - tim) "ms"
End If
Timer1.Enabled = True
End Sub
Private Sub butBitTest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butBitTest.Click
If Not EntLink1 Then
MsgBox("还未与PLC建立联接!")
Exit Sub
End If
Dim rd As Boolean
Dim re As Short
Dim mry As EntQsPlc_Asc.PlcClient.PlcMemory = cmbBitMry.SelectedIndex + 1
re = PLC.Bit_Test(Handle1, mry, CUShort(txtBitAdd.Text), rd)
txtBitTest.Text = rd
txtReBit.Text = re
End Sub
Private Sub butBitSet_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butBitSet.Click
If Not EntLink1 Then
MsgBox("还未与PLC建立联接!")
Exit Sub
End If
Dim re As Short
re = PLC.Bit_Set(Handle1, cmbBitMry.SelectedIndex + 1, Val(txtBitAdd.Text))
txtReBit.Text = re
End Sub
Private Sub butBitRst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butBitRst.Click
If Not EntLink1 Then
MsgBox("还未与PLC建立联接!")
Exit Sub
End If
Dim re As Short
re = PLC.Bit_Reset(Handle1, cmbBitMry.SelectedIndex + 1, Val(txtBitAdd.Text))
txtReBit.Text = re
End Sub
End Class
怎样在vb中自动检测到可用串口号的具体程序代码
j = 0
For i = 1 To 16 Step 1
If comISP.PortOpen = True Then '先关闭串口
comISP.PortOpen = False
End If
comISP.CommPort = i
On Error Resume Next '说明当一个运行时错误发生时,控件转到紧接着发生错误的语句之后的语句,并在此继续运行。访问对象时要使用这种形式而不使用 On Error GoTo。
comISP.PortOpen = True
If Err.Number 8002 Then '无效的串口号。这样可以检测到虚拟串口,如果用Err.Number = 0的话检测不到虚拟串口
If j = 0 Then
j = i
End If
cboPort.AddItem "COM" i '生成串口选择列表
End If
comISP.PortOpen = False
Next i
If j = 1 Then
cboPort.Text = "COM" j '自动打开可用的最小串口号
comISP.CommPort = j
comISP.PortOpen = True
cmdOpenCom.Caption = "关闭串口"
shpCOM.FillColor = vbGreen
If Err.Number = 8005 Then '串口已打开,vbExclamation '
comISP.PortOpen = False
cboPort.Text = ""
cmdOpenCom.Caption = "打开串口"
shpCOM.FillColor = vbRed
End If
End If
vb.net2010十六进制读取串口的问题
不是很明白你的题意
strHex = strHex + [String].Format("{0:X2} "
这里的意思是把每个字节数据转换成了十六进制,每个字节占两个字符
如果你串口收到的4个字节数据:43,27,56,200
那么你的结果是:2B1B38C8
即receivebytes.Text="2B1B38C8"
不足两位的补0
如果你串口收到的4个字节数据:3,27,56,200
那么你的结果是:031B38C8
即receivebytes.Text="031B38C8"
如何用vb.net获得网页的源代码
Dim url As String=" 网址"
Dim httpReq As System.Net.HttpWebRequest
Dim httpResp As System.Net.HttpWebResponse
Dim httpURL As New System.Uri(url)
httpReq = CType(WebRequest.Create(httpURL), HttpWebRequest)
httpReq.Method = "GET"
httpResp = CType(httpReq.GetResponse(), HttpWebResponse)
httpReq.KeepAlive = False ' 获取或设置一个值,该值指示是否与
Internet资源建立持久连接。
Dim reader As StreamReader = _
New StreamReader(httpResp.GetResponseStream,
System.Text.Encoding.GetEncoding(-0))
Dim respHTML As String = reader.ReadToEnd() 'respHTML就是网页源代码
分享题目:vb.net串口源码 vb串口程序
文章转载:http://myzitong.com/article/doejehe.html