以文本方式查看主题 - Foxtable(狐表) (http://foxtable.net/bbs/index.asp) -- 专家坐堂 (http://foxtable.net/bbs/list.asp?boardid=2) ---- [求助]串口发送数据和接收数据不成功,串口助手发送和接收是成功的。 (http://foxtable.net/bbs/dispbbs.asp?boardid=2&id=183921) |
-- 作者:yjm789000 -- 发布时间:2022/11/19 0:16:00 -- [求助]串口发送数据和接收数据不成功,串口助手发送和接收是成功的。 下面的串口代码我写不明白了,试了好多次了,麻烦老师帮我改成正确的下拉。想实现串口助手的功能 Dim zl As WinForm.TextBox = e.Form.Controls("TextBox1") Dim zls2 As WinForm.TextBox = e.Form.Controls("TextBox2") Dim zls3 As WinForm.TextBox = e.Form.Controls("TextBox3") Dim zls As String = zl.value zls = zls.replace(" ", "") Dim zl1 As UInteger = CUInt( zls.substring(0, 2)) Dim zl2 As UInteger = CUInt( zls.substring(2, 2)) Dim zl3 As UInteger = CUInt( zls.substring(4, 2)) Dim zl4 As UInteger = CUInt( zls.substring(6, 2)) Dim zl5 As UInteger = CUInt( zls.substring(8, 2)) Dim zl6 As UInteger = CUInt( zls.substring(10, 2)) Dim aaff As UInteger() = { zl1, zl2, zl3, zl4, zl5, zl6} Dim aa As Integer = CRC16(aaff, aaff.Length) ,这个是计算效验码的,计算没有问题是可以和串口助手对上了。 Dim bb As String = Convert.ToString(aa, 16).ToUpper() Dim zl7 As Byte = CByte(bb.substring(0, 2)) Dim zl8 As Byte = CByte(bb.substring(2, 2)) Dim zl11 As Byte = CByte(zls.substring(0, 2)) Dim zl22 As Byte = CByte(zls.substring(2, 2)) Dim zl33 As Byte = CByte(zls.substring(4, 2)) Dim zl44 As Byte = CByte(zls.substring(6, 2)) Dim zl55 As Byte = CByte(zls.substring(8, 2)) Dim zl66 As Byte = CByte(zls.substring(10, 2)) Dim Val() As Byte = New Byte() {zl11, zl22, zl33, zl44, zl55, zl66, zl7, zl8} Ports("COM6").Write(Val, 0, Val.Length) zls2.value = zls & bb Dim s2 As String s2 = s2 & Ports("COM6").ReadExisting() zls3.Value = s2 串口助手发送和接收都是正常的,我想实现下面这个串口助手的功能。 这是串口助手的发送和接收成功了。 下面是串口硬件的数据格式解释: 我要输入的指令是:01 05 00 00 00 00 ,效验码是计算出来的,CD CA
|
-- 作者:有点蓝 -- 发布时间:2022/11/19 9:05:00 -- cd是十六进制,输入的指令应该也是十六进制,要先转换为十进制使用,参考:http://www.foxtable.com/webhelp/topics/1622.htm |
-- 作者:yjm789000 -- 发布时间:2022/11/19 11:27:00 -- 回复:(有点蓝)cd是十六进制,输入的指令应该也是十... 老师,可以结合我的代码写的详细点么,我就是不理解这个16进制,10进制,字符串,数组这些东西组合使用,我的代码就一直写不正确,指令发送不出,谢谢! |
-- 作者:有点蓝 -- 发布时间:2022/11/19 11:38:00 -- 比如 Dim zl1 As UInteger = HexToDec(zls.substring(0, 2)) Dim zl7 As Byte = HexToDec(bb.substring(0, 2))
|
-- 作者:yjm789000 -- 发布时间:2022/11/19 13:50:00 -- 回复:(有点蓝)比如Dim zl1 As UInteger = &nb... 老师发送没有问题了,而且可以控制设备了。但是接收的数据对不上。串口助手接收返回的数据和发送的数据是一样了,我这边接收的数据就一个数字。 我的接收代码: Dim cnt As Integer = Ports("COM6").BytesToRead If cnt > 0 Then \'缓冲区是否有数据 Dim Val1(cnt - 1) As Byte zls3.value = Ports("COM6").Read(val1, 0, cnt) End If |
-- 作者:有点蓝 -- 发布时间:2022/11/19 14:04:00 -- Dim Val1(cnt - 1) As Byte Ports("COM6").Read(val1, 0, cnt) For Each bt As Byte In Val1 output.show(bt.Tostring("X2")) Next output.show("----") Dim s As String = Convert.ToBase64String(Val1) output.show(s)
|