首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VB >

EXcel中 如何给单片机串口发指令发不下去

2012-01-22 
EXcel中 怎么给单片机串口发指令发不下去我的单片是这样的,外部中断一下向串口发送80,串口接收到VBA给它发

EXcel中 怎么给单片机串口发指令发不下去
我的单片是这样的,外部中断一下向串口发送80,串口接收到VBA给它发的“95 10 20 25”给串口回90 ,都是十六进制,单片机程序没问题,我用串口调试助手都试过了, EXcel 中部分VBA程序如下

Dim HData As String
Dim cishu As Integer

Private Sub UserForm_Initialize()
   
  cishu = 1
End Sub

Sub caiji()

  Dim tem As Variant
  Dim OutBuffer(3) As Byte
  Dim strtemp As String
 
   
  Select Case cishu
  Case 1
   
  TextBox1.Text = "0.5"
  TextBox2.Text = ""
  TextBox3.Text = ""
   
  TextBox5.Text = ""
  cishu = cishu + 1
  ' HData = ""
  Case 2
   
  TextBox2.Text = "4.5"
   
  tem = 95
  OutBuffer(0) = Val("&H" & tem)
  tem = 10
  OutBuffer(1) = Val("&H" & tem)
  tem = 20
  OutBuffer(2) = Val("&H" & tem)
  tem = 25
  OutBuffer(3) = Val("&H" & tem)
   
  MSComm1.Output = OutBuffer
   
   
  End Select
   
  
  HData = ""
   
  End Sub
  Sub kk()
  Dim m, k, l As Variant
  delay 200

   
  TextBox3.Text = "2.886" 'Format(Reading(0), "0.000")
  cishu = 1
   
   
   
  m = Val(TextBox3.Text) * Val(TextBox10.Text) / (Val(TextBox2.Text) - Val(TextBox3.Text)) '桥路阻抗

  k = Val(TextBox6.Text) / Val(TextBox9.Text) * (Val(TextBox2.Text) - Val(TextBox1.Text)) 'S测
  l = (Val(TextBox8.Text) - Val(TextBox7.Text)) '目标值

  TextBox5.Text = Format(((m * k - m * l) / l + Val(TextBox4.Text)), "0.000")
   
   


  ActiveCell.Offset(1, 0).Select
  ActiveWorkbook.Save
  HData = ""
  
  End Sub


Private Sub MSComm1_OnComm() '接收数据
  On Error Resume Next
  Dim BytesReceived() As Byte
  Dim buffer As String
   
  Dim i As Integer
  Select Case MSComm1.CommEvent
  Case comEvReceive '接收十六进制数据。并以十六进制显示
  MSComm1.InputLen = 0
  buffer = MSComm1.Input '接收数据至字符串中
  BytesReceived() = buffer '将数据转入Byte数组中
  For i = 0 To UBound(BytesReceived) '显示结果以十六进制显示
  If Len(Hex(BytesReceived(i))) = 1 Then
  HData = HData & "0" & Hex(BytesReceived(i))
  Else
  HData = HData & Hex(BytesReceived(i))
  End If
   
  'ActiveSheet.Range(ActiveCell.Address) = HData
  ' ActiveCell.Offset(1, 0).Select
  '最后将结果后入Text1中
  MSComm1.OutBufferCount = 0 '清除发送缓冲区
  MSComm1.InBufferCount = 0 '清除接收缓冲区
  'MSComm1.PortOpen = False
  Next
  End Select
   
  If HData = "80" Then
  ActiveSheet.Range(ActiveCell.Address) = HData
  ActiveCell.Offset(1, 0).Select
  'HData = ""
  caiji
  ElseIf HData = "90" Then
  
  ActiveSheet.Range(ActiveCell.Address) = HData


  ActiveCell.Offset(1, 0).Select
  HData = ""
  kk
  ' Else
  'Exit Sub
  End If
 End Sub
 
怎么就case 2 下面的95 10 20 25 发不出去呢, TextBox2.Text = "4.5"这句话执行了的啊

[解决办法]
lz确定mscomm的串口参数设置于单片机上的设置一样么?还有,你确定你能收到正确的数据么?你在接收的时候,inputmode设置的是多少?如果接收十六进制数,参数应该设置为1.然后接收缓冲区buffer设置为Variant类型。

另外
tem = 95
OutBuffer(0) = Val("&H" & tem)
tem = 10
OutBuffer(1) = Val("&H" & tem)
tem = 20
OutBuffer(2) = Val("&H" & tem)
tem = 25
OutBuffer(3) = Val("&H" & tem)

可以改成
OutBuffer(0) = &h95
OutBuffer(1) = &h10
OutBuffer(2) = &h20
OutBuffer(3) = &h25


VBA与vb6实际上都是调用的相同的串口控件。没有区别。

热点排行