2 Replies - 2638 Views - Last Post: 07 December 2010 - 05:45 AM Rate Topic: -----

#1 reynal74  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 30-October 10

Breaking port connection between C# & Arduino

Posted 06 December 2010 - 10:50 PM

Hello everyone, I have been struggling trying to get this peace of code to work. I' writing a application that communicates with Arduino requesting it a certain amount of readings. I have no problems getting the reading from the board (The first time). However, when I attempt to get reading the second time the application frezzes and I don't get anything. If I hit the Arduino reset button (hitting the button just resets the Arduino and causes the serial port connection to close), but in the C# code " serialPort1.Close(); " is suppose to close the port. Can some one help me out here? I know that the problem is in my C# application. here is the code I'm using to write the data and close the port. I will really appreciate your help.

     

.
.
.

                string[] DataArray = new string[NumberOfReadings * 2];//Creating array to hold all the data received
                for (int count = 0; count <= NumberOfReadings; count++)
                {
                    try
                    {
                        RxData = serialPort1.ReadLine();//reading data from the port
                        DataArray[count] = RxData;
                    }
                    catch (Exception RzData)
                    {
                        RxData = RzData.ToString();
                        DataArray[count] = RxData;
                    }
                }

                //Printing Data from DataArray into a file
                for (int count = 0; count <= NumberOfReadings; count++)
                {
                    writer.WriteLine(DataArray[count]);//writing the transmitted data
                }

                //Trashing data from array
                for (int count = 0; count <= NumberOfReadings; count++)
                {
                    DataArray[count] = null;
                }

                MessageBox.Show("The Transmission from Arduino has ended.");
                writer.Flush();//flush the streams
                writer.Close();//close the streams
                ArduinoLogFile.Close();//Closing the data file
            }
            else
            {
                MessageBox.Show("There is no Serial Ports open.");//message box telling user that there is not ports open
                StartArduinoButton.Enabled = false;//disabling the start arduino button
            }

            serialPort1.Close();//closing the serial port
            this.Refresh();
            this.Close();//closing the application
        }//end of StartArduinoButton



Is This A Good Question/Topic? 0
  • +

Replies To: Breaking port connection between C# & Arduino

#2 mavarazo  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 36
  • View blog
  • Posts: 181
  • Joined: 25-October 10

Re: Breaking port connection between C# & Arduino

Posted 06 December 2010 - 11:47 PM

I think the problem isn't in that part of coding you posted.
Was This Post Helpful? 0
  • +
  • -

#3 reynal74  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 30-October 10

Re: Breaking port connection between C# & Arduino

Posted 07 December 2010 - 05:45 AM

Thanks for replying,. This is the complete code, I can't find what't wrong

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.IO.Ports;

namespace CODE
{
    public partial class CODE : Form
    {

        DateTime CurrentTime = DateTime.Now; //Setting the clock to current system clock time
        string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); //path to the desktop made into strings
        string RxData;//String to hold the data from arduino serial port

        public CODE()
        {
            InitializeComponent();
        }

        //Opening the selected Port
        private void OpenPortButtom_Click(object sender, EventArgs e)
        {

            if (PortSelectBox.SelectedItem == null)//check if not port selected
            {
                MessageBox.Show("No Serial Ports was selected, Try again.");//message box telling user that there is not ports open
            }
            else
            {
                //++++++++SETTINGS FOR THE SERIAL PORT+++++++++++
                serialPort1.PortName = PortSelectBox.SelectedItem.ToString();//setting serialPort1 to a selected port by user 
                serialPort1.BaudRate = 115200;//this value most be the same as in serial.begin(####bpm) of arduino's code
                serialPort1.DataBits = 8;
                serialPort1.ReadBufferSize = 4096;  //4 * 1024bit = 4KB
                serialPort1.WriteBufferSize = 2048; //2 * 1024bit = 2KB

                if (serialPort1.IsOpen == true)
                {
                    serialPort1.Close();//closing port if previously open
                    serialPort1.Open();//opening port  
                }
                else
                {
                    serialPort1.Open();//opening port when the program starts
                }

                OpenPortButtom.Enabled = false; //disabling open port button
                ClosePortButtom.Enabled = true;//enabling the closing port button
                SettupButton.Enabled = true;//enabling the settup button
                StartArduinoButton.Enabled = true;//enabling the flush serial button
            }
        }//end of OpenPortButtom

        //Closing any open port
        private void ClosePortButtom_Click(object sender, EventArgs e)
        {
            if (serialPort1.IsOpen == true)//checking if any port is open in order to close it
            {
                serialPort1.Close(); //close serialPort1
                MessageBox.Show("Serial Port: " + serialPort1.PortName + " have been closed.");//message box telling user that the port is closed
            }
            else
            {
                MessageBox.Show("There is no Serial Ports open.");//message box telling user that there is not ports open
            }

            OpenPortButtom.Enabled = true; //enabling open port button
            ClosePortButtom.Enabled = false;//disabling the closing port button
            SettupButton.Enabled = false;//disabling the settup button
            StartArduinoButton.Enabled = false;//disabling the flush serial button
        }//end of ClosePortButtom

        //settings for the button that starts arduino serial transmission
        private void StartArduinoButton_Click(object sender, EventArgs e)
        {

            if (serialPort1.IsOpen == true)//checking if any port is open in order to close it
            {
                //creating output file
                string ArduinoLogName = desktopPath + "\\" + "ArduinoData_" + DateTime.Now.ToString("MMM-d-yyyy") + ".txt";//creates the log file
                FileStream ArduinoLogFile = new FileStream(ArduinoLogName, FileMode.Create);//creates a new file or overwrites a previous file created
                StreamWriter writer = new StreamWriter(ArduinoLogFile);//variable to write 

                serialPort1.Write("<TX>");//Sending arduino the transmitting command
                int NumberOfReadings; //variable that holds the number of readings requested

                if (SelNumberOfReading.SelectedItem == null)
                {
                    NumberOfReadings = 50;//default number of readings
                }
                else
                {
                    NumberOfReadings = int.Parse(SelNumberOfReading.SelectedItem.ToString());//converting the selected number of readings from string to ints 
                }

                string[] DataArray = new string[NumberOfReadings * 2];//Creating array to hold all the data received
                for (int count = 0; count <= NumberOfReadings; count++)
                {
                    try
                    {
                        RxData = serialPort1.ReadLine();//reading data from the port
                        DataArray[count] = RxData;
                    }
                    catch (Exception RzData)
                    {
                        RxData = RzData.ToString();
                        DataArray[count] = RxData;
                    }
                }

                //Printing Data from DataArray into a file
                for (int count = 0; count <= NumberOfReadings; count++)
                {
                    writer.WriteLine(DataArray[count]);//writing the transmitted data
                }

                //Trashing data from array
                for (int count = 0; count <= NumberOfReadings; count++)
                {
                    DataArray[count] = null;
                }

                MessageBox.Show("The Transmission from Arduino has ended.");
                writer.Flush();//flush the streams
                writer.Close();//close the streams
                ArduinoLogFile.Close();//Closing the data file
            }
            else
            {
                MessageBox.Show("There is no Serial Ports open.");//message box telling user that there is not ports open
                StartArduinoButton.Enabled = false;//disabling the start arduino button
            }

            serialPort1.Close();//closing the serial port
            this.Refresh();
            this.Close();//closing the application
        }//end of StartArduinoButton


        //FIRST METHOD OF WRITING INTO THE FILE
        /*
        //settings for the button that starts arduino serial transmission
        private void StartArduinoButton_Click(object sender, EventArgs e)
        {
            if (serialPort1.IsOpen == true)//checking if any port is open in order to close it
            {
                //creating output file
                string ArduinoLogName = desktopPath + "\\" + "ArduinoData_" + DateTime.Now.ToString("MMM-d-yyyy") + ".txt";//creates the log file
                FileStream ArduinoLogFile = new FileStream(ArduinoLogName, FileMode.Create);//creates a new file or overwrites a previous file created
                StreamWriter writer = new StreamWriter(ArduinoLogFile);//variable to write 

                serialPort1.Write("<TX>");//Sending arduino the transmitting command
                int NumberOfReadings; //variable that holds the number of readings requested

                if (SelNumberOfReading.SelectedItem == null)
                {
                    NumberOfReadings = 50;//default number of readings
                }
                else
                {
                    NumberOfReadings = int.Parse(SelNumberOfReading.SelectedItem.ToString());//converting the selected number of readings from string to ints 
                }

                for (int count = 0; count <= NumberOfReadings; count++)
                {
                    try
                    {
                        RxData = serialPort1.ReadLine();//reading data from the port
                        writer.WriteLine(RxData);//writing the transmitted data
                        Console.WriteLine(RxData);
                    }
                    catch (Exception RzData)
                    {
                        RxData = RzData.ToString();
                        writer.WriteLine(RxData);//writing the transmitted data
                        Console.WriteLine(RxData);
                    }
                }
              
                MessageBox.Show("The Transmission from Arduino has ended.");
                writer.Flush();//flush the streams
                writer.Close();//close the streams
                ArduinoLogFile.Close();//Closing the data file
            }
            else
            {
                MessageBox.Show("There is no Serial Ports open.");//message box telling user that there is not ports open
                StartArduinoButton.Enabled = false;//disabling the start arduino button
            }

            serialPort1.Close();//closing the serial port
            this.Refresh();
            this.Close();//closing the application
        }//end of StartArduinoButton
        */

        //settings for button that tells arduino how many readings to take
        private void SettupButton_Click(object sender, EventArgs e)
        {
            if (serialPort1.IsOpen == true)//checking if any port is open in order to close it
            {
                if (SelNumberOfReading.SelectedItem == null)
                {
                    MessageBox.Show("No number of reading was selected, using default (50 readings).");
                    serialPort1.Write("<N50>");//Sending the readings selected [default (50 readings)]
                }
                else
                {
                    serialPort1.Write("<N" + SelNumberOfReading.SelectedItem.ToString() + ">");//Sending arduino the readings selected, format <N#####>
                    MessageBox.Show("Arduino has been set to " + SelNumberOfReading.SelectedItem.ToString() + " number of readings.");//message box telling user the selected number of readings
                }

                OpenPortButtom.Enabled = false; //disabling open port button
                ClosePortButtom.Enabled = true;//enabling the closing port button
            }
            else
            {
                MessageBox.Show("There is no Serial Ports open.");//message box telling user that there is not ports open
                SettupButton.Enabled = false;//disabling the settup button
            }
        }//end of SettupButton

        private void PortSelectBox_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void CODE_Load(object sender, EventArgs e)
        {

        }

        private void SelNumberOfReading_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

    }//end of partial class for CODE
}//end of namespace CODE




Was This Post Helpful? 0
  • +
  • -

Page 1 of 1