【上下位机通信】:下位机(STM32)通过网口(DM9000之类的.)向上位机PC发送数据。求上位机例子程序~
用 socket 类 udp tcp 我这 给你个 最简单的 例子 udp
服务器端
Imports System.Net.Sockets
Imports System.Net
Module socketsever
Sub main()
startserver()
End Sub
Private Sub startserver()
'创建服务器端的socket
Dim sServer As New Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)
Dim myip As IPAddress
'制订本地回路的ip地址
myip = IPAddress.Parse("10.13.115.105")
'设置服务器端口号
Dim sServerep As New IPEndPoint(myip, 2001)
'设置客户端的端口号
Dim clientep As New IPEndPoint(myip, 2002)
'将端口与socket绑定
sServer.Bind(sServerep)
While True
Try
Dim bytes(100) As Byte
'等待客户端的请求
sServer.ReceiveFrom(bytes, clientep)
Dim str As String = System.Text.Encoding.ASCII.GetString(bytes)
'将字符转化成大写
str = str.ToUpper
bytes = System.Text.Encoding.ASCII.GetBytes(str)
'返回结果
sServer.SendTo(bytes, clientep)
Catch e As SocketException
End Try
End While
End Sub
End Module
界面 和程序
Imports System.Net.Sockets
Imports System.Net
Public Class Form1
Inherits System.Windows.Forms.Form
Private Sub bsend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bsend.Click
'创建客户端的socket
Dim cClient As New Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)
'指定本地回路的ip地址
Dim myip, myip1 As IPAddress
myip = IPAddress.Parse("10.13.115.105")
myip1 = IPAddress.Parse("10.13.115.136")
'设置服务器端口号
Dim Serverep As New IPEndPoint(myip1, 2001)
'设置客户端的端口号
Dim clientep As New IPEndPoint(myip, 2002)
'将端口与socket绑定
cClient.Bind(clientep)
Try
Dim str As String = Textinput.Text
Dim bytes() As Byte = System.Text.Encoding.ASCII.GetBytes(str)
'向服务器发出请求
cClient.SendTo(bytes, Serverep)
Dim bytes2(100) As Byte
'接受服务器的响应结果
cClient.ReceiveFrom(bytes2, Serverep)
str = System.Text.Encoding.ASCII.GetString(bytes2)
'将结果显示在文本框中
Textdisplay.Text = Textdisplay.Text & vbNewLine & str
Catch er As SocketException
MsgBox(er.ToString)
End Try
cClient.Close()
End Sub
End Class