Go Down

Topic: Serial communication using C# (Read 1 time) previous topic - next topic

Arduino1234567

How do I print my voltage readings I'm getting from Arduino to the console window in C#?

septillion

#1
Jun 12, 2017, 08:15 pm Last Edit: Jun 13, 2017, 09:59 am by septillion
You're on the wrong forum mate ;) That's a C# question. If you send the values to the PC via Serial you need to open the Serial port in your C# application.
Use fricking code tags!!!!
I want x => I would like x, I need help => I would like help, Need fast => Go and pay someone to do the job...

NEW Library to make fading leds a piece of cake
https://github.com/septillion-git/FadeLed

Arduino1234567


ron_sutherland

#3
Jun 13, 2017, 02:18 am Last Edit: Jun 13, 2017, 02:19 am by ron_sutherland
I guess that is one way to do an Anonymous search.

https://code.msdn.microsoft.com/windowsdesktop/SerialPort-brief-Example-ac0d5004

Also YaCy looks interesting if Google has gotten irritating enough.

http://yacy.net/en/index.html
my projects: https://github.com/epccs

horace

#4
Jun 13, 2017, 08:55 am Last Edit: Jun 13, 2017, 09:01 am by horace
example of a simple C# terminal using a Form
Code: [Select]
using System;
using System.Windows.Forms;
using System.IO.Ports;

namespace CsharpTerminal
{
    public partial class Form1 : Form
    {
        // delegate to transfer received data to TextBox
        public delegate void AddDataDelegate(String myString);
        public AddDataDelegate myDelegate;
       
        public Form1()
        {
            InitializeComponent();
            this.myDelegate = new AddDataDelegate(AddDataMethod);
            serialPort1.Open();
        }

        // key on textbox pressed, read key and transmit down serial line
        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {       
        if( serialPort1.IsOpen)
            serialPort1.Write(e.KeyChar.ToString());
        else
            textBox1.AppendText("No COM port open" + Environment.NewLine);
        }

     // data received from serial port - display on textbox
     private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            //textBox1.AppendText(serialPort1.ReadExisting());  // not thread safe
            string s = serialPort1.ReadExisting();
            textBox1.Invoke(this.myDelegate, new Object[] { s });
        }

     // display seral data on textbox
     public void AddDataMethod(String myString)
     {
         textBox1.AppendText(myString);
     }


     private void Form1_FormClosed(object sender, FormClosedEventArgs e)
     {
         serialPort1.Close();
     }

     }
}

it can be made more sophisticated by adding menus to select COM port, Baud rate, etc
Screen dump of Form attached - communicating with an Arduino Nano


Go Up