Click here to Skip to main content
15,883,623 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.
Posted
Updated 2-Dec-16 0:24am
Comments
[no name] 2-Dec-16 5:31am    
Such a common question. Have you Googled it? Just Google "Sending a data packet from PC to serial port in VB".

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:
C#
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);
 
Share this answer
 
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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900