我有6000到6000,000的數據,並且這些數據必須發送到串行端口。我每3-5毫秒發送一次數據到串口。在發送數據之前,我必須對它進行一些計算(例如,對某些數據採用sin和cos),並在圖表上描繪數據。每次發送數據時,我都會在圖表上描述它,然後計算下一個數據,但是這方式有點慢(有時需要比3-5毫秒更多的時間)。動態或靜態描繪數據以加速發送數據
可以通過以下方式加快我的程序嗎?它們是正確的方式嗎?
1-首先計算運動圖表完全(例如 100sin(0.0001 *指數+ 10)對於x = 0〜500萬)和將其存儲在一個陣列 然後當我將數據發送到端口,我必須在數組 中找到數據,然後描繪併發送它。
2-如上所示,但將其存儲在文件中並從文件中讀取 。
想這是我的代碼:
namespace WPF_Toolkit_SpeedTest
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public ObservableCollection<ChartItem> Items { set; get; }
public MainWindow()
{
InitializeComponent();
}
#region winmm.dll functions
int _counter = 0;
public delegate void TimerEventHandler(UInt32 id, UInt32 msg, ref UInt32 userCtx, UInt32 rsv1, UInt32 rsv2);
/// <summary>
/// A multi media timer with millisecond precision
/// </summary>
/// <param name="msDelay">One event every msDelay milliseconds</param>
/// <param name="msResolution">Timer precision indication (lower value is more precise but resource unfriendly)</param>
/// <param name="handler">delegate to start</param>
/// <param name="userCtx">callBack data </param>
/// <param name="eventType">one event or multiple events</param>
/// <remarks>Dont forget to call timeKillEvent!</remarks>
/// <returns>0 on failure or any other value as a timer id to use for timeKillEvent</returns>
[DllImport("winmm.dll", SetLastError = true, EntryPoint = "timeSetEvent")]
static extern UInt32 timeSetEvent(UInt32 msDelay, UInt32 msResolution, TimerEventHandler handler, ref UInt32 userCtx, UInt32 eventType);
/// <summary>
/// The multi media timer stop function
/// </summary>
/// <param name="uTimerID">timer id from timeSetEvent</param>
/// <remarks>This function stops the timer</remarks>
[DllImport("winmm.dll", SetLastError = true)]
static extern void timeKillEvent(UInt32 uTimerID);
TimerEventHandler tim;//TimerEventHandler tim = new TimerEventHandler(this.Link);
public void Link(UInt32 id, UInt32 msg, ref UInt32 userCtx, UInt32 rsv1, UInt32 rsv2)
{
_counter++;
if ((_counter % 5) == 0) //if ((_counter % 10) == 0)
{
Dispatcher.Invoke(new Action(()=>{
Items.Add(new ChartItem(_counter, 100*Math.Sin((.0002*_counter))));
if (_counter>1000)
{
Items.RemoveAt(0);
}
}));
}
if (_counter > 10000)
{
timeKillEvent(id);
Dispatcher.Invoke(new Action(() => { button1.Content = stp.ElapsedMilliseconds; }));
}
}
uint timerId;
#endregion
private void button1_Click(object sender, RoutedEventArgs e)
{
Items = new ObservableCollection<ChartItem>();
lineChart.ItemsSource = Items;
Items.Add(new ChartItem(0, 0));
tim = new TimerEventHandler(this.Link);
uint rsv = 0;
stp.Start();
button1.Content = stp.ElapsedMilliseconds;
timerId = timeSetEvent(1, 1, tim, ref rsv, 1);
}
System.Diagnostics.Stopwatch stp = new System.Diagnostics.Stopwatch();
}
public class ChartItem : INotifyPropertyChanged
{
public ChartItem(double t, double v)
{
time = t;
myVar = v;
}
private double time;
public double Time
{
get { return time; }
set
{
time = value;
OnPropertyChanged("Time");
}
}
private double myVar;
public double Value
{
get { return myVar; }
set
{
myVar = value;
OnPropertyChanged("Value");
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}
這個代碼僅顯示描繪的一部分,但同時我也發送數據。
如果你每次都在圖表上描繪,懷疑是造成減速的原因。也許嘗試將昂貴的操作分配到單獨的線程以保持吞吐量高。 –
你是說你每3-5ms通過一個串口移動多達600萬個數據項?即使您的數據項只有1個字節,仍然需要至少1000 Mbs的串行比特率。這是什麼樣的串口?!? – RBarryYoung
我每5毫秒發送22個字節到串行端口,每次我從圖表上的這22個字節添加一個點。我發送數據的時間是30-3000秒,所以我發送約30000/5到3000000/5數據。 – abdolahS