㈠ VB给单片机发数据问题
Option Explicit
Dim intTime As Integer
Private strSendText As String '发送文本数据
Private bytSendByte() As Byte '发送二进制数据
Private blnReceiveFlag As Boolean
Private blnAutoSendFlag As Boolean
Private intPort As Integer
Private strSet As String
Private intReceiveLen As Integer
Private bytReceiveByte() As Byte
Private strAscii As String '设置初值
Private strHex As String
Private intHexWidth As Integer
Private intLine As Integer
Private m As Integer
Private strAddress As String
'字符表示的十六进制数转化为相应的整数,错误则返回 -1
Function ConvertHexChr(str As String) As Integer
Dim test As Integer
test = Asc(str)
If test >= Asc("0") And test <= Asc("9") Then
test = test - Asc("0")
ElseIf test >= Asc("a") And test <= Asc("f") Then
test = test - Asc("a") + 10
ElseIf test >= Asc("A") And test <= Asc("F") Then
test = test - Asc("A") + 10
Else
test = -1 '出错信息
End If
ConvertHexChr = test
End Function
'字符串表示的十六进制数据转化为相应的字节串,返回转化后的字节数
Function strHexToByteArray(strText As String, bytByte() As Byte) As Integer
Dim HexData As Integer '十六进制(二进制)数据字节对应值
Dim hstr As String * 1 '高位字符
Dim lstr As String * 1 '低位字符
Dim HighHexData As Integer '高位数值
Dim LowHexData As Integer '低位数值
Dim HexDataLen As Integer '字节数
Dim StringLen As Integer '字符串长度
Dim Account As Integer
Dim n As Integer
'计数
'txtSend = "" '设初值
HexDataLen = 0
strHexToByteArray = 0
StringLen = Len(strText)
Account = StringLen \ 2
ReDim bytByte(Account)
For n = 1 To StringLen
Do '清除空格
hstr = Mid(strText, n, 1)
n = n + 1
If (n - 1) > StringLen Then
HexDataLen = HexDataLen - 1
Exit For
End If
Loop While hstr = " "
Do
lstr = Mid(strText, n, 1)
n = n + 1
If (n - 1) > StringLen Then
HexDataLen = HexDataLen - 1
Exit For
End If
Loop While lstr = " "
n = n - 1
If n > StringLen Then
HexDataLen = HexDataLen - 1
Exit For
End If
HighHexData = ConvertHexChr(hstr)
LowHexData = ConvertHexChr(lstr)
If HighHexData = -1 Or LowHexData = -1 Then '遇到非法字符中断转化
HexDataLen = HexDataLen - 1
Exit For
Else
HexData = HighHexData * 16 + LowHexData
bytByte(HexDataLen) = HexData
HexDataLen = HexDataLen + 1
End If
Next n
If HexDataLen > 0 Then '修正最后一次循环改变的数值
HexDataLen = HexDataLen - 1
ReDim Preserve bytByte(HexDataLen)
Else
ReDim Preserve bytByte(0)
End If
If StringLen = 0 Then '如果是空串,则不会进入循环体
strHexToByteArray = 0
Else
strHexToByteArray = HexDataLen + 1
End If
End Function
Private Sub cmdManualSend_Click()
If Not Me.MSComm.PortOpen Then
Me.MSComm.CommPort = intPort
Me.MSComm.Settings = strSet
Me.MSComm.PortOpen = True
End If
Call ctrTimer_Timer
If Not blnAutoSendFlag Then
Me.MSComm.PortOpen = False
End If
End Sub
Private Sub cmdAutoSend_Click()
If blnAutoSendFlag Then
Me.ctrTimer.Enabled = False
If Not blnReceiveFlag Then
Me.MSComm.PortOpen = False
End If
Me.cmdAutoSend.Caption = "自动发送"
Else
If Not Me.MSComm.PortOpen Then
Me.MSComm.CommPort = intPort
Me.MSComm.Settings = strSet
Me.MSComm.PortOpen = True
End If
Me.ctrTimer.Interval = intTime
Me.ctrTimer.Enabled = True
Me.cmdAutoSend.Caption = "停止发送"
End If
blnAutoSendFlag = Not blnAutoSendFlag
End Sub
Private Sub ctrTimer_Timer()
Dim longth As Integer
strSendText = Me.txtSend.Text
longth = strHexToByteArray(strSendText, bytSendByte())
If longth > 0 Then
Me.MSComm.Output = bytSendByte
End If
End Sub
'输入处理,处理接收到的字节流,并保存在全局变量
Private Sub InputManage(bytInput() As Byte, intInputLenth As Integer)
Dim n As Integer '定义变量及初始化
ReDim Preserve bytReceiveByte(intReceiveLen + intInputLenth)
For n = 1 To intInputLenth Step 1
bytReceiveByte(intReceiveLen + n - 1) = bytInput(n - 1)
Next n
intReceiveLen = intReceiveLen + intInputLenth
End Sub
'为输出准备文本,保存在全局变量
'总行数保存在intLine
Public Sub GetDisplayText()
Dim n As Integer
Dim intValue As Integer
Dim intHighHex As Integer
Dim intLowHex As Integer
Dim strSingleChr As String * 1
Dim intAddress As Integer
Dim intAddressArray(8) As Integer
Dim intHighAddress As Integer
strAscii = "" '设置初值
strHex = ""
strAddress = ""
'获得16进制码和ASCII码的字符串
For n = 1 To intReceiveLen
intValue = bytReceiveByte(n - 1)
If intValue < 32 Or intValue > 128 Then '处理非法字符
strSingleChr = Chr(46) '对于不能显示的ASCII码,
Else '用"."表示
strSingleChr = Chr(intValue)
End If
strAscii = strAscii + strSingleChr
intHighHex = intValue \ 16
intLowHex = intValue - intHighHex * 16
If intHighHex < 10 Then
intHighHex = intHighHex + 48
Else
intHighHex = intHighHex + 55
End If
If intLowHex < 10 Then
intLowHex = intLowHex + 48
Else
intLowHex = intLowHex + 55
End If
strHex = strHex + Chr$(intHighHex) + Chr$(intLowHex) + " "
If (n Mod intHexWidth) = 0 Then
strAscii = strAscii + Chr$(13) + Chr$(10)
strHex = strHex + Chr$(13) + Chr$(10)
Else
End If
Next n
txtAsc = strAscii 'Ascii
txtHex = strHex '16进制
'获得地址字符串
intLine = intReceiveLen \ intHexWidth
If (intReceiveLen - intHexWidth * intLine) > 0 Then
intLine = intLine + 1
End If
'设置换行
For n = 1 To intLine
intAddress = (n - 1) * intHexWidth
intHighAddress = 8
intAddressArray(0) = intAddress
For m = 1 To intHighAddress
intAddressArray(m) = intAddressArray(m - 1) \ 16
Next m
For m = 1 To intHighAddress
intAddressArray(m - 1) = intAddressArray(m - 1) - intAddressArray(m) * 16
Next m
For m = 1 To intHighAddress
If intAddressArray(intHighAddress - m) < 10 Then
intAddressArray(intHighAddress - m) = intAddressArray(intHighAddress - m) + Asc("0")
Else
intAddressArray(intHighAddress - m) = intAddressArray(intHighAddress - m) + Asc("A") - 10
End If
strAddress = strAddress + Chr$(intAddressArray(intHighAddress - m))
Next m
strAddress = strAddress + Chr$(13) + Chr$(10)
Next n
txtAdd = strAddress '地址
End Sub
Private Sub cmdReceive_Click()
If blnReceiveFlag Then
If Not blnReceiveFlag Then
Me.MSComm.PortOpen = False
End If
Me.cmdReceive.Caption = "开始接收"
Else
If Not Me.MSComm.PortOpen Then
Me.MSComm.CommPort = intPort
Me.MSComm.Settings = strSet
Me.MSComm.PortOpen = True
End If
Me.MSComm.InputLen = 0
Me.MSComm.InputMode = 0
Me.MSComm.InBufferCount = 0
Me.MSComm.RThreshold = 1
Me.cmdReceive.Caption = "停止接收"
End If
blnReceiveFlag = Not blnReceiveFlag
End Sub
Private Sub Form_Load()
intHexWidth = 8
txtAdd = ""
txtHex = ""
txtAsc = ""
txtSend = "11"
txtAdd.Width = 1335
txtHex.Width = 2535
txtAsc.Width = 1215
'设置默认发送接收关闭状态
blnAutoSendFlag = False
blnReceiveFlag = False
'接收初始化
intReceiveLen = 0
'默认发送方式为16进制
'intOutMode = 1
'初始化串行口
intPort = 1
intTime = 1000
strSet = "9600,n,8,1"
Me.MSComm.InBufferSize = 1024
Me.MSComm.OutBufferSize = 512
If Not Me.MSComm.PortOpen Then
Me.MSComm.CommPort = intPort
Me.MSComm.Settings = strSet
Me.MSComm.PortOpen = True
End If
Me.MSComm.PortOpen = False
End Sub
Private Sub cmdClear_Click()
Dim bytTemp(0) As Byte
ReDim bytReceiveByte(0)
intReceiveLen = 0
Call InputManage(bytTemp, 0)
Call GetDisplayText
Call disPlay
End Sub
Private Sub MsComm_OnComm()
Dim bytInput() As Byte
Dim intInputLen As Integer
Select Case Me.MSComm.CommEvent
Case comEvReceive
If blnReceiveFlag Then
If Not Me.MSComm.PortOpen Then
Me.MSComm.CommPort = intPort
Me.MSComm.Settings = strSet
Me.MSComm.PortOpen = True
End If
'此处添加处理接收的代码
Me.MSComm.InputMode = comInputModeBinary '二进制接收
intInputLen = Me.MSComm.InBufferCount
ReDim bytInput(intInputLen)
bytInput = Me.MSComm.Input
Call InputManage(bytInput, intInputLen)
Call GetDisplayText
'Call disPlay
If Not blnReceiveFlag Then
Me.MSComm.PortOpen = False
End If
End If
End Select
End Sub
Private Sub disPlay()
txtHex = ""
txtAsc = ""
txtAdd = ""
End Sub
㈡ 我通过VB通信把PC中的数据发送给单片机
用MSCOMM控件收发,发送前把发送类型改成2进制格式,发送字符也用byte格式,定义成数组格式,接收也使用这种格式,但是数组要设定成特殊格式。
用定时器定时调用也可,手工发送也可,主要是发送格式正确就行了,关键是发送前制定好通讯协议,确保数据全部能够发送过去,最好有校验。
有问题HI上找我。
㈢ VB可以直接发送字模数组到单片机吗
当然可以。。MSCOMM控件可以实现电脑与单片机之间串口通信,前题是你单片机的软件要设计好
单片机可以接收数组存入片外存储器。这些都是可以实现的,硬件来说没问题,看你软件怎么设计了
㈣ VB和单片机进行串口通信 通过VB界面两个简单的按钮 发送两个数据给单片机
做一个整形到ASCII的转换函数。如果你要发送的数据最大值可以确定,可以根据最大值定义一个ASCII数组,数组的每一个单元存放整形数据的一位。
发送前先对发送整形数组里的单元转换成ASCII数组,然后再按照通用的发送函数进行发送。
void InttoChar (uint IntNumber)
//---------------------------------------------------------
// Name: void InttoChar (int IntNumber)
// Func.: Translate integer to ASCII charactor array
// Char.: IntNumber number to be translated to ASCII charactor
//---------------------------------------------------------
{
if (IntNumber < 10)
{
AsciiArray[0] = IntNumber + 0x30;
AsciiArray[1] = 0x20;
AsciiArray[2] = 0x20;
AsciiArray[3] = 0x20;
AsciiArray[4] = 0x20;
return;
}
if (IntNumber < 100)
{
AsciiArray[0] = IntNumber / 10 + 0x30;
AsciiArray[1] = IntNumber % 10 + 0x30;
AsciiArray[2] = 0x20;
AsciiArray[3] = 0x20;
AsciiArray[4] = 0x20;
return;
}
if (IntNumber < 1000)
{
AsciiArray[0] = IntNumber / 100 + 0x30;
AsciiArray[1] = IntNumber % 100 / 10 + 0x30;
AsciiArray[2] = IntNumber % 10 + 0x30;
AsciiArray[3] = 0x20;
AsciiArray[4] = 0x20;
return;
}
if (IntNumber < 10000)
{
AsciiArray[0] = IntNumber / 1000 + 0x30;
AsciiArray[1] = IntNumber % 1000 / 100 + 0x30;
AsciiArray[2] = IntNumber % 100 / 10 + 0x30;
AsciiArray[3] = IntNumber % 10 + 0x30;
AsciiArray[4] = 0x20;
return;
}
else
{
AsciiArray[0] = IntNumber / 10000 + 0x30;
AsciiArray[1] = IntNumber % 10000 / 1000 + 0x30;
AsciiArray[2] = IntNumber % 1000 / 100 + 0x30;
AsciiArray[3] = IntNumber % 100 / 10 + 0x30;
AsciiArray[4] = IntNumber % 10 + 0x30;
return;
}
}
㈤ 如何用VB通过串口和51单片机通讯使得单片机能够在IO口产生脉冲,脉冲的数量和频率由VB设定
这个需要分步来完成
1、实现单片机串口通讯
2、编写VB程序,添加串口控件,实现VB与单片机串口通讯
3、写单片机IO驱动,实现脉冲输出
4、VB发数据给单片机,实现脉冲数量和频率控制
5、更详细的可以私信我完成
㈥ VB用Mscomm如何向单片机发送一个数据
Dim i As Integer Dim j As String i = 125 'j=125作为调试和讲解用 j = Mid(Str(i), 2, Len(Str(i)) - 1) '将数据变量变成字符行变量,但转换后成了"空格125",故对其进行处理 MSComm1.Output = j '将数据进行发送 MSComm1.InBufferCount = 0 '发送缓冲区清零 不知道这位问问好友为何要将数据以字符的形式发送
希望采纳
㈦ VB中,点击确定,把Text中的内容发送给单片机
你如果要把text文件中的内容发到单片机中,就必须进行文件操作,先打开text文件,再把打开的数据通过串口发送到单片机中,你最好要自己定义个软件传送同步协议,那样就很好保证写卡的正确率,你可以以数据包的形式发送,比如命令可以放第一字节,接下来是整包数据长度。单片机接收厚写卡就是单片机的问题了。同样读卡也可以以数据包形式把数据连同命令一起发送给电脑,并可以把卡中数据写到text文件中。
希望对你有点帮助。
㈧ VB给单片机传送数据问题!急!!
如果想传递的数值是0-255的,可以按字节方式传递,但不可能同时,只能一前一后传递.
Private Sub Command1_Click()
Dim bytSj(1) As Byte
bytSj(0) = 50
bytSj(1) = 30
MSComm1.Output = bytSj
End Sub
Private Sub Form_Load()
MSComm1.PortOpen = True
End Sub
㈨ 请教一下关于VB与51单片机传送数据的问题
VB程序 与 单片机程序,没有问题,是波特率 误差的问题
该 VB 程序 发送 范围是 0 - 128 ,> 128 时发送 的 为 0 。
单片机 波特率 和 晶振 有关,与 PC 通讯一定要用 11.0592 MHz 的晶振
如果 用 6.000MHz 或者 12.000MHz 的 一般都 会有一定 的误差
就是 这个 误差 会 引起通讯 数据 失真。