echo 串口发送命令_echo 串口发送命令_c#通过串口发送命令(c# send command via serial port)...

I have to handle a chain of four motors using a serial port. The user manual indicates that during initialization I need to send (for example) the strings

Z

1ECHO

2ECHO

3ECHO

4ECHO

And tells me to do that, in a code I do not recognize, with commands

PRINT(#128, "Z", #13)

PRINT(#129, "ECHO", #13)

PRINT(#130, "ECHO", #13)

PRINT(#131, "ECHO", #13)

PRINT(#132, "ECHO", #13)

To explain this, the manual also adds a sentence I do not understand:

with the strings above you can initialize the motors via the motors software but a host program have to send the same commands with different prefix: 0, 1, 2, 3 and 4 are in fact 0x80, 0x81, 0x82, 0x83 and 0x84, respectively. The decimal equivalents of hexadecimal values ​​are 128, 129, 130, 131 and 132.

A colleague wrote a working code in Visual Basic as follows:

SerialPort.Output(chr(128) & "Z" & chr(13))

SerialPort.Output(chr(129) & "ECHO" & chr(13))

SerialPort.Output(chr(130) & "ECHO" & chr(13))

SerialPort.Output(chr(131) & "ECHO" & chr(13))

SerialPort.Output(chr(132) & "ECHO" & chr(13))

I've translated into .NET with:

SerialPort.Write((char)128 + "Z" + (char)13);

SerialPort.Write((char)129 + "ECHO" + (char)13);

SerialPort.Write((char)130 + "ECHO" + (char)13);

SerialPort.Write((char)131 + "ECHO" + (char)13);

SerialPort.Write((char)132 + "ECHO" + (char)13);

But it does not work.

Now I can not try but I wonder if it can work to rewrite my code like this:

SerialPort.Write("Z" + (char)13);

SerialPort.Write("1ECHO" + (char)13);

SerialPort.Write("2ECHO" + (char)13);

SerialPort.Write("3ECHO" + (char)13);

SerialPort.Write("4ECHO" + (char)13);

If it will work, why does it work? And if it does not work, how do I correctly translate my colleague's code?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值