| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 623 人关注过本帖
标题:串口通信问题
只看楼主 加入收藏
GLAIFLN
Rank: 1
等 级:新手上路
帖 子:26
专家分:0
注 册:2007-1-8
收藏
 问题点数:0 回复次数:2 
串口通信问题
我最近用VB6.0与单片机进行串口通信,计算机发送的数据单片机可以收到,而单片机发送的数据计算机无法收到,用串口调试助手测试单片机发送的数据可以正常收到,问题就出在计算机这边。我要把单片机发送的数据在计算机上用文本框显示成二进制或16进制数,看MSCOMM控件有许多项参数,像我这样的情况,需要用什么的属性。哪位可以给编写个事例呀?现在在郁闷中!!!!谢谢
搜索更多相关主题的帖子: 串口 通信 
2007-10-15 16:17
simpson
Rank: 3Rank: 3
等 级:论坛游民
威 望:7
帖 子:863
专家分:17
注 册:2006-11-16
收藏
得分:0 

给你个 ,自己慢慢琢磨,

Dim i As Integer
Dim SMBusData(5) As Byte
Dim inbytes() As Byte
Dim MS_Data As Double
Dim LS_Data As Double

ReceiveDataWord = 0

SMBusData(0) = &HAA
SMBusData(1) = &H50
SMBusData(2) = SubCmd
SMBusData(3) = &H0
SMBusData(4) = &H0

For i = 1 To 5
MSComm1.Output = SMBusData

ChkInBuffCountCOM1 (1)

If MSComm1.InBufferCount <> 5 Then
Return = 1
Exit Function
End If

MSComm1.InputLen = 5

inbytes = MSComm1.Input


If inbytes(1) = &H80 And inbytes(2) = &H3 Then
Return= 1
Else
Return = 0
LS_Data = inbytes(3)
Exit For
End If
Next i


SMBusData(0) = &HAA
SMBusData(1) = &H50
SMBusData(2) = SubCmd + 1 ' NEXT ADDRESS
SMBusData(3) = &H0
SMBusData(4) = &H0

For i = 1 To 5
MSComm1.Output = SMBusData

ChkInBuffCountCOM1 (1)

If MSComm1.InBufferCount <> 5 Then
Return = 1
Exit For
End If

MSComm1.InputLen = 5

inbytes = MSComm1.Input


If inbytes(1) = &H80 And inbytes(2) = &H3 Then
Return = 1
Else
Return = 0
MS_Data = inbytes(3)
ReceiveDataWord = MS_Data * &H100& + LS_Data
Exit For
End If
Next i

Public Function ChkInBuffCountCOM1(Length As Long)
Dim i As Integer
Dim FinishTime As Double

FinishTime = GetTickCount + 500 ' unit ms
MSComm1.InBufferCount = 0 ' single step execute can't run this line
Do
DoEvents
Loop Until MSComm1.InBufferCount >= Length Or GetTickCount > FinishTime

If GetTickCount > FinishTime Then
CanNotReadFlag = True
If CorePackFlag = True Then
MsgBox ("can't Read() , Error Code = C201")
Else
MsgBox ("can't Read() , Error Code = F201")
End If
End If

End Function


全国最大的 Java专业电子书免费分享[url]http:///in.asp?id=xrmao[/url]
2007-10-15 20:26
lanshanlhy
Rank: 1
等 级:新手上路
帖 子:130
专家分:0
注 册:2006-4-16
收藏
得分:0 
我给个简单的:(是数据接收哦)
Private Sub MSComm1_OnComm()
On Error Resume Next

Dim a() As Byte
Dim strBuff As String
Dim StrData As String
Dim i As Integer

Select Case MSComm1.CommEvent
Case 1
Case 2

MSComm1.InputLen = 0
strBuff = MSComm1.Input
a() = strBuff
For i = 0 To UBound(a)
If Len(Hex(a(i))) = 1 Then
StrData = StrData & "0" & Hex(a(i)) & ","
Else
StrData = StrData & Hex(a(i)) & ","
End If
Next
Text1 = Text1 + StrData

Case 3
Case 4
Case 5
Case 6
Case 7
Case Default

End Select
End Sub

永远以今天为荣!
2007-10-27 10:11
快速回复:串口通信问题
数据加载中...
 
   



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

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