Click here to Skip to main content
14,870,616 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi Guys. I have a problem like this. I am writing a program about communication between the device and the computer.
No problem when I test it with virtual serial port. But when there is a physical connection, the data is not complete. Below are examples. I will be glad if you help. thanks.

Not: Two data is same.

Virtual Port Data (Correct data )

Gelen  : [ENQ]
Giden  : [ACK]
Gelen  : [STX]1H|\^&|||XS^00-21^72463^^^^05342311||||||||E1394-97[CR][ETX]54[CR][LF]
Giden  : [ACK]
Gelen  : [STX]2P|1[CR][ETX]3F[CR][LF]
Giden  : [ACK]
Gelen  : [STX]3C|1[CR][ETX]33[CR][LF]
Giden  : [ACK]
Gelen  : [STX]4O|1||^^        0834106^A|^^^^WBC\^^^^RBC\^^^^HGB\^^^^HCT\^^^^MCV\^^^^MCH\^^^^MCHC\^^^^PLT\^^^^NEUT%\^^^^LYMPH%\^^^^MONO%\^^^^EO%\^^^^BASO%\^^^^NEUT#\^^^^LYMPH#\^^^^MONO#\^^^^EO#\^^^^BASO#\^^^^RDW-SD\^^^^RDW-CV\^^^^PDW\^^^^MPV\^^^^P-LCR\^^^^[ETB]A4[CR][LF]
Giden  : [ACK]
Gelen  : [STX]5PCT|||||||N||||||||||||||F[CR][ETX]EC[CR][LF]
Giden  : [ACK]
Gelen  : [STX]6C|1[CR][ETX]36[CR][LF]
Giden  : [ACK]
Gelen  : [STX]7R|1|^^^^WBC^1|7.61|10*3/uL||N||||||20160721144117[CR][ETX]0A[CR][LF]
Giden  : [ACK]
Gelen  : [STX]0R|2|^^^^RBC^1|4.40|10*6/uL||N||||||20160721144117[CR][ETX]FC[CR][LF]
Giden  : [ACK]
Gelen  : [STX]1R|3|^^^^HGB^1|11.6|g/dL||N||||||20160721144117[CR][ETX]8D[CR][LF]
Giden  : [ACK]
Gelen  : [STX]2R|4|^^^^HCT^1|35.5|%||N||||||20160721144117[CR][ETX]81[CR][LF]
Giden  : [ACK]
Gelen  : [STX]3R|5|^^^^MCV^1|80.7|fL||L||||||20160721144117[CR][ETX]17[CR][LF]
Giden  : [ACK]
Gelen  : [STX]4R|6|^^^^MCH^1|26.4|pg||N||||||20160721144117[CR][ETX]2F[CR][LF]
Giden  : [ACK]
Gelen  : [STX]5R|7|^^^^MCHC^1|32.7|g/dL||N||||||20160721144117[CR][ETX]E3[CR][LF]
Giden  : [ACK]
Gelen  : [STX]6R|8|^^^^PLT^1|241|10*3/uL||N||||||20160721144117[CR][ETX]EF[CR][LF]
Giden  : [ACK]
Gelen  : [STX]7R|9|^^^^NEUT%^1|73.1|%||W||||||20160721144117[CR][ETX]14[CR][LF]
Giden  : [ACK]
Gelen  : [STX]0R|10|^^^^LYMPH%^1|18.7|%||W||||||20160721144117[CR][ETX]88[CR][LF]
Giden  : [ACK]
Gelen  : [STX]1R|11|^^^^MONO%^1|7.6|%||W||||||20160721144117[CR][ETX]06[CR][LF]
Giden  : [ACK]
Gelen  : [STX]2R|12|^^^^EO%^1|0.3|%||N||||||20160721144117[CR][ETX]50[CR][LF]
Giden  : [ACK]
Gelen  : [STX]3R|13|^^^^BASO%^1|0.3|%||N||||||20160721144117[CR][ETX]E3[CR][LF]
Giden  : [ACK]
Gelen  : [STX]4R|14|^^^^NEUT#^1|5.57|10*3/uL||W||||||20160721144117[CR][ETX]CA[CR][LF]
Giden  : [ACK]
Gelen  : [STX]5R|15|^^^^LYMPH#^1|1.42|10*3/uL||W||||||20160721144117[CR][ETX]10[CR][LF]
Giden  : [ACK]
Gelen  : [STX]6R|16|^^^^MONO#^1|0.58|10*3/uL||W||||||20160721144117[CR][ETX]C7[CR][LF]
Giden  : [ACK]
Gelen  : [STX]7R|17|^^^^EO#^1|0.02|10*3/uL||N||||||20160721144117[CR][ETX]10[CR][LF]
Giden  : [ACK]
Gelen  : [STX]0R|18|^^^^BASO#^1|0.02|10*3/uL||N||||||20160721144117[CR][ETX]9B[CR][LF]
Giden  : [ACK]
Gelen  : [STX]1R|19|^^^^RDW-SD^1|37.0|fL||N||||||20160721144117[CR][ETX]12[CR][LF]
Giden  : [ACK]
Gelen  : [STX]2R|20|^^^^RDW-CV^1|13.0|%||N||||||20160721144117[CR][ETX]7A[CR][LF]
Giden  : [ACK]
Gelen  : [STX]3R|21|^^^^PDW^1|13.1|fL||N||||||20160721144117[CR][ETX]42[CR][LF]
Giden  : [ACK]
Gelen  : [STX]4R|22|^^^^MPV^1|11.1|fL||N||||||20160721144117[CR][ETX]4A[CR][LF]
Giden  : [ACK]
Gelen  : [STX]5R|23|^^^^P-LCR^1|32.9|%||N||||||20160721144117[CR][ETX]35[CR][LF]
Giden  : [ACK]
Gelen  : [STX]6R|24|^^^^PCT^1|0.27|%||N||||||20160721144117[CR][ETX]BB[CR][LF]
Giden  : [ACK]
Gelen  : [STX]7R|25|^^^^Blasts?|10|||||||||20160721144117[CR][ETX]16[CR][LF]
Giden  : [ACK]
Gelen  : [STX]0R|26|^^^^Immature_Gran?|10|||||||||20160721144117[CR][ETX]D2[CR][LF]
Giden  : [ACK]
Gelen  : [STX]1R|27|^^^^Left_Shift?|0|||||||||20160721144117[CR][ETX]60[CR][LF]
Giden  : [ACK]
Gelen  : [STX]2R|28|^^^^NRBC?|10|||||||||20160721144117[CR][ETX]D0[CR][LF]
Giden  : [ACK]
Gelen  : [STX]3R|29|^^^^Atypical_Lympho?|100|||A||||||20160721144117[CR][ETX]2D[CR][LF]
Giden  : [ACK]
Gelen  : [STX]4R|30|^^^^Abn_Lympho?|0|||||||||20160721144117[CR][ETX]5E[CR][LF]
Giden  : [ACK]
Gelen  : [STX]5R|31|^^^^RBC_Agglutination?|60|||||||||20160721144117[CR][ETX]39[CR][LF]
Giden  : [ACK]
Gelen  : [STX]6R|32|^^^^Turbidity/HGB_Interference?|80|||||||||20160721144117[CR][ETX]AA[CR][LF]
Giden  : [ACK]
Gelen  : [STX]7R|33|^^^^Iron_Deficiency?|80|||||||||20160721144117[CR][ETX]9D[CR][LF]
Giden  : [ACK]
Gelen  : [STX]0R|34|^^^^HGB_Defect?|90|||||||||20160721144117[CR][ETX]29[CR][LF]
Giden  : [ACK]
Gelen  : [STX]1R|35|^^^^Fragments?|0|||||||||20160721144117[CR][ETX]1E[CR][LF]
Giden  : [ACK]
Gelen  : [STX]2R|36|^^^^PLT_Clumps?|0|||||||||20160721144117[CR][ETX]3C[CR][LF]
Giden  : [ACK]
Gelen  : [STX]3R|37|^^^^Positive_Morph||||A||||||20160721144117[CR][ETX]05[CR][LF]
Giden  : [ACK]
Gelen  : [STX]4R|38|^^^^SCAT_DIFF|PNG&R&20210326&R&2016_07_21_14_41_0834106_DIFF.PNG|||N||||||20160721144117[CR][ETX]D9[CR][LF]
Giden  : [ACK]
Gelen  : [STX]5R|39|^^^^DIST_RBC|PNG&R&20210326&R&2016_07_21_14_41_0834106_RBC.PNG|||N||||||20160721144117[CR][ETX]60[CR][LF]
Giden  : [ACK]
Gelen  : [STX]6R|40|^^^^DIST_PLT|PNG&R&20210326&R&2016_07_21_14_41_0834106_PLT.PNG|||N||||||20160721144117[CR][ETX]8B[CR][LF]
Giden  : [ACK]
Gelen  : [STX]7L|1|N[CR][ETX]0A[CR][LF]
Giden  : [ACK]
Gelen  : [EOT]


physical connection ( Wrong data )

Gelen  : [ENQ]
Giden  : [ACK]
Gelen  : [STX]1H|\^
Giden  : [ACK]
Gelen  : &|||XS^00-21^72463^^^^05342311|
Giden  : [ACK]
Gelen  : |||||||E1394-97[CR][ETX]54[CR][LF][STX]2P|1[CR][ETX]3F[CR]
Giden  : [ACK]
Gelen  : [LF][STX]3C|1[CR][ETX]33[CR][LF][STX]4O|1|
Giden  : [ACK]
Gelen  : |^^        0834
Giden  : [ACK]
Gelen  : 106^A|^^^^WBC\^^^^RBC\^^^^HGB\^
Giden  : [ACK]
Gelen  : ^^^HCT\^^^^MCV\^^^^MCH\^^^^MCH
Giden  : [ACK]
Gelen  : C\^^^^PLT\^^^^NEUT%\^^^^LYMPH%\
Giden  : [ACK]
Gelen  : ^^^^MONO%\^^^^E
Giden  : [ACK]
Gelen  : O%\^^^^BASO%\^^^^NEUT#\^^^^LYMP
Giden  : [ACK]
Gelen  : H#\^^^^MONO#\^^^^EO#\^^^^BASO#\
Giden  : [ACK]
Gelen  : ^^^^RDW-SD\^^^^RDW-CV\^^^^PDW\^
Giden  : [ACK]
Gelen  : ^^^MPV\^^^^P-LCR\^^^^[ETB]A4[CR][LF][STX]5PC
Giden  : [ACK]
Gelen  : T|||||||N||||||||||||||F[CR][ETX]EC[CR][LF][STX]
Giden  : [ACK]
Gelen  : 6C|1[CR][ETX]36[CR][LF][STX]7R|1|^^^^WBC^1|7.61|
Giden  : [ACK]
Gelen  : 10*3/uL||N||||||20160721144117
Giden  : [ACK]
Gelen  : [CR][ETX]0A[CR][LF][STX]0R|2|^^^^RBC^1|4.40|10*6
Giden  : [ACK]
Gelen  : /uL||N||||||20160721144117[CR][ETX]FC[CR]
Giden  : [ACK]
Gelen  : [LF][STX]1R|3|^^^^HGB^1|11.6|g/dL||N||
Giden  : [ACK]
Gelen  : ||||20160721144117[CR][ETX]8D[CR][LF][STX]2R|4|^^^^HCT^1|35.5|%
Giden  : [ACK]
Gelen  : ||N||||||20160721144117[CR][ETX]81[CR][LF][STX]
Giden  : [ACK]
Gelen  : 3R|5|^^^^MCV^1|80.7|fL||L||||||
Giden  : [ACK]
Gelen  : 20160721144117[CR][ETX]17[CR][LF][STX]4R|6|^^^^M
Giden  : [ACK]
Gelen  : CH^1|26.4|pg||N||||||2016072114
Giden  : [ACK]
Gelen  : 4117[CR][ETX]2F[CR][LF][STX]5R|7|^^^^MCHC^1|32.
Giden  : [ACK]
Gelen  : 7|g/dL||N||||||20160721144117[CR][ETX]
Giden  : [ACK]
Gelen  : E3[CR][LF][STX]6R|8|^^^^PLT^1|241|10*3/uL
Giden  : [ACK]
Gelen  : ||N||||||20160721144117[CR][ETX]EF[CR][LF][STX]
Giden  : [ACK]
Gelen  : 7R|9|^^^^NEUT%^1
Giden  : [ACK]
Gelen  : |73.1|%||W||||||20160721144117[CR]
Giden  : [ACK]
Gelen  : [ETX]14[CR][LF][STX]0R|10|^^^^LYMPH%^1|18.7|
Giden  : [ACK]
Gelen  : %||W||||||20160721144117[CR][ETX]88[CR][LF][STX]
Giden  : [ACK]
Gelen  : 1R|11|^^^^MONO%^1|7.6|%||W|||||
Giden  : [ACK]
Gelen  : |20160721144117[CR][ETX]06[CR][LF][STX]2R|12|^^
Giden  : [ACK]
Gelen  : ^^EO%^1|0.3|%||N||||||201607211
Giden  : [ACK]
Gelen  : 44117[CR][ETX]50[CR][LF][STX]3R|13|^^^^BASO%^1|0
Giden  : [ACK]
Gelen  : .3|%||N||||||20160721144117[CR][ETX]E3
Giden  : [ACK]
Gelen  : [CR][LF][STX]4R|14|^^^^NEUT#^1|5.57|10*3
Giden  : [ACK]
Gelen  : /uL||W||||||20160721144117[CR][ETX]CA[CR]
Giden  : [ACK]
Gelen  : [LF][STX]5R|15|^^^^LYMPH#^1|1.42|10*3/
Giden  : [ACK]
Gelen  : uL||W||||||20160721144117[CR][ETX]10[CR]
Giden  : [ACK]
Gelen  : [LF][STX]6R|16|^^^^MONO#^1|0.58|10*3/u
Giden  : [ACK]
Gelen  : L||W||||||20160721144117[CR][ETX]C7[CR][LF][STX]
Giden  : [ACK]
Gelen  : 7R|17|^^^^EO#^1|0.02|10*3/uL||N
Giden  : [ACK]
Gelen  : ||||||20160721144117[CR][ETX]10[CR][LF][STX]0R|18|^^^^BASO#^1|0
Giden  : [ACK]
Gelen  : .02|10*3/uL||N||||||2016072114
Giden  : [ACK]
Gelen  : 4117[CR][ETX]9B[CR][LF][STX]1R|19|^^^^RDW-SD^1|3
Giden  : [ACK]
Gelen  : 7.0|fL||N||||||20160721144117[CR][ETX]12[CR][LF][STX]2R|20|^^^^
Giden  : [ACK]
Gelen  : RDW-CV^1|13.0|%||N||||||2016072
Giden  : [ACK]
Gelen  : 1144117[CR][ETX]7A[CR][LF][STX]3R|21|^^^^PDW^1|1
Giden  : [ACK]
Gelen  : 3.1|fL||N||||||20160721144117[CR]
Giden  : [ACK]
Gelen  : [ETX]42[CR][LF][STX]4R|22|^^^^MPV^1|11.1|fL||
Giden  : [ACK]
Gelen  : N||||||20160721144117[CR][ETX]4A[CR][LF][STX]5R|
Giden  : [ACK]
Gelen  : 23|^^^^P-LCR^1|32.9|%||N||||||
Giden  : [ACK]
Gelen  : 20160721144117[CR][ETX]35[CR][LF][STX]6R|24|^^^^
Giden  : [ACK]
Gelen  : PCT^1|0.27|%||N||||||2016072114
Giden  : [ACK]
Gelen  : 4117[CR][ETX]BB[CR][LF][STX]7R|25|^^^^Blasts?|10
Giden  : [ACK]
Gelen  : |||||||||20160721144117[CR][ETX]16[CR][LF][STX]
Giden  : [ACK]
Gelen  : 0R|26|^^^^Immature_Gran?|10||||
Giden  : [ACK]
Gelen  : |||||20160721144117[CR][ETX]D2[CR][LF][STX]1R|27|^^^^Left_Shift
Giden  : [ACK]
Gelen  : ?|0|||||||||20160721144117[CR][ETX]60[CR]
Giden  : [ACK]
Gelen  : [LF][STX]2R|28|^^^^NRBC?|10|||||||||20
Giden  : [ACK]
Gelen  : 160721144117[CR][ETX]D0[CR][LF][STX]3R|29|^^^^A
Giden  : [ACK]
Gelen  : typical_Lympho?|100|||A||||||20
Giden  : [ACK]
Gelen  : 160721144117[CR][ETX]2D[CR][LF][STX]4R|30|^^^^Ab
Giden  : [ACK]
Gelen  : n_Lympho?|0|||||||||2016072114
Giden  : [ACK]
Gelen  : 4117[CR][ETX]5E[CR][LF][STX]5R|31|^^^^RBC_Agglut
Giden  : [ACK]
Gelen  : ination?|60|||||||||20160721144
Giden  : [ACK]
Gelen  : 117[CR][ETX]39[CR][LF][STX]6R|32|^^^^Turbidity/H
Giden  : [ACK]
Gelen  : GB_Interference?|80|||||||||20
Giden  : [ACK]
Gelen  : 160721144117[CR][ETX]AA[CR][LF][STX]7R|33|^^^^Iron_Deficiency?|
Giden  : [ACK]
Gelen  : 80|||||||||20160721144117[CR][ETX]9D[CR][LF]
Giden  : [ACK]
Gelen  : [STX]0R|34|^^^^HGB_Defect?|90||||||
Giden  : [ACK]
Gelen  : |||20160721144117[CR][ETX]29[CR][LF][STX]1R|35|^
Giden  : [ACK]
Gelen  : ^^^Fragments?|0|||||||||201607
Giden  : [ACK]
Gelen  : 21144117[CR][ETX]1E[CR][LF][STX]2R|36|^^^^PLT_Cl
Giden  : [ACK]
Gelen  : umps?|0|||||||||20160721144117[CR]
Giden  : [ACK]
Gelen  : [ETX]3C[CR][LF][STX]3R|37|^^^^Positive_Morph
Giden  : [ACK]
Gelen  : ||||A||||||20160721144117[CR][ETX]05[CR][LF][STX]4R|38|^^^^SCAT_
Giden  : [ACK]
Gelen  : DIFF|PNG&R&20210326&R&2016_07_
Giden  : [ACK]
Gelen  : 21_14_41_0834106_DIFF.PNG|||N||
Giden  : [ACK]
Gelen  : ||||20160721144117[CR][ETX]D9[CR][LF][STX]5R|39|
Giden  : [ACK]
Gelen  : ^^^^DIST_RBC|PNG&R&20210326&R&2016_07_21_14_41
Giden  : [ACK]
Gelen  : _0834106_RBC.PNG|||N||||||2016
Giden  : [ACK]
Gelen  : 0721144117[CR][ETX]60[CR][LF][STX]6R|40|^^^^DIST
Giden  : [ACK]
Gelen  : _PLT|PNG&R&20210326&R&2016_07_2
Giden  : [ACK]
Gelen  : 1_14_41_0834106_PLT.PNG|||N||||||2016072114411
Giden  : [ACK]
Gelen  : 7[CR][ETX]8B[CR][LF][STX]7L|1|N[CR][ETX]0A[CR][LF][EOT]
Giden  : [ACK]


What I have tried:

C#
private void SerialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
       {
           indata = serialPort1.ReadExisting();

           string route = indataclean(indata);

           int a = 0;
           foreach (char i in indata)
           {
               a += Convert.ToInt32(i);
           }

           switch (a)
           {
               case 6: //ACK
                   serialPort1.Write(ACK);
                   textBox7.Text += "Giden  : " + g + "ACK" + f + "\r\n";
                   break;
               case 21: //NAK

                   break;
               case 4: //EOT
                   textBox7.Text += "Gelen  : " + route + "\r\n";
                   break;
               case 5: //ENQ
                   textBox7.Text += "Gelen  : " + route + "\r\n";
                   serialPort1.Write(ACK);
                   textBox7.Text += "Giden  : " + g + "ACK" + f + "\r\n";
                   break;
               case 3: //ETX

                   break;
               case 2: //STX
                   serialPort1.Write(ACK);
                   break;
               default:

                   textBox7.Text += "Gelen  : " + route + "\r\n";
                   parseData();
                   serialPort1.Write(ACK);
                   textBox7.Text += "Giden  : " + g + "ACK" + f + "\r\n";
                   break;
           }
       }

private void Button1_Click(object sender, EventArgs e)
       {
           try
           {
               serialPort1.PortName = portBox.Text;
               serialPort1.BaudRate = 9600;
               serialPort1.DataBits = 8;
               serialPort1.StopBits = StopBits.One;
               serialPort1.Parity = Parity.None;
               serialPort1.ReadTimeout = 1000;
               serialPort1.WriteTimeout = 1000;
               serialPort1.DtrEnable = true;
               serialPort1.RtsEnable = true;
               serialPort1.ReadBufferSize = 4096;
               serialPort1.WriteBufferSize = 2048;
               serialPort1.Open();

           }
           catch(Exception ex)
           {

           }
       }
Posted
Updated 27-Mar-21 7:22am
v3

Your problem, I suppose, is that you read what is inside the Buffer without checking if the right count of Characters are inside the Buffer.
The Serialport_DataReceived-Event is raised with the first incomming Character. So perhaps your Application is awaiting 10 Characters - but there are, when you read the Buffer, only 6 Characters in it. This is what you need to change.
You should realize that a Serialport is not something quiet fast ...
   
Comments
EssenceGold 26-Mar-21 13:30pm
   
Thanks for your answer. Can you show me with an example or where do I need to change it in my code.
Ralf Meier 26-Mar-21 13:43pm
   
Not really. It depends on your Communication-Protokoll.
The Serailport has the Property BytesToRead. This Property gives you the Information how much Bytes are actually inside the Buffer.
If your are awaiting (for exmaple) 10 Chars and there are only 6 in it you should exit the Method - with the next incomming Char the Event raises again.
If the Buffer contains more than 10 Chars - but you need only 10 - you should not ReadExisting - read only those which are needed by you.

Another Approach could be not to use the DataReceived-Event. You could also use a Timer (for example each 100 ms) and check there if the count of Chars are matching to what you need.

Give it a try - you know much better than I what is needed and when ...
Hello. I tried a little and I would fix the problem as follows Thanks to everyone who is interested. 


C#
private void gelenData()
        {
                string route = indataclean(indata);

                int a = 0;
                foreach (char i in indata)
                {
                    a += Convert.ToInt32(i);
                }

                switch (a)
                {
                    case 6: //ACK
                        serialPort1.Write(ACK);
                        textBox7.Text += "Giden  : "[ACK]\r\n";
                        break;
                    case 4: //EOT
                        textBox7.Text += "Gelen  : " + route + "\r\n";
                        break;
                    case 5: //ENQ
                        textBox7.Text += "Gelen  : " + route + "\r\n";
                        serialPort1.Write(ACK);
                        textBox7.Text += "Giden  : "[ACK]\r\n";
                        break;
                    case 2: //STX
                        serialPort1.Write(ACK);
                        break;
                    default:
                        textBox7.Text += "Gelen  : " + route + "\r\n";
                        parseData();
                        serialPort1.Write(ACK);
                        textBox7.Text += "Giden  : "[ACK]\r\n";
                        break;
                }
       
            indata = "";         
        }

        private void SerialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            byte[] data = new byte[serialPort1.BytesToRead];
            serialPort1.Read(data, 0, data.Length);
            indata += Encoding.ASCII.GetString(data);

            if (indata.Contains(LF) || indata.Contains(ENQ) || indata.Contains(EOT))
            {
                gelenData();
            }
            
        }
   
Comments
Ralf Meier 28-Mar-21 6:50am
   
not quiet correct ... I suppose.
As I see each incomming sequence is ended with a LF - I haven't seen that before.
Your Solution might work (in the moment - but could make an error in future). You look if your incomming sequence contains a LF - I would change it to that your incomming data must End with LF ot ENQ or EOT. Also you should work only with that String-part which is ended with one of these Chars ...

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