如何用VB通过串口和51单片机通讯使得单片机能够在IO口产生脉冲,脉冲的数量和频率由VB设定?

9802℃ SUZANNE

如何用VB通过串口和51单片机通讯使得单片机能够在IO口产生脉冲,脉冲的数量和频率由VB设定?

如何利用VB实现计算机与单片机的串口通信

用热探头,接ADC采样,用51区数据,从串口传入PC。PC做个数据曲线就可了

VB利用串口与单片机通信

Option Explicit

Dim strData As String

Dim sjSEnd() As Byte

'输入处理为16进制字符串

Public Sub InputManage(bytInput() As Byte, intInputLenth As Integer)

Dim n As Integer

ReDim bytReceiveByte(intInputLenth)

For n = 0 To intInputLenth - 1 Step 1

strData = strData & Hex(bytInput(n))

Next n

Text2 = strData

End Sub

Private Sub Command1_Click()

Timer1.Enabled = True

End Sub

Private Sub Form_Load()

MSComm1.Settings = "19200,N,8,1"

MSComm1.RThreshold = 1

MSComm1.PortOpen = True

Text1 = "" '在Text1中写16进制指令

Timer1.Interval = 200

Timer1.Enabled = False

End Sub

Private Sub MSComm1_OnComm()

Dim bytInput() As Byte

Dim intInputLen As Integer

If (MSComm1.CommEvent = comEvReceive) Then

MSComm1.InputMode = comInputModeBinary

intInputLen = MSComm1.InBufferCount

If intInputLen > 0 Then

Timer1.Enabled = False

End If

ReDim bytInput(intInputLen)

bytInput = MSComm1.Input

Call InputManage(bytInput, intInputLen)

'数据处理

'Dim l As Integer

'Dim ml As Byte

'l = Val("&H" & Mid(strData, 1, 2))

'ml = Val("&H" & Mid(strData, 3, 2))

'If Len(strData) = 2 * l + 2 Then

' strReceive(ml) = Mid(strData, 5, 2 * (l - 2))

'随后进行你想的数据处理

' strData = "" '清除接收数据

'End If

End If

End Sub

Private Sub Timer1_Timer()

Dim l As Integer

Dim i As Integer

l = Len(Text1) '可用于多字节指令

ReDim sjSEnd(l / 2 + 1)

sjSEnd(0) = &HBB

sjSEnd(l / 2 + 1) = &HEE

For i = 1 To Len(Text1) Step 2

sjSEnd((i + 1) / 2) = Val("&H" & Mid(Text1, i, 2))

Next

MSComm1.Output = sjSEnd

End Sub

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

InputModeBinary = 1 '通过 Input 属性以二进制方式检取回数据

写法有错,正确写法:

MSComm1.InputMode = comInputModeBinary '以二进制方式接收

或:

MSComm1.InputMode = 1 '以二进制方式接收

51单片机和VB做串口通讯的问题

不要连接,发送两个字节就行了,接收也两个字节

比如传感器要的数据是&hfff,就发送&hf和&hff

用二进制模式

redim sj(1) as byte

sj(0)=?

sj(1)=?

MSComm1.OutBufferCount = 0

MSComm1.Output =sj