Private Sub Command1_Click()
End
End Sub
Private Sub Command2_Click()
Winsockserver.SendData Text4.Text + ":" + textsend.Text
textget.Text = textget.Text + vbCrLf + Text4.Text + ":" + textsend.Text + " " + Str(Time)
textsend.Text = ""
End Sub
Private Sub Form_Load()
Command2.Visible = False
textsend.Visible = False
Text4.Visible = False
textget.Visible = False
Label1.Visible = False
Winsockserver.LocalPort = 1001
Winsockserver.Listen
End Sub
Private Sub textsend_Change()
'Winsockserver.SendData textsend.text
End Sub
Private Sub textsend_KeyPress(KeyAscii As Integer)
'If KeyAscii = 13 Then
'Winsockserver.SendData textsend.text
'textsend.text = ""
If KeyAscii = 13 Then
Winsockserver.SendData textsend.Text
' Text1.text = Text1.text + vbCrLf + Text4.text + "拆枣: " + Text2.text + " " + Str(Time)
textsend.Text = ""
End If
End Sub
Private Sub Timer1_Timer()
If Text4.Text = "" Then Text4.Text = "no name"
End Sub
Private Sub Winsockserver_Close()
Winsockserver.Close
End
End Sub
Private Sub Winsockserver_ConnectionRequest(ByVal requestID As Long)
Command2.Visible = True
textsend.Visible = True
textget.Visible = True
Text4.Visible = True
Label1.Visible = True
If Winsockserver.State <>sckClosed Then Winsockserver.Close
Winsockserver.Accept requestID
End Sub
Private Sub Winsockserver_DataArrival(ByVal bytesTotal As Long)
Dim tmpstr As String
Winsockserver.GetData tmpstr
'textget.text = textget + tmpstr + textsend
textget.Text = textget.Text + vbCrLf + tmpstr + " " + Str(Time)
End Sub
'闹吵(1)Command1:退出按钮;
'(液御侍2)textsend:发送数据文本框;
'(3)Winsockserver: 服务器Winsock;
'(4)textget :接收数据文本框。
一个clien
VB用MSCOMM控件与单片机经RS232串口实现通讯。一方为主发,一方为应答。规定好通讯格式。VB程序利用MSCOMM的ONCOMM事件进行接收。Output
属性示例
下面的例子说明如何将用户键入的每一个字符送到串行端口:
Private
Sub
Form_KeyPress
(KeyAscii
As
Integer)
Dim
Buffer
as
Variant
'
设置并打开串口
MSComm1.CommPort
=
1
MSComm1.PortOpen
=
True
Buffer
=
Chr$(KeyAscii)
MSComm1.Output
=
Buffer
End
Sub
同样在串口打开时,给MSCOMM的属性Output赋值,就团告向串口写入数据.
串口传送数据实质是传送ASCII码,对于数值数据的处理:通常以16位二进制数字传递,而小数点不传递,仅乘一定倍率来实现.同时16位二进制数字按两个8位(ASCII码值)输送,上位机通过程序代码处理成10进制数.
串口通信中数制转换必须塌胡明相当重视。
详细请参阅MSDN和人民邮电出版社的"UISUAL
BASIC
串口通讯工程开发实例导航"一书.其中有详细介绍.重点做纯第一章“串口调试精灵”。其代码修改后可用作通讯模块。
data(0) = 1data(1) = 3
data(2) = 0
data(3) = 0
data(4) = 0
data(5) = 1
data(6) = 132
data(7) = 10 '需要发送16进制F0
MSComm1.Output = data()
1 串口缓冲区大小?是否影响你的程序正常察埋执行答派?
2 是否在某种条件陷入死循环?端口 *** 作方法在读写串口时,不能及时获取串口状态信息,对错误和异常的处理能力
弱,容易造成死机.
利用空间Tc o m m来编写串清没贺口通信程序.
终归要分析代码。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)