Click to See Complete Forum and Search --> : Serial Port Bluetooth Communication with a windows form


Chris.Coder
February 28th, 2009, 04:37 PM
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:


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();


}

Chris.Coder
March 2nd, 2009, 12:04 PM
Anyone have any idea?

jasonli
March 2nd, 2009, 12:23 PM
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.

ricky_cpp
March 3rd, 2009, 03:17 AM
Hi,

I will suggsest 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)
}

Chris.Coder
March 3rd, 2009, 01:19 PM
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

Chris.Coder
March 4th, 2009, 12:16 PM
Any Ideas?

ricky_cpp
March 18th, 2009, 07:07 AM
Can you please post your part of code (concept) here?

Chris.Coder
March 31st, 2009, 10:51 AM
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

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();
}

ricky_cpp
April 2nd, 2009, 02:26 AM
Hi Chris,

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