Serial Port Communication.

Posted on 2011-10-20
Last Modified: 2012-05-12
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?

/// <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 });

            // 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);

                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;

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

            textBox1.Text = status;

Open in new window

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

            for (int i = 0; i <= 6; i++)
                string vstr = "ID0" + i.ToString();
                vstr = ConvertEscapeSequences(vstr);

Open in new window

Question by:ocaccy
    LVL 18

    Accepted Solution

    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

    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.


    Author Comment

    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: 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.

    Author Closing Comment

    Faced with so much weight wise, I humbly bow as a sign of deep respect.
    The answer was the solution!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Suggested Solutions

    This is about my first experience with programming Arduino.
    In the modern office, employees tend to move around the workplace a lot more freely. Conferences, collaborative groups, flexible seating and working from home require a new level of mobility. Technology has not only changed the behavior and the expe…
    This video discusses moving either the default database or any database to a new volume.
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now