CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Feb 2009
    Location
    .NET3.5/Visual Studio 2008
    Posts
    5

    Serial Port Bluetooth Communication with a windows form

    Heya,

    I'm having problems trying to connect to a serial port to receive data and store it in a text file. The funny thing is it works perfectly when I step through it using breakpoints. I think I need to implement some sort of delay but thats only my best guess.

    Thanks so much!

    Chris

    Here is the code I currently have to setup the serial port:

    Code:
    private void FETT_START_RECEIVING_Click(object sender, EventArgs e) 
            {       
                SERIALPORT_1.PortName = "COM17"; 
                SERIALPORT_1.Open(); 
                SERIALPORT_1.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(DataRecievedOnPort); 
     
               
            } 
             
            private void DataRecievedOnPort(object sender, SerialDataReceivedEventArgs e) 
            { 
                 
               SerialPort Temp = sender as SerialPort; 
               string text = Temp.ReadExisting(); 
               string path = FETT_RECEIVING_FILEBROWSING_DIALOG.SelectedPath + "\\" + "test.txt"; 
               StreamWriter tw = new StreamWriter(path); 
               tw.Write(text); 
               tw.Close(); 
               
     
            }
    Last edited by Chris.Coder; March 2nd, 2009 at 01:03 PM.

  2. #2
    Join Date
    Feb 2009
    Location
    .NET3.5/Visual Studio 2008
    Posts
    5

    Re: Serial Port Bluetooth Communication with a windows form

    Anyone have any idea?

  3. #3
    Join Date
    Dec 2005
    Location
    Waterloo ON
    Posts
    545

    Re: Serial Port Bluetooth Communication with a windows form

    Try to use another function, like Read().

    Serial port is receiving data continously, but in your event handler text file is open and close every time, which cause delay. Maybe lots of received data are lost because of that.

    You can put a textbox control in windows form to get all of received data to see if all data are received.

    Just suggestion.
    The difficulty is that you have no idea how difficult it is.

    .Net 3.5/VS 2008

  4. #4
    Join Date
    Aug 2008
    Posts
    78

    Re: Serial Port Bluetooth Communication with a windows form

    Hi,

    I will s****est to use BeginRead and EndRead methods which support asynchronous operations. May be at beginning of project you will be able to avoid some delays but you can not avoid them forever especially as the project grows.

    - declare an asyStream to your serial port
    - ..BeginRead(, , , , OnDataReceive)
    -.OnDataReceive(..)
    {
    RecieveBytes;
    Start again BeginRead(, , , , OnDataReceive)
    }

  5. #5
    Join Date
    Feb 2009
    Location
    .NET3.5/Visual Studio 2008
    Posts
    5

    Cool Re: Serial Port Bluetooth Communication with a windows form

    Still having the same problem when using .READ()

    I have a device that is sending 192 characters each time it sends through bluetooth. It is quite irritating that It all works when I step through the program.

    All I want to do is receive this text and write it to a text file. Is there a better way of doing this? I have read about people using Sockets but can't find any good examples.


    Thanks,

    Chris

  6. #6
    Join Date
    Feb 2009
    Location
    .NET3.5/Visual Studio 2008
    Posts
    5

    Re: Serial Port Bluetooth Communication with a windows form

    Any Ideas?

  7. #7
    Join Date
    Aug 2008
    Posts
    78

    Re: Serial Port Bluetooth Communication with a windows form

    Can you please post your part of code (concept) here?

  8. #8
    Join Date
    Feb 2009
    Location
    .NET3.5/Visual Studio 2008
    Posts
    5

    Re: Serial Port Bluetooth Communication with a windows form

    Sorry for the delay in reply. All I am trying to do is open the serial port and receive text from our device, and write it to a file. Right now I have "cheated" in a way by placing the invoke the way I did. It is only calling the datareceived event once and then it won't fire again. I have tried many examples I have found online but I have yet to find the correct placement of the invoke/delegate to handle the serial port thread.

    Here is the current code I have. I know that the invoke is done incorrectly.

    Thanks for any help
    Code:
     private void FETT_START_RECEIVING_Click(object sender, EventArgs e)
            {
                FETT_RECEIVEMODE_SERIALPORT_1.PortName = FETT_DEVICE_SELECT_1.SelectedItem.ToString();
                FETT_RECEIVEMODE_SERIALPORT_1.DtrEnable = true;
    
                FETT_RECEIVEMODE_SERIALPORT_1.Open();
    
                FETT_RECEIVEMODE_SERIALPORT_1.DataReceived += new SerialDataReceivedEventHandler(DataReceivedOnPort);
    
    
                this.BeginInvoke(new EventHandler(DataReceivedOnPort));
    
            }
    
           
    
    
    
    
    
            private void DataReceivedOnPort(object sender, EventArgs e)
            {
                Thread.Sleep(500);
                byte[] data = new byte[200];
                data[0] = 0;
    
                
                        for (int i = 0; i < 197; i++)
                        {
                            data[i] = (byte)FETT_RECEIVEMODE_SERIALPORT_1.ReadByte();
                            //bw.Write(data[i]);
                        }
    
    
    
    
                      Write_To_File(data);
                           
    
            }
            private void Write_To_File(byte[] data)
            {
                string CRN = Get_CRN(data);
                string path = Fett_Naming_Convention(CRN, FETT_RECEIVEMODE_PATH_LABEL_1.Text);
                FileStream fs = File.Create(path);
                BinaryWriter bw = new BinaryWriter(fs);
    
     
    
                bw.Write(data);
    
                bw.Close();
                fs.Close();
            }

  9. #9
    Join Date
    Aug 2008
    Posts
    78

    Re: Serial Port Bluetooth Communication with a windows form

    Hi Chris,

    Sorry for replying so late.
    Can you please tell, What exactly is the problem?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured