Click here to Skip to main content
15,913,055 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
See more:
Continuation... Completely new to programming but I need to send the array of data to a serial device stating "<stx>01P00104##<etx>". Can I get help of how to format this correctly?


C#
private void buttonStart_Click(object sender, EventArgs e)
        {
            serialPort1.PortName = "COM3";
            serialPort1.BaudRate = 9600;
            serialPort1.Parity = Parity.None;
            serialPort1.DataBits = 8;
            serialPort1.StopBits = StopBits.One;
            serialPort1.Handshake = Handshake.None;

            serialPort1.Open();
            if (serialPort1.IsOpen)
            {
                buttonStart.Enabled = false;
                buttonStop.Enabled = true;
                textBox1.ReadOnly = false;
            }
        }
        private const byte stx = 0x02;
        private const byte etx = 0x03;
        private byte[]WrapString(string send)
        {
            int length = send.Length;
            byte[] data = new byte[length + 2];
            data[0] = stx;
            data[length + 1] = etx;
            Array.Copy(System.Text.Encoding.ASCII.GetBytes(send), 0, data, 1, length);
            return data;
        }
        byte data = WrapString("01P00104##");
        private void linkLabel_HC1_100_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (serialPort1.IsOpen)
                serialPort1.Write(data);
        }
Posted
Comments
phil.o 9-Jul-15 11:43am    
Shouldn't we read byte[] data = WrapString("01P00104##"); instead of byte data = ...?
And, anyway, this line is misplaced; the body of a class can only contain declarations, you cannot directly execute code like you are trying from inside the body of your class.
Richard Deeming 9-Jul-15 11:50am    
Technically, field initializers can call static methods, so this line would work if the WrapString method was static.
phil.o 9-Jul-15 11:58am    
You're absolutely right :)
Member 11780461 9-Jul-15 11:50am    
Ok, Ill make that read in change, but where would you recommend I make the execution because I want the array command sent after I click on the link label.
phil.o 9-Jul-15 11:58am    
I suggest you then create a LinkLabel_Click event handler, and execute your code from there.

You are doing it wrong. Add STX and ETX as characters, before getting ASCII. Use (char)STX and (char)ETX, add it to string-only data, and then use System.Text.Encoding.ASCII.GetBytes to the whole string, to get your bytes.

The reason for this is quite obvious, but it would be easier for you to understand it yourself (I hope) than explaining it. You should not mix bytes and characters, as you are not dealing with 1-byte string encoding.

That will solve the problem.

—SA
 
Share this answer
 
v4
Comments
Member 11780461 10-Jul-15 13:19pm    
Posted a new question with my change if you have time to check out:
http://www.codeproject.com/Questions/1008564/How-to-get-Characters-Get-Bytes?arn=0
Sergey Alexandrovich Kryukov 10-Jul-15 13:33pm    
I just answered, please see.
—SA
As a complement of solution 1 by Sergey, here is how you can do to link an event-handler to one of your control's events:

  • In the designer window of your form, select your LinkLabel control.
  • In the 'Properties' Window (CTRL W, P - to show it), click on the 'Events' button.
  • In the event list, search for the 'LinkClicked' event, and click on the column next to it to get the focus.
  • Type the name of the event-handler: (MyLink_LinkClicked, for example if you have named your control 'MyLink'), and press TAB when you're done. This will create a method with the name you just entered.
  • In the code window, you can now do whatever you want to when the LinkLabel has been clicked.

This should be something like:
C#
private void MyLink_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) {
   byte data = WrapString("01P00104##");
} 

Keep in mind the answer given in solution 1, though: prepend/append the chars to the original send string, and get the bytes out of it at the end.
 
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