我写了一个c#程序,我将函数发送到pic并从中接收所需的数据。发送完最后一个函数后,它将循环并再次发送第一个函数。但是,之后收到的数据不一致。有时它会丢失数据,或者收到的数据的顺序不正确。我不确定问题是什么,因为每次我都以相同的顺序发送函数时,应该以相同的方式接收和显示数据。我已经尝试过放置Thread.Sleep函数,以防数据发送或接收速度过快但是没有那么多帮助。有什么建议???附:它通过串口连接。
我的代码:
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
}
}
}