导航:首页 > 操作系统 > vb和单片机

vb和单片机

发布时间:2022-10-30 20:46:33

⑴ VB与单片机串口通讯并采集数据,实现实时曲线显示

这个问题有点太笼统了,不知道你具体需要什么?
首先把0~6V信号转换为AD采样的采样电压范围内,转换为数字信号后,通过总线送给单片机,单片机把数据整理后,通过串口送给PC,PC根据时间单位的要求,定时(比如每秒一次)把采集到的3路信号分别在坐标上描出一个点,点多了就成为了一条实时曲线.

⑵ vb 与单片机通信 &hbb数据收不到

MSComm1.Output = Chr(send3) 这句有问题。
请不要将send3转化为字符,使用MSComm1.Output = send3
====================================
dim sende(0) as byte
send3(0)=&Hbb
mscomm1.output=send3

⑶ 如何通过vb控制单片机的四个IO口

这跟你约定的VB上位机和MCU协议有关了!
首先确定你的控制模式是上位机VB是串口Send给MCU数据吧?
MCU应该读串口发的数据吧?
你定义控制几个IO就应控制几个IO啊!
如果MCU的不是你编写的代码,就应该按照MCU的约定功能去做!反正你首先要确定双方协议

⑷ 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编程,如何实现计算机同8位单片机的串口通信

**********************VB代码 PC**************************
Dim s As String
Dim sent() As Byte
Private Sub Form_Load()
With MSComm1
If .CommPort <> 2 Then .CommPort = 2 '设置Com2为通信端口
.Settings = "9600,N,8,1" '设置通信端口参数 9600赫兹、无校验、8个数据位、1个停止位.(这里需要进一步说明的是:.Setting=”BBBB,P,D,S”。
'含义是:B:Baud Rate(波特率);P:Parity(奇偶);D:Data Bit;S:Stop Bit)
.InBufferSize = 1024 '设置缓冲区接收数据为1024字节
.OutBufferSize = 512
.OutBufferCount = 0 '清空发送缓冲区
.InBufferCount = 0 '滑空接收缓冲区
If Not .PortOpen Then
.PortOpen = True '打开通信端口
End If
End With
End Sub

Private Sub Command1_Click()
s = Text1.Text
Call sendmsg
End Sub

Private Sub sendmsg()
Dim i As Integer
ReDim sent(Len(Text1.Text) + 1) As Byte
For i = 1 To Len(Text1.Text)
sent(i) = Asc(Mid(s, i, 1)) '发送ASC码
Next i
sent(i) = Asc(vbCrLf) '发送换行表示数据结束
MSComm1.Output = sent
MSComm1.OutBufferCount = 0
End Sub
************************C代码,单片机*****************************

#include <STDIO.H>
#include <AT89LV52.h>

void initial(void); //初始化

vvoid initial(void)
{
IP = 0X00; //定义串口高优先及
IE=0x00; //允许串口,定时器0
TCON=0X05;
TMOD=0x21; //定时器1为自动装入方式
PCON=0x00; //速率bu翻倍
SCON=0X50; //串口工作方式:8位UART,数据传输速率可变
TH1=0XFD; //OXFF(22.184M)57600 baud
TL1=0XFD; //设置定时器
//PCON=0X80|PCON; //速率为9600 baud(晶振11.059M),SMOD=0数据传输速率4.8KB,th1=tl1=fd,smod=1,速率为19.2kb
TR1 = 1; //启动定时器1
}

main()
{
int i=0,j,getbuf[5];
initial();
while(i<5)
{
getbuf[i]=_getkey(); //接受ASC码
if (getbuf[i] == 13) break; //接收到换行表示数据结束
i++;
}

}

⑹ 利用VB实现串口接收单片机数据

InputModeBinary
=
1
'通过
Input
属性以二进制方式检取回数据
写法有错,正确写法:
MSComm1.InputMode
=
comInputModeBinary
'以二进制方式接收
或:
MSComm1.InputMode
=
1
'以二进制方式接收

⑺ 单片机串口分别三次向VB上位机发送数组数据

第一,你要解决数据是否同步的问题。电脑的频率比单片机的时钟频率快很多,所以串行通信选择同步传输。当单片机发送完数组的第一个数据,上位机接收到这个数据后发送一个应答信号给单片机,然后单片机收到应答信号接着发送第二个数据给上位机,以此类推(应答信号的具体数值是你的通讯协议定义的)

第二,你的vb要添加MSComm控件,参考文章
http://wenku..com/linkurl=gaBSjBHSDk9b_THl3f1XYPtpRZgpIt6wczgHWXe_h_1q8ICdfPRP_Q-Fq4JLsbT_r_D8gzTRs_-sa9956OHq0XD9ahbc6sa4d-_omQVAlXG
控件MSComm的接收数据程序:
Private Sub MSComm1_OnComm()
Dim indata As Variant
Dim bte(0) As Byte
Select Case MSComm1.CommEvent
Case 2
indata = MSComm1.Input
bte(0) = AscB(indata)
If bte(0) = 数组的第一个数值
Then MSComm1.Output = 应答信号1
text1.text=bte(0)
end if
If bte(0) =数组的第二个数值 Then MSComm1.Output =应答信号2
text2.text=bte(0)
end if
If bte(0) =数组的第三个数值 Then MSComm1.Output =应答信号3
text3.text=bte(0)
end if
MSComm1.OutBufferCount = 0
End Select
End Sub

⑻ vb和单片机通信的问题。mscomm.RThreshold=1的疑问

当vb收到一个字节了,vb产生OnComm事件,但是vb是windows 操作系统下的程序,执行到事件的mscomm1.Input 时已经过去一定时间,串口是有缓冲区的,这时候读input已经传过来很多字节了。所以就发生了你所说的结果

⑼ 我用VB编的和单片机232通讯的程序,我如何判断通讯超时呢,比如把通讯线拔了,如何让它提示通讯断开呢,

通讯超时:用轮询法接收数据,当3秒内无接收到某地址的单片机的任何数据,即可判断为通讯超时;超时后,把标签置为通讯中断标志就可以了。

PC只接收数据:
1 首先打开定时器1时间设为1ms,作为轮询接收数据;
2 定时器1中断是,接收串口数据,当接收到数据时,保存数据,同时关闭定时器2通讯超时3秒定时器,发送反馈信息给单片机(可选择);
当没接收到数据,打开定时器2;
3 当定时器2中断时,认为相隔3秒未收到数据,判断为通讯超时;

PC收发数据:
作为主机更简单,主机发数据给单片机后,立刻打开定时器2;
如果在定时器2中断前接收到单片机的反馈数据,关闭定时器2;
当定时器2中断时,直接认为通讯中断。

⑽ vb可以编辑单片机程序吗

可以呀,只是编辑的话,就是写程序。用vb的编辑器可以的,只是不好用。
单片机程序用任何编辑器都 可以写的,包括windows自带的记事本。
编译就不行了,要用keil 、wave等 。

阅读全文

与vb和单片机相关的资料

热点内容
pboc长度加数据加密 浏览:187
英雄联盟国际服手游怎么下安卓 浏览:297
程序员的思路 浏览:234
只能用命令获得的四种方块 浏览:358
怎么用命令方块防止开创造 浏览:807
扫描版的pdf 浏览:790
编程猫怎样做3d游戏 浏览:207
怎么查找云服务器上的ftp 浏览:156
我的世界服务器如何注册账号 浏览:934
统计英文字符python 浏览:423
linux信息安全 浏览:908
压缩机接线柱爆 浏览:999
程序员自主创业 浏览:584
汇编程序员待遇 浏览:359
怎么批量有顺序的命名文件夹 浏览:211
杭州程序员健身 浏览:19
dvd光盘存储汉子算法 浏览:758
苹果邮件无法连接服务器地址 浏览:963
phpffmpeg转码 浏览:672
长沙好玩的解压项目 浏览:145