0

I have two buttons on my window.

  1. By clicking the start button I want to open the port and see the data in the textbox and at the same time i want to save this data in Another empty text file line by line.
  2. And by clicking the stop button the program just stops saving the data but still shows the incoming data from serial port in the textbox. Can someone help? My code for start and stop button looks like:

    private void buttonStart_Click(object sender, EventArgs e)
    {
    
        serialPort1.PortName = pp.get_text();
        string Brate = pp.get_rate();
        serialPort1.BaudRate = Convert.ToInt32(Brate);
    
          serialPort1.Open();
    
        if (serialPort1.IsOpen)
        {
            buttonStart.Enabled = false;
            buttonStop.Enabled = true;
            textBox1.ReadOnly = false;
        }
    }
    
    
    private void buttonStop_Click(object sender, EventArgs e)
    {
    
    
        string Fname = pp.get_filename();
        System.IO.File.WriteAllText(Fname, this.textBox1.Text);
    
    
    }
    
2
  • 1
    ok, as it stands this isnt saving as it goes, its saving as you hit the stop button. You also dont show how textbox1 is being filled.. you just need the data arrival toggle saving to file while you do the stop...
    – BugFinder
    Commented Apr 21, 2016 at 11:36
  • Could you please give an example? I am new to c#. Thanks for the patience Commented Apr 21, 2016 at 11:54

1 Answer 1

2

1) You need to register to DataRecieved event of serial port to receive response from SerialPort instance.

sp = new SerialPort();
sp.DataReceived += sp_DataReceived;

Then, in sp_DataRecieved:

    void sp_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {
        // this the read buffer
        byte[] buff = new byte[9600];
        int readByteCount = sp.BaseStream.Read(buff, 0, sp.BytesToRead);
        // you can specify other encodings, or use default
        string response = System.Text.Encoding.UTF8.GetString(buff);

        // you need to implement AppendToFile ;)
        AppendToFile(String.Format("response :{0}",response));

        // Or, just send sp.ReadExisting();
        AppendToFile(sp.ReadExisting());
    }

2) You will receive data if there is still data in read buffer of SerialPort instance. After closing port, you need to deregister from DataReceived event.

sp -= sp_DataRecieved;

UPDATE

You can use this method to append to file

private void AppendToFile(string toAppend)
{
    string myFilePath = @"C:\Test.txt";
    File.AppendAllText(myFilePath, toAppend + Environment.NewLine);
}
5
  • private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e) { RxString = serialPort1.ReadExisting(); this.Invoke(new EventHandler(DisplayText)); } My dataReceived event code Commented Apr 21, 2016 at 12:45
  • I can read the data from serial port and can show it in a textbox. But I need to save it at the same time in Another file. above is my dataRceived event handler. Commented Apr 21, 2016 at 12:56
  • in UPDATE you have shown the filepath. I want to just pass the file name, Because I have to save an empty file at run time. Also I dont know where to write the code in UPDATE. I mean I have to write it in the start button or stop? Thanks for your patience Commented Apr 21, 2016 at 13:32
  • I think I have helped enough, you need to implement these codes yourself. Also, if you can't figure out where to put the snippets you need more practice and research. Hope this helps.
    – raidensan
    Commented Apr 21, 2016 at 14:33
  • I am glad it worked for you, please mark the answer as accepted so other users know it is a working solution.
    – raidensan
    Commented Apr 22, 2016 at 10:45

Not the answer you're looking for? Browse other questions tagged or ask your own question.