我正在讓一個C#程序像一臺可憐的示波器一樣工作。我有一個發送到串行(Serial.write(analogRead(A0)))的Arduino,然後C#有一個線程,在主線程刷新圖表時讀取每個ms樣本。我的疑問是,我應該使用Serial.write還是Serial.print?在C#中使用線程實時顯示arduino analogRead()
是否有可能獲得2kS/s?我使用115200的波特率,這裏是代碼。
namespace TEST
{
public partial class Form1 : Form
{
static int buffer_size = 1024;
public static string comboBoxText;
public static int[] buffer = new int[buffer_size];
IEnumerable<int> yData;
static int[] range = Enumerable.Range(0, buffer_size).ToArray();
IEnumerable<int> xData = range;
public static bool flag = true;
public Form1()
{
Random rand = new Random();
InitializeComponent();
for (int c = 0; c<buffer_size;c++) {
buffer[c] = 0;
}
Thread thread1 = new Thread(fillBuffer);
thread1.Start();
comboBox1.Items.Add("Select");
foreach (string s in SerialPort.GetPortNames())
{
comboBox1.Items.Add(s);
}
}
static public void fillBuffer()
{
Thread.Sleep(1000);
SerialPort serialPort1 = new SerialPort();
serialPort1.PortName = "COM5";
serialPort1.BaudRate = 115200;
serialPort1.Open();
while (true)
{
}
}
private void timer1_Tick(object sender, EventArgs e)
{
yData = buffer;
chart1.Series[0].Points.DataBindY(yData);
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
try {
comboBoxText = comboBox1.Text;
}
catch
{
MessageBox.Show("Porta Inválida");
return;
}
comboBox1.Enabled = false;
}
}
有沒有什麼我可以做的樣品每0.5ms,然後顯示樣本作爲點的集合?我沒有得到好的結果。如果有人能幫忙,謝謝!
你試過串口事件嗎? –