VB6.0与西门子1200TCP通信

2025-10-03 20:40:07

1、打开博图软件,点击设备添加1214C CPU。双击网口,弹出属性对话框,设置以太网IP地址

VB6.0与西门子1200TCP通信

2、点击程序块,在Main程序里添加开放式通信指令块

1、TCON(建立通信连接)

2、TSEND(通过现有的通信发送数据)

3、TRCV(通过现有的通信接收数据)

VB6.0与西门子1200TCP通信

3、添加TCON(建立通信连接)指令块,用鼠标点击后拖放到程序段里。

VB6.0与西门子1200TCP通信

4、TCON属性设置,点击指令块右上角开始组态图标弹出属性对话框。属性框的连接参数设置,1、指定连接类型为TCP

2、伙伴为主动连接(客户端),点击连接的数据块建立一个数据块

3、本地的端口设备(1200默认为2000,没作修改)

4、添加建立通信连接的条件。此处用的是常通条件。

5、连接的数据块,填写属性设置里自动建立的数据块

VB6.0与西门子1200TCP通信

VB6.0与西门子1200TCP通信

5、添加TRCV(通过现有的通信接收数据)指令块。注意2点,

1、接收数据的条件 

2、接到的数据存放的数据块地址(新建一个新的数据块)

3、修改数据的类型及接收数据的长度

VB6.0与西门子1200TCP通信

VB6.0与西门子1200TCP通信

6、添加TSEND(通过现有的通信发送数据)

1、发送数据的条件 

2、发送数据的数据块地址(此处用的是接收的数据块)

VB6.0与西门子1200TCP通信

7、添加 输出程序做测试。

VB6.0与西门子1200TCP通信

8、VB6.0侧程序

1、新建 一个新的程序

2、把窗口的名称修改成frmClient

3、点击菜单栏里的工程|部件|microsoft winsock control 5.0.点击它 按放在form窗体上就可以了

4、添加2个文本框  Text1 和 Text2 和 两个Labe 分别改为服务器IP和端口号

VB6.0与西门子1200TCP通信

VB6.0与西门子1200TCP通信

9、1、添加发送数据文本框改名为txtSend

2、添加接收数据文本框改名为txtOutput

3、添加 发送 Command1和 清空 按钮Command2

4、添加图形符号作指示灯Shape4 -Shape11

5、添加8个按钮改名为button1-button8

6、添加一个连接按钮改名为cmdConnect

7、添加一个退出按钮Command5

VB6.0与西门子1200TCP通信

10、程序分析

Private Sub button1_Click()   '按钮1-8程序

txtSend.Text = "10000000"

tcpClient.SendData txtSend.Text

End Sub

Private Sub button2_Click()

txtSend.Text = "01000000"

tcpClient.SendData txtSend.Text

End Sub

Private Sub button3_Click()

txtSend.Text = "00100000"

tcpClient.SendData txtSend.Text

End Sub

Private Sub button4_Click()

txtSend.Text = "00010000"

tcpClient.SendData txtSend.Text

End Sub

Private Sub button5_Click()

txtSend.Text = "00001000"

tcpClient.SendData txtSend.Text

End Sub

Private Sub button6_Click()

txtSend.Text = "00000100"

tcpClient.SendData txtSend.Text

End Sub

Private Sub button7_Click()

txtSend.Text = "00000010"

tcpClient.SendData txtSend.Text

End Sub

Private Sub button8_Click()

txtSend.Text = "00000001"

tcpClient.SendData txtSend.Text

End Sub

Private Sub Command1_Click()   ‘按下发送按钮 把 txtSend的内容发出

'

tcpClient.SendData txtSend.Text

txtSend.Text = ""

End Sub

Private Sub Command2_Click()  ‘按下清空按钮 把 txtSend的内容清空

txtOutput = ""

End Sub

Private Sub cmdConnect_Click()  '按下连接按钮 

tcpClient.RemoteHost = Text1.Text    '读取Text1内的IP。

tcpClient.RemotePort = Val(Text2.Text)  '读取Text2内的端口号。

tcpClient.Connect

Label6.Caption = "链接打开"

Label6.FontSize = 12

Label6.ForeColor = vbBlue

cmdConnect.Enabled = False

Open "e:\TCP\ip.txt" For Output As #1   '按下连接按钮后把IP存到e:\TCP\ip.txt的文件夹内

Print #1, Text1.Text

Close #1

End Sub

Private Sub Command5_Click()   '按下退出按钮  退出程序

Unload Me

End Sub

Private Sub tcpClient_DataArrival _    '数据接收

(ByVal bytesTotal As Long)

Dim strData As String

tcpClient.GetData strData

txtOutput.Text = strData + vbCrLf

End Sub

Private Sub Form_Unload(Cancel As Integer)   '关闭窗口时先关闭socket

    tcpClient.Close

End Sub

 Private Sub Form_load()    '窗口打开把e:\TCP\ip.txt文件内的IP给Text1

    If Text1.Text = "" Then

     Open "e:\TCP\ip.txt" For Input As #1

Text1.Text = Input(LOF(1), 1)

Close #1

    End If

    

End Sub

Private Sub txtOutput_Change()   '把接收到的数据 拆分 对应该8个指示灯

Dim x1 As Long

Dim y1, y2, y3, y4, y5, y6, y7, y8 As Long

If txtOutput.Text <> "" Then

x1 = txtOutput.Text

Else

x1 = 0

End If

y1 = x1 \ 10000000

y2 = (x1 Mod 10000000) \ 1000000

y3 = ((x1 Mod 10000000) Mod 1000000) \ 100000

y4 = (((x1 Mod 10000000) Mod 1000000) Mod 100000) \ 10000

y5 = ((((x1 Mod 10000000) Mod 1000000) Mod 100000) Mod 10000) \ 1000

y6 = (((((x1 Mod 10000000) Mod 1000000) Mod 100000) Mod 10000) Mod 1000) \ 100

y7 = ((((((x1 Mod 10000000) Mod 1000000) Mod 100000) Mod 10000) Mod 1000) Mod 100) \ 10

y8 = ((((((x1 Mod 10000000) Mod 1000000) Mod 100000) Mod 10000) Mod 1000) Mod 100) Mod 10

If y1 = 1 Then  '指示灯的状态改变

Shape4.FillColor = vbGreen

button1.BackColor = vbGreen

Else

Shape4.FillColor = vbWhite

button1.BackColor = vbWhite

End If

If y2 = 1 Then

Shape5.FillColor = vbGreen

button2.BackColor = vbGreen

Else

Shape5.FillColor = vbWhite

button2.BackColor = vbWhite

End If

If y3 = 1 Then

Shape6.FillColor = vbGreen

button3.BackColor = vbGreen

Else

Shape6.FillColor = vbWhite

button3.BackColor = vbWhite

End If

If y4 = 1 Then

Shape7.FillColor = vbGreen

button4.BackColor = vbGreen

Else

Shape7.FillColor = vbWhite

button4.BackColor = vbWhite

End If

If y5 = 1 Then

Shape8.FillColor = vbGreen

button5.BackColor = vbGreen

Else

Shape8.FillColor = vbWhite

button5.BackColor = vbWhite

End If

If y6 = 1 Then

Shape9.FillColor = vbGreen

button6.BackColor = vbGreen

Else

Shape9.FillColor = vbWhite

button6.BackColor = vbWhite

End If

If y7 = 1 Then

Shape10.FillColor = vbGreen

button7.BackColor = vbGreen

Else

Shape10.FillColor = vbWhite

button7.BackColor = vbWhite

End If

If y8 = 1 Then

Shape11.FillColor = vbGreen

button8.BackColor = vbGreen

Else

Shape11.FillColor = vbWhite

button8.BackColor = vbWhite

End If

End Sub

Private Sub Command3_Click()    ' 把文件夹内的IP读到Text3中

 Open "e:\TCP\ip.txt" For Input As #1

Text3.Text = Input(LOF(1), 1)

'Print #1, ""

Close #1

Dim A

A = Split(Text3.Text)

End Sub

Private Sub Command4_Click()  ' 读多行数据放到list中

Dim SS$()     

Dim A$, i%

List1.Clear

Open "e:\test\testdata.txt" For Input As #1

While Not EOF(1)

Line Input #1, A$ '可以考虑行读入

List1.AddItem A$

Wend

Close

ReDim SS(List1.ListCount)

For i = 0 To List1.ListCount - 1

SS(i) = List1.List(i)

Next

End Sub

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢