1 Replies - 22334 Views - Last Post: 16 August 2011 - 08:09 AM Rate Topic: -----

#1 lildawg   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 16-August 11

Sending files through serial port

Posted 16 August 2011 - 06:14 AM

Hi, I'm trying to create a chat service via serial port, which can also send text files.

I've gotten the basic part done, right up to the sending file part, but I'm having trouble with the receiving file part. I am not able to reconstruct the text file at the receiver part. please advice. attached below is my code. thanks.

private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            // send messages          
            if (sendfiles == 0)
            {
                RxString = serialPort1.ReadExisting();
                this.Invoke(new EventHandler(DisplayText));
            }
            // send text files
            else if (sendfiles == 1) 
            {
                RxString = serialPort1.ReadExisting();
                System.Threading.Thread.Sleep(500);
                this.Invoke(new EventHandler(CreateNewFile));
                MessageBox.Show(sendfiles.ToString());
            }
            else
            {
                RxString = serialPort1.ReadExisting();
                this.Invoke(new EventHandler(DisplayText));
            }

        }

        private void DisplayText(object sender, EventArgs e)
        {
            textBox2.AppendText(RxString);
        }

        private void CreateNewFile(object sender, EventArgs e)
        {
            # region Create New File

            string filename2 = string.Format("rcdfile{0}.txt", rcdfilesvalue);
            string filename1 = "C:\\Users\\TxJx\\Desktop\\New Folder\\";
            string filename = String.Concat(filename1, filename2);

            #endregion

            #region Write To File

            using (StreamWriter sw = new StreamWriter(filename))
            {
                sw.Write(RxString);

            }

            #endregion
        }

 #region Send Text Files

        private void sendFile_Click(object sender, EventArgs e)
        {

            if (serialPort1.IsOpen)
            {
                #region Open File
                OpenFileDialog OFDialog = new OpenFileDialog();
                OFDialog.Title = "Open File";
                OFDialog.Filter = "Text Files (*.txt)" + "|*.txt|All files (*.*)|*.*";
                OFDialog.InitialDirectory = @"C:\";

                #endregion

                #region Prepare to Read File

                if (OFDialog.ShowDialog() == DialogResult.OK)
                {
                    serialPort1.DiscardInBuffer();
                    sendfiles = 1;
                    rcdfilesvalue = rcdfilesvalue + 1;
                    filepath = OFDialog.FileName;
                    ReadFile(filepath);
                }

                #endregion

            }
            else
            { MessageBox.Show("COM Port is Not Connected!", "Error"); }
        }

        private void ReadFile(string filepath)
        {

            textBox1.AppendText(" \r\n Sending file... \r\n");

            using (FileStream readfile1 = File.OpenRead(filepath))
                serialPort1.Write(new BinaryReader(readfile1).ReadBytes((int)readfile1.Length), 0, (int)readfile1.Length);

            fileinput = serialPort1.ReadExisting();
            textBox2.AppendText(" \r\n File received... \r\n");
            textBox1.AppendText("File Sent! \r\n");
        }



Is This A Good Question/Topic? 0
  • +

Replies To: Sending files through serial port

#2 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Sending files through serial port

Posted 16 August 2011 - 08:09 AM

We have a wonderful tutorial for this:

http://www.dreaminco...cation-in-c%23/
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1