I wrote a c# program where I will send functions to a pic and receive the desired data from it. After the last function is sent, it will loop and send the first function again. However, afterwards the received data is not consistent. Sometimes it has missing data or the received data is not in the right order as it should. I am not sure what the problem is because the data should be received and displayed the same way every time since I am sending the functions in the same order. I already attempted putting the Thread.Sleep function just in case the data is being sent or received too fast but that did not help as much. Any suggestions??? P.s. It is connected through serial port.
My code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO.Ports;
using System.Timers;
using System.IO;
using System.Net;
using System.Threading;
namespace serialreadwrite
{
class Program
{
static void Main(string[] args)
{
#region Initialize Code
SerialPort _serialPort = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One);
#region Initialize Serial and Open
_serialPort.Handshake = Handshake.None;
_serialPort.ReadTimeout = 1000;
_serialPort.WriteTimeout = 1000;
_serialPort.RtsEnable = true;
_serialPort.DtrEnable = true;
_serialPort.Open();
#endregion
int num;
Console.WriteLine("How often will you like to transmit: \nMins:");
num = Convert.ToInt32(Console.ReadLine());
int num2 = 60000 * num;
#endregion
int i = 0;
bool Array_Volt_Flag = false;
bool Array_Amps_Flag = false;
bool Auto_MPPT_Flag = false;
bool Motor_Volt_Flag = false;
bool Motor_Amps_Flag = false;
bool Max_Motor_Flag = false;
bool Motor_RPM_Flag = false;
bool SN_Flag = false;
bool SMC_Type_Flag = false;
bool Auto_MPPV_Flag = false;
string[] temp_Array = null;
while (true) //Loops the whole code (send/receive) over and over
{
string time = Convert.ToString(DateTime.Now); // Added time to the loop
try
{
/*Send*/
string c = Convert.ToString(i);
byte[] array_out = Encoding.ASCII.GetBytes(c);
_serialPort.Write(array_out, 0, array_out.Length);
byte[] array_out2 = new byte[1];
array_out2[0] = 0xD;
_serialPort.Write(array_out2, 0, array_out2.Length);
Thread.Sleep(3000);
int reader = 0;
string fullstring = "";
/*Receive*/
for (int jj = 0; jj < _serialPort.BytesToRead; jj++)
{
reader = _serialPort.ReadByte();
fullstring += Convert.ToChar(reader);
}
string[] Array = fullstring.Split('\r');
for (int x = 0; x < Array.Length; x++)
{
if (Array[x].Contains("Switched") || Array[x].Contains("Action"))
continue;
else if (Array[x].Contains("Array Volt") && Array_Volt_Flag == false)
{
temp_Array = Array[x].Split('=');
if (temp_Array.Length == 2)
{
Console.WriteLine(time + "\n");
Console.WriteLine("Array Volt =" + temp_Array[1]);
Array_Volt_Flag = true;
}
}
else if (Array[x].Contains("Array Amps") && Array_Amps_Flag == false)
{
temp_Array = Array[x].Split('=');
if (temp_Array.Length == 2)
{
Console.WriteLine("Array Amps =" + temp_Array[1]);
Array_Amps_Flag = true;
}
}
else if (Array[x].Contains("Auto MPPT") && Auto_MPPT_Flag == false)
{
Console.WriteLine("Auto MPPT");
Auto_MPPT_Flag = true;
}
else if (Array[x].Contains("Motor Volt") && Motor_Volt_Flag == false)
{
temp_Array = Array[x].Split('=');
if (temp_Array.Length == 2)
{
Console.WriteLine("Motor Volt =" + temp_Array[1]);
Motor_Volt_Flag = true;
}
}
else if (Array[x].Contains("Motor Amps") && Motor_Amps_Flag == false)
{
temp_Array = Array[x].Split('=');
if (temp_Array.Length == 2)
{
Console.WriteLine("Motor Amps =" + temp_Array[1]);
Motor_Amps_Flag = true;
}
}
else if (Array[x].Contains("Max Motor") && Max_Motor_Flag == false)
{
temp_Array = Array[x].Split('=');
if (temp_Array.Length == 2)
{
Console.WriteLine("Max Motor =" + temp_Array[1]);
Max_Motor_Flag = true;
}
}
else if (Array[x].Contains("Motor RPM") && Motor_RPM_Flag == false)
{
temp_Array = Array[x].Split('=');
if (temp_Array.Length == 2)
{
Console.WriteLine("Motor RPM =" + temp_Array[1]);
Motor_RPM_Flag = true;
}
}
else if (Array[x].Contains("Sn") && SN_Flag == false)
{
temp_Array = Array[x].Split('n');
if (temp_Array.Length == 2)
{
Console.WriteLine("Sn" + temp_Array[1]);
SN_Flag = true;
}
}
else if (Array[x].Contains("SMC") && SMC_Type_Flag == false)
{
temp_Array = Array[x].Split('M');
if (temp_Array.Length == 2)
{
Console.WriteLine("SM" + temp_Array[1]);
SMC_Type_Flag = true;
}
}
else if (Array[x].Contains("Array MPP") && Auto_MPPV_Flag == false)
{
Console.WriteLine("Auto MPPT");
Auto_MPPV_Flag = true;
}
}
Thread.Sleep(6000);
} //end try
catch (Exception e)
{
Console.WriteLine(e.Message);
} //end catch
i++;
if (i == 12)
{
Thread.Sleep(num2);
Array_Volt_Flag = false;
Array_Amps_Flag = false;
Auto_MPPT_Flag = false;
Motor_Volt_Flag = false;
Motor_Amps_Flag = false;
Max_Motor_Flag = false;
Motor_RPM_Flag = false;
Auto_MPPV_Flag = false;
SN_Flag = false;
SMC_Type_Flag = false;
i = 0;
}
}//end while
}
}
}