Solved

.NET SerialPort DataReceived problem

Posted on 2006-06-29
3
1,785 Views
Last Modified: 2012-06-21
Hi,

I am using 2.0 and the SerialPort class to make and CSD data call and then parse the incoming data via DataReceived.

I am also using Portmon to analyse the actual data that is incoming through the port. So I can make sure I am getting everything correct.

I have been experiencing a few problems with picking up the data from the remote modem.

Randomly when I make a datacall every other packet sent from the remote modem, which can be seen in full via Portmon, is sent through as null via the DataReceived event. The event is being called but the data not included. The alternate packet is fine! - i.e data seen in portmon is sent via DataReceived.

To make the matter more harded to pin point, Somtimes when I connect EVERY packet is sent and rec totally fine, and there is no problem. This is what confuses me, why it works one time then not the other!!

Does anyone have a clue why this could be??

At first I thought it could be a result of signal strength on the remote modem, but the correct information is coming through ok as its shown in portmon.

Then i thought, and still do, is that the problem must be with my code somewhere.

I have tried reading from the port in bytes, char, readexisting, readline all with the same result.

I have also tried different encodings.

Hopefully I'm missing something somewhere.

Any help would be great., thanks in advance.

My current code;


         public bool OpenPort()
        {
            if (!mPort.IsOpen)
            {                
                try
                {
                    mPort.PortName = "COM1";
                    mPort.Handshake = Handshake.RequestToSendXOnXOff;
                    mPort.ReadTimeout = 50000;
                    mPort.Encoding = System.Text.Encoding.ASCII;
                    mPort.WriteTimeout = 50000;
                    mPort.Parity = Parity.None;
                    mPort.BaudRate = 9600;
                    mPort.StopBits = StopBits.One;
                    mPort.DataBits = 8;
                    mPort.DtrEnable = true;

                    mPort.Open();

                    return true;
                }
                catch
                {
                    // add relevant exceptions here
                    AddToLogFile("Can not open port");
                    return false;
                }
            }
            else
            {
                return true;
            }
        }

 // this is hooked up elsewhere      
 mPort.DataReceived += new SerialDataReceivedEventHandler(mPort_DataRecDownloadLog);

        public void mPort_DataRecDownloadLog(object sender, EventArgs e)
        {
                if (mPort.IsOpen)
                {
                   
                    int bnread = mPort.BytesToRead;
                    char[] a_char = new char[bnread];
                    int nread = 0;
                    nread = mPort.Read(a_char, 0, bnread);
                    string str = new string(a_char, 0, nread);
                   
                    ParseBuffer(str);
                 }
         }
0
Comment
Question by:jendy
3 Comments
 
LVL 5

Accepted Solution

by:
t_itanium earned 500 total points
Comment Utility
hi...
i have thiis problem once.. i dont know but i think that the data is sent some times in different parts...


i had once a weight scale that gives 5 digit wight with the unit at right ie:
11111 g

some times it works fine and some times it gives to inout  111 then 11 g

so it might be this is your problem ..i overcome this by using the system.threading

and use Thread.Sleep(time)

example:

  public void mPort_DataRecDownloadLog(object sender, EventArgs e)
        {
                if (mPort.IsOpen)
                {
                    Thread.Sleep(500);   //or 1000
                    int bnread = mPort.BytesToRead;
                    char[] a_char = new char[bnread];
                    int nread = 0;
                    nread = mPort.Read(a_char, 0, bnread);
                    string str = new string(a_char, 0, nread);
                   
                    ParseBuffer(str);
                 }
         }

0
 

Author Comment

by:jendy
Comment Utility
Sorry, forgot about this one!

Thanks t_itanium, adding the wait did solve the problem.

It doesn't seem the mosst accurate method to me, but I haven't found anything else that works so I'll run with this for now.

Thanks again.

jendy
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

763 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

5 Experts available now in Live!

Get 1:1 Help Now