搜索
bottom↓
回复: 15

上位机怎么以十六进制发数据给单片机啊

[复制链接]

出0入0汤圆

发表于 2012-6-22 00:55:41 | 显示全部楼层 |阅读模式
我是用VB写的;
直接写mscomm1.output=text1.text;
发过去的好像是ASCII形式....
求解啊

阿莫论坛20周年了!感谢大家的支持与爱护!!

你熬了10碗粥,别人一桶水倒进去,淘走90碗,剩下10碗给你,你看似没亏,其实你那10碗已经没有之前的裹腹了,人家的一桶水换90碗,继续卖。说白了,通货膨胀就是,你的钱是挣来的,他的钱是印来的,掺和在一起,你的钱就贬值了。

出0入0汤圆

发表于 2012-6-22 08:14:42 | 显示全部楼层
VB不熟悉,不过上位机变成文本文件默认就是ASCII码。你要是想要16进制只能自己转换。C#幽默人的选项,将文本内容自动转换成16进制格式。VB不清楚。如果没有只能自己转换。反正也不难。

出0入0汤圆

发表于 2012-6-22 08:23:06 | 显示全部楼层
格式转换Format

出0入0汤圆

 楼主| 发表于 2012-6-22 08:33:13 | 显示全部楼层
我尝试过这样写
mscomm1.output=text1.text(0)
但是这样写,运行的时候报错说,数据类型有误......
不知道楼上大侠是什么意思;
能举个小例子不

出0入0汤圆

 楼主| 发表于 2012-6-22 08:34:21 | 显示全部楼层
tiancaigao7 发表于 2012-6-22 08:14
VB不熟悉,不过上位机变成文本文件默认就是ASCII码。你要是想要16进制只能自己转换。C#幽默人的选项,将文 ...

嗯嗯,

出0入0汤圆

 楼主| 发表于 2012-6-22 08:34:55 | 显示全部楼层
34071417 发表于 2012-6-22 08:23
格式转换Format


我尝试过这样写
mscomm1.output=text1.text(0)
但是这样写,运行的时候报错说,数据类型有误......
不知道楼上大侠是什么意思;
能举个小例子不

出0入0汤圆

发表于 2012-6-22 08:41:35 | 显示全部楼层
C#中有format(“%D”,s),你找找应该有对应的转换函数

出0入96汤圆

发表于 2012-6-22 08:44:27 | 显示全部楼层
VB 中 我用过HEX(text1.text)德方式强制转换,但是注意超限的问题

出0入0汤圆

 楼主| 发表于 2012-6-22 08:44:46 | 显示全部楼层
34071417 发表于 2012-6-22 08:41
C#中有format(“%D”,s),你找找应该有对应的转换函数

直接把数据类型转换成十六进制吗?
要是这样mscomm1.output会不会也报错啊....

出0入0汤圆

 楼主| 发表于 2012-6-22 08:46:02 | 显示全部楼层
gallle 发表于 2012-6-22 08:44
VB 中 我用过HEX(text1.text)德方式强制转换,但是注意超限的问题

HEX(text1.text)和text1.text(0)有什么区别啊
我一直不懂text1.text(0)怎么就把数据变成十六进制了

出0入96汤圆

发表于 2012-6-22 08:55:58 | 显示全部楼层
  1. 以二进制方式接收,转为16进制字符形式显示:
  2. Private Sub Form_Load()
  3. MSComm1.CommPort = 1 '通道1
  4. MSComm1.Settings = "9600,N,8,1" '"9600,N,8,1"
  5. MSComm1.RThreshold = 1 '接收缓冲区收到每一个字符都会使 MSComm 控件产生 OnComm 事件
  6. MSComm1.PortOpen = True '打开串口
  7. MSComm1.InputMode = comInputModeBinary '以二进制方式接收
  8. End Sub


  9. Private Sub MSComm1_OnComm()
  10. On Error Resume Next
  11. Text1 = ""
  12. Dim a() As Byte
  13. Dim strBuff As String
  14. Dim strData As String
  15. Dim i As Integer
  16. Dim x As Integer
  17. Select Case MSComm1.CommEvent
  18. Case 2
  19. MSComm1.InputLen = 0
  20. strBuff = MSComm1.Input
  21. a() = strBuff
  22. For i = 0 To UBound(a)
  23. If Len(Hex(a(i))) = 1 Then
  24. strData = strData & "0" & Hex(a(i))
  25. Else
  26. strData = strData & Hex(a(i))
  27. End If
  28. Next
  29. Text1 = Text1 + strData
  30. End Select
  31. End Sub

  32. 你提问是仅接收1字节2进制数据,我认为,VB经RS232口接收单片机的数据字节数是依据通信协议而定,上述代码是MSCOMM控件接收数据的通用代码,可根据需要进行修改,见如下代码中的数据判别:
  33. Private Sub MSComm1_OnComm()
  34. On Error Resume Next
  35. Dim BytReceived() As Byte
  36. Dim strBuff As String
  37. Dim strData As String
  38. Dim i As Integer
  39. Dim x As Integer
  40. Select Case MSComm1.CommEvent
  41. Case 2
  42. MSComm1.InputLen = 0
  43. strBuff = MSComm1.Input
  44. BytReceived() = strBuff
  45. For i = 0 To UBound(BytReceived)
  46. If Len(Hex(BytReceived(i))) = 1 Then
  47. strData = strData & "0" & Hex(BytReceived(i))
  48. Else
  49. strData = strData & Hex(BytReceived(i))
  50. End If
  51. Next
  52. Text3 = Text3 + strData
  53. If Left(strData, 2) = "7D" And Len(strData) = 2 Then '接收1字节数据
  54. Text1(0).Text = Left(strData, 8)
  55. Call DataClear
  56. ElseIf Left(strData, 2) = "7F" And Len(strData) = 10 Then '接收5字节数据
  57. Text1(1).Text = Left(strData, 10)
  58. Call DataClear
  59. End If
  60. End Select
  61. End Sub

  62. Public Sub DataClear()
  63. MSComm1.OutBufferCount = 0 '清空发送缓冲区
  64. MSComm1.InBufferCount = 0
  65. Text3 = ""
  66. End Sub

  67. 下面补充用VB调试精灵的源代码改的16进制收发代码:
  68. Option Explicit
  69. Dim intTime As Integer
  70. Private strSendText As String '发送文本数据
  71. Private bytSendByte() As Byte '发送二进制数据
  72. Private blnReceiveFlag As Boolean
  73. Private blnAutoSendFlag As Boolean
  74. Private intPort As Integer
  75. Private strSet As String
  76. Private intReceiveLen As Integer
  77. Private bytReceiveByte() As Byte
  78. Private strAscii As String '设置初值
  79. Private strHex As String
  80. Private intHexWidth As Integer
  81. Private intLine As Integer
  82. Private m As Integer
  83. Private strAddress As String
  84. '字符表示的十六进制数转化为相应的整数,错误则返回 -1
  85. Function ConvertHexChr(str As String) As Integer
  86. Dim test As Integer
  87. test = Asc(str)
  88. If test >= Asc("0") And test <= Asc("9") Then
  89. test = test - Asc("0")
  90. ElseIf test >= Asc("a") And test <= Asc("f") Then
  91. test = test - Asc("a") + 10
  92. ElseIf test >= Asc("A") And test <= Asc("F") Then
  93. test = test - Asc("A") + 10
  94. Else
  95. test = -1 '出错信息
  96. End If
  97. ConvertHexChr = test
  98. End Function

  99. '字符串表示的十六进制数据转化为相应的字节串,返回转化后的字节数
  100. Function strHexToByteArray(strText As String, bytByte() As Byte) As Integer
  101. Dim HexData As Integer '十六进制(二进制)数据字节对应值
  102. Dim hstr As String * 1 '高位字符
  103. Dim lstr As String * 1 '低位字符
  104. Dim HighHexData As Integer '高位数值
  105. Dim LowHexData As Integer '低位数值
  106. Dim HexDataLen As Integer '字节数
  107. Dim StringLen As Integer '字符串长度
  108. Dim Account As Integer
  109. Dim n As Integer
  110. '计数
  111. 'txtSend = "" '设初值
  112. HexDataLen = 0
  113. strHexToByteArray = 0
  114. StringLen = Len(strText)
  115. Account = StringLen \ 2
  116. ReDim bytByte(Account)
  117. For n = 1 To StringLen
  118. Do '清除空格
  119. hstr = Mid(strText, n, 1)
  120. n = n + 1
  121. If (n - 1) > StringLen Then
  122. HexDataLen = HexDataLen - 1
  123. Exit For
  124. End If
  125. Loop While hstr = " "
  126. Do
  127. lstr = Mid(strText, n, 1)
  128. n = n + 1
  129. If (n - 1) > StringLen Then
  130. HexDataLen = HexDataLen - 1
  131. Exit For
  132. End If
  133. Loop While lstr = " "
  134. n = n - 1
  135. If n > StringLen Then
  136. HexDataLen = HexDataLen - 1
  137. Exit For
  138. End If
  139. HighHexData = ConvertHexChr(hstr)
  140. LowHexData = ConvertHexChr(lstr)

  141. If HighHexData = -1 Or LowHexData = -1 Then '遇到非法字符中断转化
  142. HexDataLen = HexDataLen - 1
  143. Exit For
  144. Else
  145. HexData = HighHexData * 16 + LowHexData
  146. bytByte(HexDataLen) = HexData
  147. HexDataLen = HexDataLen + 1
  148. End If
  149. Next n
  150. If HexDataLen > 0 Then '修正最后一次循环改变的数值
  151. HexDataLen = HexDataLen - 1
  152. ReDim Preserve bytByte(HexDataLen)
  153. Else
  154. ReDim Preserve bytByte(0)
  155. End If
  156. If StringLen = 0 Then '如果是空串,则不会进入循环体
  157. strHexToByteArray = 0
  158. Else
  159. strHexToByteArray = HexDataLen + 1
  160. End If
  161. End Function

  162. Private Sub cmdManualSend_Click()
  163. If Not Me.MSComm.PortOpen Then
  164. Me.MSComm.CommPort = intPort
  165. Me.MSComm.Settings = strSet
  166. Me.MSComm.PortOpen = True
  167. End If
  168. Call ctrTimer_Timer
  169. If Not blnAutoSendFlag Then
  170. Me.MSComm.PortOpen = False
  171. End If
  172. End Sub
  173. Private Sub cmdAutoSend_Click()
  174. If blnAutoSendFlag Then
  175. Me.ctrTimer.Enabled = False
  176. If Not blnReceiveFlag Then
  177. Me.MSComm.PortOpen = False
  178. End If
  179. Me.cmdAutoSend.Caption = "自动发送"
  180. Else
  181. If Not Me.MSComm.PortOpen Then
  182. Me.MSComm.CommPort = intPort
  183. Me.MSComm.Settings = strSet
  184. Me.MSComm.PortOpen = True
  185. End If
  186. Me.ctrTimer.Interval = intTime
  187. Me.ctrTimer.Enabled = True
  188. Me.cmdAutoSend.Caption = "停止发送"
  189. End If
  190. blnAutoSendFlag = Not blnAutoSendFlag
  191. End Sub

  192. Private Sub ctrTimer_Timer()
  193. Dim longth As Integer
  194. strSendText = Me.txtSend.Text
  195. longth = strHexToByteArray(strSendText, bytSendByte())
  196. If longth > 0 Then
  197. Me.MSComm.Output = bytSendByte
  198. End If
  199. End Sub
  200. '输入处理,处理接收到的字节流,并保存在全局变量
  201. Private Sub InputManage(bytInput() As Byte, intInputLenth As Integer)
  202. Dim n As Integer '定义变量及初始化
  203. ReDim Preserve bytReceiveByte(intReceiveLen + intInputLenth)
  204. For n = 1 To intInputLenth Step 1
  205. bytReceiveByte(intReceiveLen + n - 1) = bytInput(n - 1)
  206. Next n
  207. intReceiveLen = intReceiveLen + intInputLenth
  208. End Sub

  209. '为输出准备文本,保存在全局变量
  210. '总行数保存在intLine
  211. Public Sub GetDisplayText()
  212. Dim n As Integer
  213. Dim intValue As Integer
  214. Dim intHighHex As Integer
  215. Dim intLowHex As Integer
  216. Dim strSingleChr As String * 1
  217. Dim intAddress As Integer
  218. Dim intAddressArray(8) As Integer
  219. Dim intHighAddress As Integer
  220. strAscii = "" '设置初值
  221. strHex = ""
  222. strAddress = ""
  223. '获得16进制码和ASCII码的字符串
  224. For n = 1 To intReceiveLen
  225. intValue = bytReceiveByte(n - 1)
  226. If intValue < 32 Or intValue > 128 Then '处理非法字符
  227. strSingleChr = Chr(46) '对于不能显示的ASCII码,
  228. Else '用"."表示
  229. strSingleChr = Chr(intValue)
  230. End If
  231. strAscii = strAscii + strSingleChr
  232. intHighHex = intValue \ 16
  233. intLowHex = intValue - intHighHex * 16
  234. If intHighHex < 10 Then
  235. intHighHex = intHighHex + 48
  236. Else
  237. intHighHex = intHighHex + 55
  238. End If
  239. If intLowHex < 10 Then
  240. intLowHex = intLowHex + 48
  241. Else
  242. intLowHex = intLowHex + 55
  243. End If
  244. strHex = strHex + Chr$(intHighHex) + Chr$(intLowHex) + " "
  245. If (n Mod intHexWidth) = 0 Then
  246. strAscii = strAscii + Chr$(13) + Chr$(10)
  247. strHex = strHex + Chr$(13) + Chr$(10)
  248. Else
  249. End If
  250. Next n
  251. txtAsc = strAscii 'Ascii
  252. txtHex = strHex '16进制
  253. '获得地址字符串
  254. intLine = intReceiveLen \ intHexWidth
  255. If (intReceiveLen - intHexWidth * intLine) > 0 Then
  256. intLine = intLine + 1
  257. End If
  258. '设置换行
  259. For n = 1 To intLine
  260. intAddress = (n - 1) * intHexWidth
  261. intHighAddress = 8
  262. intAddressArray(0) = intAddress
  263. For m = 1 To intHighAddress
  264. intAddressArray(m) = intAddressArray(m - 1) \ 16
  265. Next m
  266. For m = 1 To intHighAddress
  267. intAddressArray(m - 1) = intAddressArray(m - 1) - intAddressArray(m) * 16
  268. Next m
  269. For m = 1 To intHighAddress
  270. If intAddressArray(intHighAddress - m) < 10 Then
  271. intAddressArray(intHighAddress - m) = intAddressArray(intHighAddress - m) + Asc("0")
  272. Else
  273. intAddressArray(intHighAddress - m) = intAddressArray(intHighAddress - m) + Asc("A") - 10
  274. End If
  275. strAddress = strAddress + Chr$(intAddressArray(intHighAddress - m))
  276. Next m
  277. strAddress = strAddress + Chr$(13) + Chr$(10)
  278. Next n
  279. txtAdd = strAddress '地址
  280. End Sub
  281. Private Sub cmdReceive_Click()
  282. If blnReceiveFlag Then
  283. If Not blnReceiveFlag Then
  284. Me.MSComm.PortOpen = False
  285. End If
  286. Me.cmdReceive.Caption = "开始接收"
  287. Else
  288. If Not Me.MSComm.PortOpen Then
  289. Me.MSComm.CommPort = intPort
  290. Me.MSComm.Settings = strSet
  291. Me.MSComm.PortOpen = True
  292. End If
  293. Me.MSComm.InputLen = 0
  294. Me.MSComm.InputMode = 0
  295. Me.MSComm.InBufferCount = 0
  296. Me.MSComm.RThreshold = 1
  297. Me.cmdReceive.Caption = "停止接收"
  298. End If
  299. blnReceiveFlag = Not blnReceiveFlag
  300. End Sub

  301. Private Sub Form_Load()
  302. intHexWidth = 8
  303. txtAdd = ""
  304. txtHex = ""
  305. txtAsc = ""
  306. txtSend = "11"
  307. txtAdd.Width = 1335
  308. txtHex.Width = 2535
  309. txtAsc.Width = 1215
  310. '设置默认发送接收关闭状态
  311. blnAutoSendFlag = False
  312. blnReceiveFlag = False
  313. '接收初始化
  314. intReceiveLen = 0
  315. '默认发送方式为16进制
  316. 'intOutMode = 1
  317. '初始化串行口
  318. intPort = 1
  319. intTime = 1000
  320. strSet = "9600,n,8,1"
  321. Me.MSComm.InBufferSize = 1024
  322. Me.MSComm.OutBufferSize = 512
  323. If Not Me.MSComm.PortOpen Then
  324. Me.MSComm.CommPort = intPort
  325. Me.MSComm.Settings = strSet
  326. Me.MSComm.PortOpen = True
  327. End If
  328. Me.MSComm.PortOpen = False
  329. End Sub

  330. Private Sub cmdClear_Click()
  331. Dim bytTemp(0) As Byte
  332. ReDim bytReceiveByte(0)
  333. intReceiveLen = 0
  334. Call InputManage(bytTemp, 0)
  335. Call GetDisplayText
  336. Call disPlay
  337. End Sub

  338. Private Sub MsComm_OnComm()
  339. Dim bytInput() As Byte
  340. Dim intInputLen As Integer
  341. Select Case Me.MSComm.CommEvent
  342. Case comEvReceive
  343. If blnReceiveFlag Then
  344. If Not Me.MSComm.PortOpen Then
  345. Me.MSComm.CommPort = intPort
  346. Me.MSComm.Settings = strSet
  347. Me.MSComm.PortOpen = True
  348. End If
  349. '此处添加处理接收的代码
  350. Me.MSComm.InputMode = comInputModeBinary '二进制接收
  351. intInputLen = Me.MSComm.InBufferCount
  352. ReDim bytInput(intInputLen)
  353. bytInput = Me.MSComm.Input
  354. Call InputManage(bytInput, intInputLen)
  355. Call GetDisplayText
  356. 'Call disPlay
  357. If Not blnReceiveFlag Then
  358. Me.MSComm.PortOpen = False
  359. End If
  360. End If
  361. End Select
  362. End Sub

  363. Private Sub disPlay()
  364. txtHex = ""
  365. txtAsc = ""
  366. txtAdd = ""
  367. End Sub
复制代码
一个例子,你学会了就差不多了

出0入0汤圆

 楼主| 发表于 2012-6-22 10:48:50 | 显示全部楼层
gallle 发表于 2012-6-22 08:55
一个例子,你学会了就差不多了

真心感谢啊......给代码学习了.......只不过要完全看懂真心不容易,我VB练的还不多。
现在有几个疑问.....没看懂,望大神教育下
                                                第24行的代码
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
大概理解是这是十六进制显示数据的功能,但是具体的一直看不懂。 Len(Hex(a(i))) = 1这个到底是实现什么条件,不懂呢......
                                   第56行代码
If Left(strData, 2) = "7D" And Len(strData) = 2 Then '接收1字节数据
Text1(0).Text = Left(strData, 8)
Call DataClear
ElseIf Left(strData, 2) = "7F" And Len(strData) = 10 Then '接收5字节数据
Text1(1).Text = Left(strData, 10)
Call DataClear
End If
这个是不是大神自己定的接受数据协议啊,但是判断条件里面的 Len(strData) = 2,这个是什么功能呢,
                         第93行代码
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
这个是什么功能啊,接受到"0"到“9”的时候显示整数0到就,但是接受到"a"到"f"或者是"A"到"F"的时候不是都是显示"10"到"15"。难道这个就是讲test变成十六进制的数据啊,没有理解透呢。
                              第201行代码
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
这个是定时器发送数据给单片机吧。假如我发送“AA”发送给单片机控制八个LED,那LED是不是就以AA的形式在那里亮呢。看完这个之后我感觉,我以前单片机接受不到AA是不是就是我没有先把要发送的数据转换成二进制数组啊.....这个就是我最想问的,求解呢,

对于第221行的Public Sub GetDisplayText() 函数,真心看不懂,头好晕......求大神能解释一下它大概的作用不

最后真心感谢大神,谢谢了。

出0入0汤圆

 楼主| 发表于 2012-6-22 10:50:40 | 显示全部楼层
gallle 发表于 2012-6-22 08:55
一个例子,你学会了就差不多了

谢谢了

出0入96汤圆

发表于 2012-6-22 12:15:13 | 显示全部楼层
这个不是我写的,我只是参考过其中的进制转换功能!

出0入0汤圆

发表于 2012-6-22 12:21:09 | 显示全部楼层
zhe ge hen hao

出0入0汤圆

 楼主| 发表于 2012-6-22 12:56:26 来自手机 | 显示全部楼层
gallle 发表于 2012-6-22 12:15 这个不是我写的,我只是参考过其中的进制转换功能!

好的,还是谢谢了,代码好东西确实
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片。注意:要连续压缩2次才能满足要求!!】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-10-3 16:10

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

快速回复 返回顶部 返回列表