2 Replies - 868 Views - Last Post: 29 November 2010 - 04:06 PM Rate Topic: -----

#1 reynal74  Icon User is offline

  • New D.I.C Head

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

Reading data from a board and writing it to a file

Posted 29 November 2010 - 11:27 AM

Almighty forum please help me because I'm going crazy with this :censored: problem. I'm reading data from a board, I know that the code receives all the data because I echo it in the console. However, when I check the file that is supposed to receive the data written to, I notice that the file is not complete. In other words, I do not receive the complete data. Can someone help me find out where is the problem and how to solve it?

        //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;

                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


Thank you,.

This post has been edited by n8wxs: 29 November 2010 - 11:46 AM
Reason for edit:: Please use the CODE TAGS when posting code


Is This A Good Question/Topic? 0
  • +

Replies To: Reading data from a board and writing it to a file

#2 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5675
  • View blog
  • Posts: 12,192
  • Joined: 02-June 10

Re: Reading data from a board and writing it to a file

Posted 29 November 2010 - 02:03 PM

What kind of board is it? 2x4 or 4x8 sheet?
Sorry, couldn't resist.

We have a tutorial on DIC for serial port communication:
Serial port communication

string ArduinoLogName = @"" +
I can see no reason to add this to the start of your file name.
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: Reading data from a board and writing it to a file

Posted 29 November 2010 - 04:06 PM

Its an Arduino board. For some reason I like that board hehe
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1