Click here to Skip to main content
15,887,344 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Hi guys,I wrote a code about serial communication and i wanna send data also receive data.Send data part is right but i should do circular buffer receive data part.I did something unfortunately after loading incoming data is stop because code couldnt find header right.As a result i couldnt it circular buffer.How can you help me?Sorry for my English.With my best regards.

What I have tried:

namespace SeriPort {     //Established by Can �z     public partial class Form1 : Form     {         int i = 0;         int offsetIndex = 0;         byte[] sentData = new byte[43];         byte[] message = new byte[43];         byte[] byte_buffer = new byte[131072];         int Freq = 0;         int receivedValidMessageCounter = 0;         //string deneme = "";         int lastDataReceived=0;         public Form1()         {             CheckForIllegalCrossThreadCalls = false;             InitializeComponent();                     }         private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)         {             int bufferSize = serialPort1.BytesToRead;             if (bufferSize > 0 && offsetIndex + message.Length < byte_buffer.Length)             {                 serialPort1.Read(byte_buffer, offsetIndex, message.Length);                 offsetIndex = offsetIndex + message.Length;//Reading Index             }             else {                 int lastDataReceived = byte_buffer.Length - offsetIndex;                 serialPort1.Read(byte_buffer, offsetIndex, lastDataReceived);                 offsetIndex = 0;                 //i = 0;                 serialPort1.Read(byte_buffer, offsetIndex, message.Length - lastDataReceived);             }         }         private void button2_Click(object sender, EventArgs e)         {             textBox2.Text = textBox2.Text.ToString().Replace(".", ",");             textBox3.Text = textBox3.Text.ToString().Replace(".", ",");             textBox4.Text = textBox4.Text.ToString().Replace(".", ",");             textBox5.Text = textBox5.Text.ToString().Replace(".", ",");             textBox6.Text = textBox6.Text.ToString().Replace(".", ",");             textBox7.Text = textBox7.Text.ToString().Replace(".", ",");             textBox8.Text = textBox8.Text.ToString().Replace(".", ",");             textBox9.Text = textBox9.Text.ToString().Replace(".", ",");             textBox10.Text = textBox10.Text.ToString().Replace(".", ",");             textBox11.Text = textBox11.Text.ToString().Replace(".", ",");                 sentData[0] = 0xC1;                 sentData[1] = 1;                 for (int f = 0; f < 4; f++)                 {                     float temp1 = Convert.ToSingle(textBox2.Text);                     byte[] data1 = BitConverter.GetBytes(temp1);                     sentData[5 - f] = data1[f];                     float temp2 = Convert.ToSingle(textBox3.Text);                     byte[] data2 = BitConverter.GetBytes(temp2);                     sentData[9 - f] = data2[f];                     float temp3 = Convert.ToSingle(textBox4.Text);                     byte[] data3 = BitConverter.GetBytes(temp3);                     sentData[13 - f] = data3[f];                     float temp4 = Convert.ToSingle(textBox5.Text);                     byte[] data4 = BitConverter.GetBytes(temp4);                     sentData[17 - f] = data4[f];                     float temp5 = Convert.ToSingle(textBox6.Text);                     byte[] data5 = BitConverter.GetBytes(temp5);                     sentData[21 - f] = data5[f];                     float temp6 = Convert.ToSingle(textBox7.Text);                     byte[] data6 = BitConverter.GetBytes(temp6);                     sentData[25 - f] = data6[f];                     float temp7 = Convert.ToSingle(textBox8.Text);                     byte[] data7 = BitConverter.GetBytes(temp7);                     sentData[29 - f] = data7[f];                     float temp8 = Convert.ToSingle(textBox9.Text);                     byte[] data8 = BitConverter.GetBytes(temp8);                     sentData[33 - f] = data8[f];                     float temp9 = Convert.ToSingle(textBox10.Text);                     byte[] data9 = BitConverter.GetBytes(temp9);                     sentData[37 - f] = data9[f];                     float temp10 = Convert.ToSingle(textBox11.Text);                     byte[] data10 = BitConverter.GetBytes(temp10);                     sentData[41 - f] = data10[f];                 }                                             sentData[42] = CalCheckSum(sentData);                 serialPort1.Write(sentData, 0, 43);                    }         private void timer2_Tick(object sender, EventArgs e)         {             if (offsetIndex + message.Length < byte_buffer.Length)             {                 try                 {                     while (IsReceivedMessageValid())                     {                         for (int t = 0; t < message.Length; t++)                         {                             message[t] = byte_buffer[i + t];                         }                         float message1 = System.BitConverter.ToSingle(message, 2);                         textBox12.Text = message1.ToString();                         float message2 = System.BitConverter.ToSingle(message, 6);                         textBox13.Text = message2.ToString();                         float message3 = System.BitConverter.ToSingle(message, 10);                         textBox14.Text = message3.ToString();                         float message4 = System.BitConverter.ToSingle(message, 14);                         textBox15.Text = message4.ToString();                         float message5 = System.BitConverter.ToSingle(message, 18);                         textBox16.Text = message5.ToString();                         float message6 = System.BitConverter.ToSingle(message, 22);                         textBox17.Text = message6.ToString();                         float message7 = System.BitConverter.ToSingle(message, 26);                         textBox18.Text = message7.ToString();                         float message8 = System.BitConverter.ToSingle(message, 30);                         textBox19.Text = message8.ToString();                         float message9 = System.BitConverter.ToSingle(message, 34);                         textBox20.Text = message9.ToString();                         float message10 = System.BitConverter.ToSingle(message, 38);                         textBox21.Text = message10.ToString();                         receivedValidMessageCounter++;                         Freq++;                         if (i < byte_buffer.Length - 44)                         {                             i = i + message.Length;                         }                         this.Text = "Frequency: " + Freq.ToString() + "Hz" + "- TotalDataReceived:" + receivedValidMessageCounter.ToString();                     }                 }                 catch                 {                     while (IsReceivedMessageValid2())                     {                         for (int t = 0; t < lastDataReceived; t++)                         {                             message[t] = byte_buffer[i + t];                         }                         for (int v = 0; v < message.Length - lastDataReceived; v++, lastDataReceived++)                         {                             message[lastDataReceived] = byte_buffer[v];                         }                         float message1 = System.BitConverter.ToSingle(message, 2);                         textBox12.Text = message1.ToString();                         float message2 = System.BitConverter.ToSingle(message, 6);                         textBox13.Text = message2.ToString();                         float message3 = System.BitConverter.ToSingle(message, 10);                         textBox14.Text = message3.ToString();                         float message4 = System.BitConverter.ToSingle(message, 14);                         textBox15.Text = message4.ToString();                         float message5 = System.BitConverter.ToSingle(message, 18);                         textBox16.Text = message5.ToString();                         float message6 = System.BitConverter.ToSingle(message, 22);                         textBox17.Text = message6.ToString();                         float message7 = System.BitConverter.ToSingle(message, 26);                         textBox18.Text = message7.ToString();                         float message8 = System.BitConverter.ToSingle(message, 30);                         textBox19.Text = message8.ToString();                         float message9 = System.BitConverter.ToSingle(message, 34);                         textBox20.Text = message9.ToString();                         float message10 = System.BitConverter.ToSingle(message, 38);                         textBox21.Text = message10.ToString();                         receivedValidMessageCounter++;                         Freq++;                         i = message.Length - lastDataReceived;                         this.Text = "Frequency: " + Freq.ToString() + "Hz" + "- TotalDataReceived:" + receivedValidMessageCounter.ToString();                     }                 }                 /* else                  {                      while (IsReceivedMessageValid2())                      {                          for (int t = 0; t < lastDataReceived; t++)                          {                              message[t] = byte_buffer[i + t];                          }                          for (int v = 0; v < message.Length - lastDataReceived; v++, lastDataReceived++)                          {                              message[lastDataReceived] = byte_buffer[v];                          }                          float message1 = System.BitConverter.ToSingle(message, 2);                          textBox12.Text = message1.ToString();                          float message2 = System.BitConverter.ToSingle(message, 6);                          textBox13.Text = message2.ToString();                          float message3 = System.BitConverter.ToSingle(message, 10);                          textBox14.Text = message3.ToString();                          float message4 = System.BitConverter.ToSingle(message, 14);                          textBox15.Text = message4.ToString();                          float message5 = System.BitConverter.ToSingle(message, 18);                          textBox16.Text = message5.ToString();                          float message6 = System.BitConverter.ToSingle(message, 22);                          textBox17.Text = message6.ToString();                          float message7 = System.BitConverter.ToSingle(message, 26);                          textBox18.Text = message7.ToString();                          float message8 = System.BitConverter.ToSingle(message, 30);                          textBox19.Text = message8.ToString();                          float message9 = System.BitConverter.ToSingle(message, 34);                          textBox20.Text = message9.ToString();                          float message10 = System.BitConverter.ToSingle(message, 38);                          textBox21.Text = message10.ToString();                          receivedValidMessageCounter++;                          Freq++;                          i = message.Length - lastDataReceived;                          this.Text = "Frequency: " + Freq.ToString() + "Hz" + "- TotalDataReceived:" + receivedValidMessageCounter.ToString();                      }                  }*/                 Freq = 0;             }         }         private bool IsReceivedMessageValid()         {                       if (byte_buffer[i] == 0xFA && byte_buffer[i + 1] == 0x01)             {                 byte control = 0x00;                 for (int l = 0; l < message.Length - 1; l++)                 {                     control += byte_buffer[i + l];                                     }                 if (control == byte_buffer[i + message.Length - 1])                     control = Convert.ToByte(256 - control);                     return true;             }             if (i <= byte_buffer.Length-1)                 {                 i++;//write index                 }             return false;         }         private bool IsReceivedMessageValid2() {             if (byte_buffer[i] == 0xFA && byte_buffer[i + 1] == 0x01)             {                 byte control = 0x00;                 for (int l = 0; l < lastDataReceived; l++)                 {                     control += byte_buffer[i + l];                                     }                                 for (int d = 0; d < message.Length - lastDataReceived-1; d++)                 {                     control += byte_buffer[d];                                     }                 if (control == byte_buffer[message.Length - lastDataReceived-1 ])                     return true;             }             if (i <= byte_buffer.Length)             {                 i++;//write index             }             return false;         }         private void Form1_Load(object sender, EventArgs e)         {             string[] portlar = SerialPort.GetPortNames();             comboBox1.Items.AddRange(portlar);             textBox1.Text = Properties.Settings.Default.textbox;             //comboBox1.Text = Properties.Settings.Default.combobox;             if (comboBox1.Items.Contains(Properties.Settings.Default.combobox))                 comboBox1.SelectedIndex = comboBox1.Items.IndexOf(Properties.Settings.Default.combobox);         }         bool c = false;         private void button1_Click(object sender, EventArgs e)         {             if (c)             {                 c = false;                 button1.Text = "Start";                 textBox1.ReadOnly = false;                 comboBox1.Enabled = true;                 label9.Text = "Paused";                 label9.ForeColor = Color.Red;                 if (serialPort1.IsOpen)                 {                     serialPort1.Close();                 }                 else { }                                 timerReceive.Stop();             }             else             {                 c = true;                 try                 {                     //serialPort1 = new SerialPort();                     serialPort1.PortName = comboBox1.Text;                     serialPort1.BaudRate = Convert.ToInt32(textBox1.Text);                     serialPort1.DataBits = 8;                     serialPort1.Parity = Parity.None;                     serialPort1.StopBits = StopBits.One;                     if (!serialPort1.IsOpen)                     {                         serialPort1.Open();                     }                     else                     {                     }                     button1.Text = "Stop";                     textBox1.ReadOnly = true;                     comboBox1.Enabled = false;                     label9.Text = "Sending...";                     label9.ForeColor = Color.Green;                     timerReceive.Start();                 }                 catch                 {                     MessageBox.Show("Please enter number", "Error");                     c = false;                 }             }         }         private void Form1_FormClosed(object sender, FormClosedEventArgs e)         {             if (checkBox1.Checked)             {                 Properties.Settings.Default.textbox = textBox1.Text;                 Properties.Settings.Default.combobox = comboBox1.Text;                 Properties.Settings.Default.Save();             }             else { }         }                 static private byte CalCheckSum(byte[] sentData)         {             byte _CheckSumByte = 0x00;             for (int a = 0; a < sentData.Length - 1; a++)                 _CheckSumByte += sentData[a];             _CheckSumByte = Convert.ToByte(256 - _CheckSumByte);             return _CheckSumByte;         } 
Posted
Updated 24-Jul-19 21:50pm
Comments
OriginalGriff 25-Jul-19 3:40am    
Nobody - but nobody - is going to wade through that pile of unformatted "one line of code" mess to work out what you code might be doing, and what problems you might be meeting doing it. Edit it, and replace that with properly formatted code that we can read. And cut it down to just the relevant fragments as well - we don't want to waste time looking at the whole app even if it is formatted trying to work out where you might be doing something.

So tell us what you tried, where you are stuck.
Help us to help you!
Use the "Improve question" widget to edit your question and provide better information.

1 solution

  • Properly indent your code. I hardly believe someone would look at that mess of posted code.
  • Focus on message capture, make it bullet proof. This is a prerequisite. In order to accomplish this task you could use a very very large buffer (in order to avoid hitting the circular buffer limit).
  • Consider using a ready-made circular buffer implementation (you may find many of them available on the web.
 
Share this answer
 

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