[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Serial Port Communication.

Posted on 2011-10-20
3
Medium Priority
?
1,299 Views
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?

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

0
Comment
Question by:ocaccy
  • 2
3 Comments
 
LVL 18

Accepted Solution

by:
Jose Parrot earned 2000 total points
ID: 37009195
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
 

Author Comment

by:ocaccy
ID: 37009411
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
 

Author Closing Comment

by:ocaccy
ID: 37009421
Faced with so much weight wise, I humbly bow as a sign of deep respect.
The answer was the solution!
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Monitor input from a computer is usually nothing special.  In this instance it prevented anyone from using the computer.  This was a preconfiguration that didn't work.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

868 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