|
-
February 28th, 2009, 05:37 PM
#1
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.
-
March 2nd, 2009, 01:04 PM
#2
Re: Serial Port Bluetooth Communication with a windows form
-
March 2nd, 2009, 01:23 PM
#3
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
-
March 3rd, 2009, 04:17 AM
#4
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)
}
-
March 3rd, 2009, 02:19 PM
#5
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
-
March 4th, 2009, 01:16 PM
#6
Re: Serial Port Bluetooth Communication with a windows form
-
March 18th, 2009, 07:07 AM
#7
Re: Serial Port Bluetooth Communication with a windows form
Can you please post your part of code (concept) here?
-
March 31st, 2009, 10:51 AM
#8
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();
}
-
April 2nd, 2009, 02:26 AM
#9
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|