串口十六进制的显示源码_串口发送十六进制数据

hacker|
88

文章目录:

如何把串口接收到的数据用十六进制数表示

串口都是用16进制发送的,有两种办法,

在接收到数据之后你把16进制转为10进制就可以了,比如你发送的十进制10,传送的时是十六进制0x0A,接收到数据后转成10进制 0AH = 10。

在发送时把数据转换成BCD码,发送十进制10,转成BCD码十六进制就是0x10,接收到数据时显示的就是10H。

BCD码转换百度一下可以。

求代码 VB串口16进制程序

Private Sub Form_Load()

MSComm1.Settings = "9600,N,8,1" '设置通信口参数

MSComm1.InBufferSize = 40 '设置MSComm1接收缓冲区为40字节

MSComm1.OutBufferSize = 2 '设置MSComm1发送缓冲区为2字节

MSComm1.InputMode = comInputModeBinary '设置接收数据模式为二进制形式

MSComm1.InBufferCount = 0 '清除接收缓冲区

MSComm1.OutBufferCount = 0 '清除发送缓冲区

'MSComm1.RThreshold = 1 '设置接收一个字节产生OnComm事件

MSComm1.CommPort = 5

MSComm1.PortOpen = True '打开通信口

End Sub

Private Sub Command2_Click()

Dim data() As Byte '串口发送字节

ReDim data(7)

data(0) = H81

data(1) = H81

data(2) = H52

data(3) = H00

data(4) = H00

data(5) = H00

data(6) = H53

data(7) = H00

MSComm1.Output = data

接收有几种办法:1.延时接收

Timer1.Enabled = True

t1_c0:

If t1_flag0 = True Then GoTo t1_c1

DoEvents

GoTo t1_c0

t1_c1:

t1_flag0 = False

Dim Bintput() As Byte

Dim Binputa As Variant

Binputa = MSComm1.Input ' 从接收队列中读入字符串

Bintput() = Binputa

Dim i As Integer

For i = 0 To UBound(Bintput)

If Len(Hex(Bintput(i))) = 1 Then

strData = strData "0" Hex(Bintput(i))

Else

strData = strData Hex(Bintput(i))

End If

Next

Text3.Text = strData

2.用事件接收---串口要打开'MSComm1.RThreshold = 1 '设置接收一个字节产生OnComm事件

Private Sub MSComm1_OnComm()

Select Case MSComm1.CommEvent

Case 2

strTxt = strTxt MSComm1.Input

If Right(strTxt, 1) = Chr(13) Then

Text1.Text = strTxt

strTxt = ""

End If

End Select

End Sub

祝你成功!

串口串口发送16进制

16进制字节数据就是一个字节数组,把modbus命令存放在一个字节数组中,发送即可。

也可以先用流行支持协议的串口软件,如格西烽火等,编写好协议,测试通过再把命令字节数组在单片机中实现。

串口调试助手的 十六进制显示功能代码(c#)谁有或者能给写份 急急急

这是因为它显示的是字符方式,'7'的ASCII码就是十六进制的37

主要看你是选择的显示方式

各位大侠帮忙:求串口调试源码,最好是C或VB的

是用VB调试精灵的源代码改过来的,以16进制方式显示发送:

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

3条大神的评论

  • avatar
    访客 2022-07-09 上午 05:01:16

    收数据模式为二进制形式MSComm1.InBufferCount = 0 '清除接收缓冲区MSComm1.OutBufferCount = 0 '清除发送缓冲区'MSComm1.RThreshold =

  • avatar
    访客 2022-07-09 下午 01:43:01

    SComm.PortOpen = True End If Me.ctrTimer.Interval = intTime Me.ctrTimer.Enabled = True Me.cmdAutoSend.Caption = "停止发送" End If bln

  • avatar
    访客 2022-07-09 下午 01:39:40

    = H00MSComm1.Output = data接收有几种办法:1.延时接收Timer1.Enabled = Truet1_c0: If t1_flag0 = T

发表评论