| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 762 人关注过本帖
标题:看看我写的串口通信的程序,有许多毛病??
只看楼主 加入收藏
chaoshuai
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2006-1-19
收藏
 问题点数:0 回复次数:4 
看看我写的串口通信的程序,有许多毛病??

我写的程序,MSCOMM控件。 带数据格式的。帮我修改以下,谢谢,

下面是原代码:
Private Sub CommonConnect_Click()
'打开串口
Select Case ListCOM.ListIndex
Case 0
MSComm1.CommPort = 1
Case 1
MSComm1.CommPort = 2
Case 2
MSComm1.CommPort = 3
Case 3
MSComm1.CommPort = 4
End Select

If MSComm1.PortOpen = False Then
Select Case ListBate.ListIndex
Case 0
MSComm1.Settings = "1200,e,8,1"
Case 1
MSComm1.Settings = "2400,e,8,1"
Case 2
MSComm1.Settings = "4800,e,8,1"
Case 3
MSComm1.Settings = "9600,e,8,1"
End Select

MSComm1.PortOpen = True '打开串口
Else
MsgBox "串口已经打开"
End If

MSComm1.OutBufferCount = 0

'清空发送缓冲区

MSComm1.InBufferCount = 0

'{---清空接收缓冲区


CommonConnect.Visible = False
CloseCOM.Visible = True

End Sub
Private Sub CloseCOM_Click()
MSComm1.PortOpen = False
CommonConnect.Visible = True
CloseCOM.Visible = False
End Sub
Private Sub MSComm1_OnComm()

Select Case MSComm1.CommEvent

Case comEvReceive

'接收二进制数据

Dim Buffer As Variant

MSComm1.InputLen = 0

MSComm1.InputMode = comInputModeBinary

Buffer = MSComm1.Input

Case Else

End Select

End Sub

Private Sub Form_Load()
YFSkin1.SkinInit Me
End Sub

Private Sub NextNo_Click()
Dim dblTmp As Double
Dim I As Integer

If NextNo.Caption = "下一表号" Then
If Trim(MeterNO.Text) = "" Then
dblTmp = 0#
Else
dblTmp = CDbl(MeterNO.Text)
End If
If dblTmp = 999999999998# Then
MeterNO.Text = "000000000000"
Else
MeterNO.Text = Right("000000000000" & CStr(dblTmp + 1), 12)
End If
Else
NextNo.Caption = "下一表号"
End If
' strMeterAddr = MeterNO.Text
End Sub

Private Sub ReadData_Click()
Dim I As Integer, J As Integer, K As Integer, L As Integer, M As Integer, Dummy As Integer
Dim strTmp As String
Dim boolMeterNoChg As Boolean
Dim ArrayRoom() As Byte
ReDim ArrayRoom(7)
ArrayRoom(0) = &H68 '侦起始符
ArrayRoom(1) = MeterNO.Text '设备号码
ArrayRoom(2) = &H68 '侦起始符
ArrayRoom(3) = Control '控制码
ArrayRoom(4) = Length '数据长度
ArrayRoom(5) = Data '数据
ArrayRoom(6) = E '偶校验,应该怎么写啊??
ArrayRoom(7) = &H16 '结束符



If MSComm1.PortOpen = False Then
MsgBox "串口没有打开,请先打开串口!"
Exit Sub
Else
If Check1(0).Value = True Then
ArrayRoom(3) = &H1
ArrayRoom(4) = &H2
ArrayRoom(5) = &H9010
MSComm1.Output = ArrayRoom
End If
Text1(0).Text = Buffer '接收的数据反映在控件当中


End If
End Sub

搜索更多相关主题的帖子: 串口 Case 通信 毛病 
2006-05-15 12:30
stz81
Rank: 1
等 级:新手上路
帖 子:65
专家分:0
注 册:2005-3-2
收藏
得分:0 

可不可以把你最常用的联系方式说一下
我的信箱:stz81@163.com
QQ:175862083

[此贴子已经被作者于2006-5-15 14:55:34编辑过]

2006-05-15 14:49
luhaidong
Rank: 1
等 级:新手上路
帖 子:22
专家分:0
注 册:2006-3-21
收藏
得分:0 
你上面的问题真的是多,找我

QQ:58959352
2006-05-19 20:34
luhaidong
Rank: 1
等 级:新手上路
帖 子:22
专家分:0
注 册:2006-3-21
收藏
得分:0 

ArrayRoom(0) = &H68 '侦起始符
ArrayRoom(1) = MeterNO.Text '设备号码
ArrayRoom(2) = &H68 '侦起始符
ArrayRoom(3) = Control '控制码
ArrayRoom(4) = Length '数据长度
ArrayRoom(5) = Data '数据
ArrayRoom(6) = E '偶校验,应该怎么写啊??
ArrayRoom(7) = &H16 '结束符

这种方式你确定正确吗??
偶校验是封装在Mscomm控件中的,不需要赋值的,我估计是桢校验ArrayRoom(6) ,ArrayRoom(7)
别的地方太长了,程序

2006-05-19 20:39
chaoshuai
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2006-1-19
收藏
得分:0 

我的联系方式:QQ:66303352

2006-06-02 14:27
快速回复:看看我写的串口通信的程序,有许多毛病??
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.031378 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved