Click here to Skip to main content
15,885,244 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello
I have a serial port unit with 8 relay.
when i send to the unit the command "FF A1 00" the unit respond with "FF A1 00 00 00 00 00 00 00 00" the 8 double zeros are all the relays are off.If i open the first relay and i ask again for status the unit response with "FF A1 01 00 00 00 00 00 00 00".
So i made an C# app with one text box with timer who send the command to unit every 1000ms and one other text box who i receive the response and 8 panel.
How can separate the response only the 00 or 01 for ever relay to paint the panels.
red for 00 and green for 01.
thanks in advance.

What I have tried:

I don't have the app here i am not at my place right now. :-(
Posted
Updated 4-Feb-19 2:07am
Comments
[no name] 4-Feb-19 12:25pm    
I have yet to see a clear statement from you regarding the "format" of the data in question.

Everyone thinks it's characters and space delimited when in fact you may be talking "binary".
Stm21 5-Feb-19 2:28am    
Thank you for your comment. Please can you explain to me how i can convert binary to text?

You can use string.Split()
string data = "FF A1 00 00 00 00 00 00 00 00";
string[] words = data.Split(' ');

if (words[2].Equals("00"))
{
    panel1.BackColor = Color.Green;
}
else
{
    panel1.BackColor = Color.Red;
}

if (words[3].Equals("00"))
{
    panel2.BackColor = Color.Green;
}
else
{
    panel2.BackColor = Color.Red;
}

if (words[4].Equals("00"))
{
    panel3.BackColor = Color.Green;
}
else
{
    panel3.BackColor = Color.Red;
}

// etc.....
 
Share this answer
 
v2
Comments
Stm21 3-Feb-19 13:39pm    
Thank you for your quick response
The data i received from unit is like this FF A1 00 00 00 00 00 00 00 00 i want to separate only the 00 or the 01 and paint the panel.for ex. if the response is "01 01 01 01 01 01 01 01" all the panel will be change color to red.
RickZeeland 3-Feb-19 14:32pm    
See the updated solution, it is not very elegant but simple to understand I hope :)
Stm21 3-Feb-19 14:55pm    
thank you very much.
This ex help me a lot but i receive the status in a textbox.
Stm21 3-Feb-19 14:59pm    
i think i made it
string data = textBox1.Text;
RickZeeland 3-Feb-19 15:13pm    
Correct, good luck finishing the job !
Can i ask again ?
System.IndexOutOfRangeException
C#
if (words[2].Equals("00"))
            {
                panel3.BackColor = Color.Green;
            }
            else
            {
                panel3.BackColor = Color.Red;
            }
 
Share this answer
 
Comments
Ralf Meier 4-Feb-19 9:24am    
Don't ask a further question as a Solution ...

This means that you try to access an Array-Element which doesn't exists - here your array 'words' don't has the element '[2]' - I suppose it has only the 2 elements '[0]' and '[1]' ...
C#
<pre>public partial class Form1 : Form
    {
        Communication comm;
        Int32 count = 0;


        public Form1()
        {
            InitializeComponent();
            comm = Communication.Instance();
            comm.Communication_Set(9600, 8);
            comboBox1.Items.Clear();
            this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form_Closing);
            foreach (string selCOM in comm.selectCOM())
            {
                comboBox1.Items.Add(selCOM);
            }
            comboBox1.Sorted = true;
            label1.Text = "";
        }

        private void openCOM_Click(object sender, EventArgs e)
        {
            try
            {
                label1.Text = comm.openCOM(comboBox1.Items[comboBox1.SelectedIndex].ToString());
            }

            catch
            {
                label1.Text = "First select COM port!";
            }
        }

        private void closeCOM_Click(object sender, EventArgs e)
        {
            label1.Text = comm.CloseCOM();
        }

        private void sendData_Click(object sender, EventArgs e)
        {
            if (textBox1.Text != string.Empty)
            {
                string hexString = textBox1.Text;
                byte[] rxByte = new byte[1];



                if (!comm.serialWrite(stringToHEX(hexString), 0, stringToHEX(hexString).Length)) 
                { 
                    label1.Text = "Cannot send data!Try to open COM port first!"; 
                    return; 
                }
                Thread.Sleep(50);
                //textBox2.Text += count.ToString() + "";
                while (comm.serialBytesToRead() > 0)
                {
                    rxByte[0] = comm.serialRead();
                    textBox2.Text += ByteToHex(rxByte).ToString();
                }
                //textBox2.Text += " \r\n";
            }
        }


        private void Clear_Click(object sender, EventArgs e)
        {
            textBox2.Text = string.Empty;
        }


        private string ByteToHex(byte[] data)
        {
            StringBuilder sb = new StringBuilder(data.Length * 3);
            foreach (byte b in data)
                sb.Append(Convert.ToString(b, 16).PadRight(3, ' ')); // edw
            return sb.ToString().ToUpper();
        }
        public byte[] stringToHEX(string s)
        {
            char[] splitter = {};
            string[] splitS = s.Split(splitter, StringSplitOptions.RemoveEmptyEntries);

            byte[] buff = new byte[splitS.Length];

            for (int i = 0; i < splitS.Length; i++)
            {
                try
                {
                    buff[i] = Convert.ToByte(splitS[i], 16);
                }

                catch
                {
                    //buff[i] = 0x00;
                }
            }
            return buff;
        }


        private void Form_Closing(object sender, FormClosingEventArgs e)
        {

            if (comm.serialIsOpen()) comm.CloseCOM();

        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            string data = textBox2.Text;
            string[] words = data.Split(' ');

            if (words[0].Equals("FF"))
            {
                panel1.BackColor = Color.Green;
            }
            else
            {
                panel1.BackColor = Color.Red;
            }
            if (words[1].Equals("A1"))
            {
                panel2.BackColor = Color.Green;
            }
            else
            {
                panel2.BackColor = Color.Red;
            }
            if (words[2].Equals("00"))
            {
                panel3.BackColor = Color.Green;
            }
            else
            {
                panel3.BackColor = Color.Red;
            }

        }
        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            sendData.PerformClick();

        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            Clear.PerformClick();
        }
 
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