Serial Port Communication.

I have 6 devices connected to the PC via a converter RS485/USB.
If have only one device connected, need only send the command IDXX once get the data.
Example ID01.
However, with two or more devices need to send twice the same IDXX for the device to respond.
I need to receive the data from all six devices at once and for comparison of data.
ID01, ID02, ID03, ID04, ID05, ID06.
I have tried the code below, but is not working.
Can someone help me with the solution?

Regards,
ocaccy
/// <summary>
        /// Handle data received event from serial port.
        /// </summary>
        /// <param name="data">incoming data</param>
        public void OnDataReceived(string dataIn)
        {
            //Handle multi-threading
            if (InvokeRequired)
            {
                Invoke(new StringDelegate(OnDataReceived), new object[] { dataIn });
                return;
            }

            // pause scrolling to speed up output of multiple lines
            bool saveScrolling = scrolling;
            scrolling = false;

            // if we detect a line terminator, add line to output
            int index;
            while (dataIn.Length > 0 &&
                ((index = dataIn.IndexOf("\r")) != -1 ||
                (index = dataIn.IndexOf("\n")) != -1))
            {
                String StringIn = dataIn.Substring(0, index);
                dataIn = dataIn.Remove(0, index + 1);

                logFile_writeLine(AddData(StringIn).Str);
                logFile_writeLine1(AddData(StringIn).Str);
                partialLine = null;	// terminate partial line
            }

            // if we have data remaining, add a partial line
            if (dataIn.Length > 0)
            {
                partialLine = AddData(dataIn);
            }

            // restore scrolling
            scrolling = saveScrolling;
            outputList_Scroll();
            listBox1_Scroll();
        }

        /// <summary>
        /// Update the connection status
        /// </summary>
        public void OnStatusChanged(string status)
        {
            //Handle multi-threading
            if (InvokeRequired)
            {
                Invoke(new StringDelegate(OnStatusChanged), new object[] { status });
                return;
            }

            textBox1.Text = status;
        }

Open in new window

private void button10_Click(object sender, EventArgs e)
        {   
            timr_ID06.Enabled = true;
            textBox2.Clear();
            CommPort com = CommPort.Instance;

            for (int i = 0; i <= 6; i++)
            {
                string vstr = "ID0" + i.ToString();
                vstr = ConvertEscapeSequences(vstr);
                com.Send(vstr);
                Thread.Sleep(500);
            }
        }

Open in new window

ocaccyAsked:
Who is Participating?
 
Jose ParrotConnect With a Mentor Graphics ExpertCommented:
RS485 is just an electrical standard.
It doesn't implies a protocol.

So you need to see if the devices require a terminator command, in order to communicate to all the devices that the next signal is a start command. This is the rule for the ModBus protocol, for instance. If you don't have the manual, it is at http://www.modbus.org/docs/PI_MBUS_300.pdf

If such terminator is absent, each device can interpreter a new command as a "partial" terminator and an error (which is ignored) , and only the next command will be interpreted. I'm not sure if this is the real reason for the issue, because it is not clear if the com.Send() command includes a valid terminator (a single CR LF in ModNus protocol). Take a look at the manual to check that.

Another aspect to analyse is the question of master/slave relationship. If you have just one device, it is the "master", but if you have more than one, you should assing one as the master and the others as slaves, in "reception" mode. It helps to avoid the contention and collisions, very common in multi-drop networks. It is like you have a lot of collisions when all the guys answer and nobody listen them... We need some silence to the right device to be listen.

If you divide the network in master and its slaves, just one device will answer a time. And quickly, so you don't need to wait for the response.

A fourth approach: if you have different brands, it is common to have some issues or even nothing running properly.

My guess is that mastering and slavering the network, you'll solve the issue.

Jose
0
 
ocaccyAuthor Commented:
Thank you Jose.
I was just sending CR, moved to CR and LF.

Among the devices that are all the same make and model, we have a RS485/USB converter.
Should not be this device with internal routines managing this hierarchy: Master-slave?

Thank you for the tips, by the Modbus link: http://www.modbus.org/docs/PI_MBUS_300.pdf and your precious attention.

It really was very helpful.

My sincere thanks, I'm Brazilian and I live in Japan. As thank you, see me very humble, and as a sign of respect, write my name in lowercase letters.
ocaccy
0
 
ocaccyAuthor Commented:
Faced with so much weight wise, I humbly bow as a sign of deep respect.
The answer was the solution!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.