用VB.NET做串口通讯一个问题
VB.NET串口通讯的时候怎么让电脑A中textbox1的内容显示到电脑B的textbox1;电脑A中textbox2的内容显示到电脑B的textbox2中!我用的是se...
VB.NET串口通讯的时候怎么让电脑A中textbox1的内容显示到电脑B的textbox1;电脑A中textbox2的内容显示到电脑B的textbox2中!
我用的是serialport2 控件
电脑A中的代码为:
Option Strict Off
Option Explicit On
Public Class Form8fasong
Private Sub Form8fasongtongzhi_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.FormClosed
SerialPort1.Close()
SerialPort2.Close()
End Sub
Private Sub Form8fasongtongzhi_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Label3.Text = Now.Date
'
SerialPort1.BaudRate = "9600" '波特率
SerialPort1.PortName = "com1" '串口名称
SerialPort1.DataBits = 8 '数据位
SerialPort1.StopBits = IO.Ports.StopBits.One '停止位
SerialPort1.Parity = IO.Ports.Parity.None '校验位
SerialPort1.Open() '打开串口
'
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim pp As Object
If TextBox2.Text = "" Then
MsgBox("输入内容不能为空!")
Exit Sub
End If
SerialPort1.Write(Trim(TextBox2.Text))
Timer1.Enabled = True
End Sub
End Class
电脑B中的内容为:
Option Strict Off
Option Explicit On
Public Class Form8fasong
Private Sub Form8fasongtongzhi_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.FormClosed
SerialPort1.Close()
SerialPort2.Close()
End Sub
Private Sub Form8fasongtongzhi_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'
SerialPort2.BaudRate = "9600" '波特率
SerialPort2.PortName = "com2" '串口名称
SerialPort2.DataBits = 8 '数据位
SerialPort2.StopBits = IO.Ports.StopBits.One '停止位
SerialPort2.Parity = IO.Ports.Parity.None '校验位
SerialPort2.Open() '打开串口
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim buf As String
buf = SerialPort2.ReadExisting.ToString
If Len(buf) <> 0 Then
TextBox1.Text = TextBox1.Text & buf
End If
End Sub
End Sub
End Class 展开
我用的是serialport2 控件
电脑A中的代码为:
Option Strict Off
Option Explicit On
Public Class Form8fasong
Private Sub Form8fasongtongzhi_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.FormClosed
SerialPort1.Close()
SerialPort2.Close()
End Sub
Private Sub Form8fasongtongzhi_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Label3.Text = Now.Date
'
SerialPort1.BaudRate = "9600" '波特率
SerialPort1.PortName = "com1" '串口名称
SerialPort1.DataBits = 8 '数据位
SerialPort1.StopBits = IO.Ports.StopBits.One '停止位
SerialPort1.Parity = IO.Ports.Parity.None '校验位
SerialPort1.Open() '打开串口
'
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim pp As Object
If TextBox2.Text = "" Then
MsgBox("输入内容不能为空!")
Exit Sub
End If
SerialPort1.Write(Trim(TextBox2.Text))
Timer1.Enabled = True
End Sub
End Class
电脑B中的内容为:
Option Strict Off
Option Explicit On
Public Class Form8fasong
Private Sub Form8fasongtongzhi_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.FormClosed
SerialPort1.Close()
SerialPort2.Close()
End Sub
Private Sub Form8fasongtongzhi_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'
SerialPort2.BaudRate = "9600" '波特率
SerialPort2.PortName = "com2" '串口名称
SerialPort2.DataBits = 8 '数据位
SerialPort2.StopBits = IO.Ports.StopBits.One '停止位
SerialPort2.Parity = IO.Ports.Parity.None '校验位
SerialPort2.Open() '打开串口
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim buf As String
buf = SerialPort2.ReadExisting.ToString
If Len(buf) <> 0 Then
TextBox1.Text = TextBox1.Text & buf
End If
End Sub
End Sub
End Class 展开
2个回答
展开全部
那台电脑室com2? 还是com1啊。你看看关于通讯控件 接收不用时钟就行。用时钟不可以吧
追问
用时钟可以的 啊,我现在可以做到从电脑A发送数据电脑B接收到,但是不知道则么让电脑A中的textbox1中的内容发送到电脑B的textbox1中而电脑啊中TEXTBOX2中的内容发送到电脑B中!!
追答
我做的是通过485 与下位机通讯Private Sub SerialPort_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived接收
发送
给TEXT中数据 放到数组里
BytComSendData(0) = 1
BytComSendData(1) = &H3
BytComSendData(2) = &H1
BytComSendData(3) = &H20
BytComSendData(4) = &H0
BytComSendData(5) = &H2
CallCRC16Fast(BytComSendData, 6, btLCRC, btHCRC) '计算CRC值
BytComSendData(6) = btLCRC
BytComSendData(7) = btHCRC
f.SerialPort1.Write(BytComSendData, 0, 8)
2013-05-17
展开全部
硬件连接没有问题吧
2台电脑连接要交叉
A Tx 连 B Rx
A Rx 连 B Tx
如此一来,2边的程序可以完全一样
2台电脑连接要交叉
A Tx 连 B Rx
A Rx 连 B Tx
如此一来,2边的程序可以完全一样
追问
但是这不是串口通信啊,我主要要用到串口通讯!!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询