[在VB中将数据包从PC发送到串口] [英] [Sending a data packet from PC to serial port in VB]

查看:97
本文介绍了[在VB中将数据包从PC发送到串口]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello Guys,



我需要一些代码示例才能将数据包从PC(vb程序)发送到串口。我正在寻找一些示例代码以及如何在代码中对数据包进行帧序列发送...我的数据包格式如下:[Type + Opcode + Parameter + CheckSum]



请帮忙!谢谢



我的尝试:



我试图发送一些命令(蓝牙模块的AT命令),以及在发送到串口时运行良好。但我不知道如何构建数据包并将其发送到串口。

解决方案

从这里开始:SerialPort.Write Method(Byte [],Int32,Int32)(System.IO.Ports) [ ^ ] - 这就是你可能的方法需要使用。

这样做很简单:

  string 参数=  我的参数; 
byte [] paramData = Encoding.UTF8.GetBytes(parameter);
int paramLen = paramData.Length;
byte [] data = new byte [ 1 + 1 + paramLen + 1 < /跨度>];
data [ 0 ] = type;
data [ 1 ] = opcode;
Array.Copy(parameter.ToCharArray(), 0 ,数据, 2 ,paramLen) ;
data [paramLen] = GetCheckSum(data);
myPort.Write(data, 0 ,data.Length);


< blockquote class =quote>

Quote:

但我不知道如何构建数据包并将其发送到串口。

您可以认为您已发送的每个AT命令都是一个帧。

您的帧只是一个包含您想要的字符串,一个AT命令或[Type + Opcode + Parameter + CheckSum]。

你必须确保字符串是由8位字符组成的,并且如果你有一些特殊字符则不会被编码为utf 8。


Hello Guys,

I need some code sample to send the data packet to serial port from PC (vb program). I am looking for some sample code for this and How to frame the packet in the code to send it serially...My packet format is like this: [Type+Opcode+Parameter+CheckSum]

Please help!. Thanks

What I have tried:

I tried to send some commands (AT commands for bluetooth module), and its working well while sending to serial port. But I dont know how to frame a packet and send it to serial port.

解决方案

Start here: SerialPort.Write Method (Byte[], Int32, Int32) (System.IO.Ports)[^] - that's the method you probably need to use.
And it's trivial to do:

string parameter = "My parameter";
byte[] paramData = Encoding.UTF8.GetBytes(parameter);
int paramLen = paramData.Length;
byte[] data = new byte[1 + 1 + paramLen + 1];
data[0] = type;
data[1] = opcode;
Array.Copy(parameter.ToCharArray(), 0, data, 2, paramLen);
data[paramLen] = GetCheckSum(data);
myPort.Write(data, 0, data.Length);


Quote:

But I don't know how to frame a packet and send it to serial port.

You can consider that every single AT command you have already send is a frame.
Your frame is simply a string containing what you want, an AT command or [Type+Opcode+Parameter+CheckSum].
you just have to make sure that the string is made of 8 bits chars and will not get encoded as utf 8 if you have some special chars.


这篇关于[在VB中将数据包从PC发送到串口]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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