2

I'm making a C# program to work like a poor oscilloscope. I have an Arduino which sends to serial (Serial.write(analogRead(A0)) ) and then the C# has a thread which reads each ms a sample while the main thread refreshes the Chart. My doubt is, should I use Serial.write or Serial.print ?

Is it possible to get 2kS/s ? I'm using the baud rate of 115200 and here is the code.

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;

        }

    }

Is there anything I can do to sample each 0.5ms and then display the sample as a collection of points ? I'm not getting good results. If anyone can help, thank you!

1
  • had you tried the event of serial ports? Commented Jun 13, 2016 at 0:50

1 Answer 1

2

At a Baudrate of 115200 and a good processor speed, your algorithm seem fast enough. But one of the things that can slow down the speed is the interval of timer1. It should be set to the lowest possible. Also for the difference between Serial.Write and Serial.Print check out this forum . Also using the .net inbuilt serial port event handler would save you a lot of stress and is also a faster and more efficient solution. You might want to check it out here

Sign up to request clarification or add additional context in comments.

6 Comments

I did it, but i don't think it's working well... The Event handler can not handle 2k samples per second. Should I use a Buffer?
@NikolasL. What is the size of the packet for 1 sample? Is ot 1024?
No, actually I'm using a pic and sending each sample one by one. I'm using a timer to control the sample and as soon as the ADC conversion finishes I send it through UART. The problem is when I'm receiving them. I'm using an hc-06 to send and I guess it sends one by one as well but the OS can not handle as many events onReceive 2.5k times/sec, so I think I should treat this event by reading the whole buffer which is 2048 bytes and then I treat. Am I right ?
First of all, its not possible that your hardware is doing some ADC processing and sending the data so fast that a computer is too slow to read it. A computer is usually faster than your hardware because your hardware uses a crystal oscillator of a couple MHz while your computer works with a processor that is of GHz frequency. So assuming your device is sending the data fast enough, then you computer should be able to read it fast enough. You said your computer and your hardware is communicating at a Baudrate of 115200.
Assuming samples are sent using the 8-N-1 format then. One sample should be sent using 10 bits and that should mean the number of samples that can be sent in 1 sec is 115200/10 which is 11520 samples in one second. Thesame goes for the recieving end. So if you are using events, you should not have any problem with this.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.