Arduino and Visual Studio

Hi,
I have an opencv code running on Ms Visual Studio 2013, the code is on colour detection (eg red colour).
SO once red colour is detected on a camera connected, the code will output to a txt file using the iostream library like this

ofstream myfile;
myfile.open("C:\\Users\\Student - ID\\Downloads\\SPACe Mission IIIA\\CVbook\\CV colour Communication\\CV colour Communication\\XX.txt");
myfile << "1";
myfile.close();

And from here, i will send out the number 1 to show red colour is detected.

And here, my arduino is connected to an Xbee, I wish there is something that can read the txt file and load the number to the arduino. Is it possible?

My ultimate goal is to get a digital signal from Visual Studio that can be understood by arduino and transmit out via Xbee

Thank you

The Arduino can't read a text file from your computer. Easiest way would be to let VB send out data trough the serial port to a connected Arduino at the same time as writing to the file.

septillion:
The Arduino can't read a text file from your computer. Easiest way would be to let VB send out data trough the serial port to a connected Arduino at the same time as writing to the file.

Hi, i visited this link on processing.
http://arduinobasics.blogspot.com.au/2012/05/reading-from-text-file-and-sending-to.html

If i use com3, where should i input the com3 option?

or is there other ways to go around.

thank you

You can use processing to do it. But why not from VB itself? VB can send data to a COM port as well...

septillion:
You can use processing to do it. But why not from VB itself? VB can send data to a COM port as well...

Visual Basic.. Don't know how to use. is there a tutorial or something i can refer to ?

Thank you

I meant Visual Studio 2013.

But if you can't edit that, you can use Processing. The code you linked is very usable. You only need some small changes I think.

I have an opencv code running on Ms Visual Studio 2013,

No, you don't. Visual Studio is a program for writing programs. It is not a program for running programs.

And from here, i will send out the number 1 to show red colour is detected.

Send it out how? What is the purpose of opening a file and writing just the value "1" to it? The presence of the file tells you nothing. The contents of the file tell you only that the color red was seen at some location at some time. Not where; not when.

I wish there is something that can read the txt file and load the number to the arduino.

There is. It's your application.

My ultimate goal is to get a digital signal from Visual Studio that can be understood by arduino and transmit out via Xbee

Give it up now. The likelyhood of Microsoft making Visual Studio able to open a serial port is less than that of me winning the lottery.

Applications that YOU develop using Visual Studio CAN be made to open a serial port.

septillion:
I meant Visual Studio 2013.

But if you can't edit that, you can use Processing. The code you linked is very usable. You only need some small changes I think.

Hi, I would still like to know the way to send info to the COM straight using the VC13. At least it provides me with an alternative way. Thank you

Google :slight_smile:
Or get .Value=com%20port&f[1].Type=SearchText&f[0].Value=9.0&f[0].Type=VisualStudioVersion&f[0].Text=Visual%20Studio%202008&ac=2]samples from MS VS database.

If your Arduino is connected to the PC using USB then you can read or write to the serial port and get that data in your Arduino sketch.

I wrote some C# code to communicate with the Arduino. For the test sketch, I set the colour of an RGB LED based on code from the running C# app.

I don't use VB but in C# its very basic. You don't have to use threading but I did in my example so that I can start and stop the writing.

using System.IO;
using System.IO.Ports;
using System.Threading;
...

namespace SampleNS
{
    public partial class serial_write : Window
    {
        public serial_write()
        {
            InitializeComponent();
        }

        bool is_init = false;
        bool is_continue = false;
        int baud_rate = 9600;
        string port = "";
        string msg = "";

        byte red = 128;
        byte green = 128;
        byte blue = 128;

        StringComparer stringComparer = StringComparer.OrdinalIgnoreCase;
        
        SolidColorBrush bshRed;
        SolidColorBrush bshGreen;
        SolidColorBrush bshBlue;
        SolidColorBrush bshColour;

        Thread writeThread;
        SerialPort sport;

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            InitSerial();
        }

        private void btnStart_Click(object sender, RoutedEventArgs e)
        {
            StartWriting();
        }

        private void btnStop_Click(object sender, RoutedEventArgs e)
        {
            StopWriting();
        }

        private void InitSerial()
        {
            try
            {
                // create serial port object
                sport = new SerialPort();

                // set baud rates
                cbBaud.Items.Add(300);
                cbBaud.Items.Add(600);
                cbBaud.Items.Add(1200);
                cbBaud.Items.Add(2400);
                cbBaud.Items.Add(9600);
                cbBaud.Items.Add(14400);
                cbBaud.Items.Add(19200);
                cbBaud.Items.Add(38400);
                cbBaud.Items.Add(57600);
                cbBaud.Items.Add(115200);
                cbBaud.SelectedIndex = 4;

                // get ports
                string[] ports = SerialPort.GetPortNames();
                if (ports != null)
                {
                    if (ports.Length > 0)
                    {
                        foreach (string p in ports)
                        {
                            cbPort.Items.Add(p);
                        }
                    }
                }
                cbPort.SelectedIndex = -1;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            finally
            {
                is_init = true;
            }
        }

        private void StartWriting()
        {
            is_continue = true;
            btnStart.IsEnabled = false;
            btnStop.IsEnabled = true;
            sport.Open();
            writeThread = new Thread(WritePort);
            writeThread.Start();
        }

        private void StopWriting()
        {
            is_continue = false;
            btnStart.IsEnabled = true;
            btnStop.IsEnabled = false;
        }

        private void WritePort()
        {
            try
            {
                while (is_continue)
                {
                    sport.WriteLine("Blah");
                }
            }
            catch (TimeoutException tex)
            {
                MessageBox.Show(tex.ToString());
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            finally
            {
                if (sport.IsOpen)
                    sport.Close();
            }
        }

    }
}