如何在vb.net中发送十六进制命令以进行转换 [英] how to send hex command to comport in vb.net

查看:82
本文介绍了如何在vb.net中发送十六进制命令以进行转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尊敬的先生,

我正在尝试发送十六进制命令来报告开放障碍的cos.我的代码成功执行,但在打开按钮处没有打开障碍.我该怎么办?
以下是我的代码.

Respected sir,

I am trying to send hex command to comport cos of open barrier. My code execute successfully but barrier not open at open button. What can i do??
Following is my code.

Imports MSCommLib
Public Class NewBarrier
    Dim MSComm1 As MSComm

    Private Sub NewBarrier_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        MSComm1 = New MSComm
        MSComm1.CommPort = 1
        MSComm1.Settings = "9600,N,8,1"
        MSComm1.DTREnable = True
        MSComm1.RTSEnable = True
        MSComm1.RThreshold = 1
        MSComm1.SThreshold = 0
        MSComm1.PortOpen = True
    End Sub
    
    Public Sub open_barr()
        With MSComm1
            If .PortOpen = False Then .PortOpen = True
            'MSComm1.Output = "0x02";
            'MSComm1.Output = "0x00"
            'MSComm1.Output = "O"
            'MSComm1.Output = "0x00"
            'MSComm1.Output = "0x03"
            'MSComm1.Output = "0x54"

            MSComm1.Output = &H20
            MSComm1.Output = &H0
            MSComm1.Output = &H4F
            MSComm1.Output = &H0
            MSComm1.Output = &H54

            'MSComm1.Output = Chr(&H2)
            'MSComm1.Output = Chr(&H0)
            'MSComm1.Output = Chr(&H4F)
            'MSComm1.Output = Chr(&H0)
            'MSComm1.Output = Chr(&H3)
            'MSComm1.Output = Chr(&H54)

        End With
    End Sub

    End Sub

    Private Sub cmdOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdOpen.Click
        Call open_barr()
    End Sub

推荐答案

您的代码(如果您也查看注释掉的位)很困惑:
Your code (if you look at the commented out bits as well) is confused:
'MSComm1.Output = "0x02";
'MSComm1.Output = "0x00"
'MSComm1.Output = "O"
'MSComm1.Output = "0x00"
'MSComm1.Output = "0x03"
'MSComm1.Output = "0x54"

MSComm1.Output = &H20
MSComm1.Output = &H0
MSComm1.Output = &H4F
MSComm1.Output = &H0
MSComm1.Output = &H54

'MSComm1.Output = Chr(&H2)
'MSComm1.Output = Chr(&H0)
'MSComm1.Output = Chr(&H4F)
'MSComm1.Output = Chr(&H0)
'MSComm1.Output = Chr(&H3)
'MSComm1.Output = Chr(&H54)

说明您不确定哪些内容您应该发送:第一个表示消息结构

Implies that you are not sure what you should be sending: The first one implies a message structure

STX NUL ETX '6'

("6"字符可能是校验字符)
第二个暗示您只是猜测并舍入了一些位:

(the ''6'' character is probably a check character)
The second implies you just guessed and turned bits round:

SPC NUL 'O' NUL '6'


您试图随机扔出更多东西的第三个人,希望这会对您感到可惜!


Your third that you tried throwing some more stuff in at random in the hope it would take pity on you!

STX NUL 'O' NUL ETX '6'



这种方法行不通.您需要查看手册并确定需要什么数据.我猜这是一条结构化消息,以STX开头,以ETX结尾,并带有某种形式的校验位(必须正确,否则将忽略整个消息),但不知道校验位应如何计算,或不知道什么中间的数据实际上应该是,我帮不了你.



That approach won''t work. You need to look at the manual and work out what data it is expecting. I am guessing it is a structured message, starting with STX, ending in ETX and with a check digit of some form (which must be right or the whole message will be ignored) but without knowing how the check digit should be calculated, or what the data in the middle should actually be, I can''t help you.


这篇关于如何在vb.net中发送十六进制命令以进行转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆