Look up SignalIR on CodePlex. It's designed for real-time communications
Printable View
Look up SignalIR on CodePlex. It's designed for real-time communications
DataMiser, the sensor receiver is connected to my pc via rs232. So once I run my program I am listening to COM port 4 which is where the receiver is configured to send. I did not convert the program to a windows form app but my program is working now. I set the receive threshold to a value >0 and I coded the serial parameters myself instead of asking for input. Now my program runs a loop that listens for incoming traffic and receives it. Now I cant get the formatting right. The receiver is sending two lines of data and I am receiving chunks of it and my handler is being fired about 8 times per reading. I know this is common when working with serial ports, but it reacts the same even when I try to convert the bytes to string. Check out my handler method:
//retrieve number of bytes in the buffer
int bytes = _serialPort.BytesToRead;
//create a byte array to hold the awaiting data
byte[] dataBuffer = new byte[bytes];
//Read the data and store it
_serialPort.Read(dataBuffer, 0, bytes);
//convert to string.
string s = System.Text.ASCIIEncoding.ASCII.GetString(dataBuffer);
//display the data
Console.WriteLine("Data" + s + " was received at " + DateTime.Now);
Well if I remember correctly isn't there a readline() method? If so you could use this to read a line at a time. Or you could use another var that will que up the incoming data until it contains one or more CR characters then parse it as needed and write to disk.
What exactly is causing you a problem?
Yes, I used ReadLine() but that didnt work for me. I used:
string data = _serialPort.ReadLine();
Console.WriteLine(data);
I don't know if there is more to it than that but when I used that my program would only tell me that I had received data, not show the actual data. I guess whats causing my problem is my
string s = System.Text.ASCIIEncoding.ASCII.GetString(dataBuffer); line.
The data that is being sent to me has the following format:
ID:34242 State:NY
Zip:12345 StreetType:Ave
when it is received by my program it displays it like this:
ID:34242
State:N
Y Zip:12
345 Street
Type:Ave
So not only does some data get pushed to the next line, but it doesn't get formatted into one string.
So you need to que up the data and print it when you have enough. Create a variable with a larger scope so that it holds it's value. For example if you create it at the top of the class.
Rather than displaying s append s to this que var and then check to see what this var holds and display when it has the data you want.
Or if you simply want to update a file as I thought you were trying to do you just append whatever you receive to the file either once several bytes have been received or anytime something is received. Just be sure not to add a CRFL to the end of the data where it is not needed.
Yes, I do want to send the data to a file. I am simply displaying it on the console app so that I can see the string as its coming to through the port and monitoring the rest of the readings. I've tried what you have suggested. It has improved the formatting to a certain degree but the spacing is off and im still getting several iterations of the same reading. Ive attached my code so you can take a look at the modified DataReceive Event Handler.
so this is what the actual data from the sensors look like in its raw form:
6001:0 Temp=66
S -158 N -125 C 89
This will help you see why I made the decision about the regex statement. This is the the form I want it in: 6001:0 Temp=66 S -158 N -125 C 89. My reason for having the string in this form is to than split the string and send each field to a certain column in a db table.
Why would you create a new event handler inside this loop?
You are also writing the data you received more than once, could be why you are seeing the same data? Or perhaps the device is actually sending it more than once.Code:while (_serialPort.IsOpen)
{
Thread.Sleep(1000);
_serialPort.DataReceived += new SerialDataReceivedEventHandler(Port_DataReceived);
}
I am not familar with regex so I can not make heads or tails from what you are doing there.Code:Console.WriteLine(spL.Read(dataBuffer, 0, bufSize));
string s = System.Text.ASCIIEncoding.ASCII.GetString(dataBuffer);
receiveBuffer.Append(s);
var regex = new Regex(@"(\d{4} Temp=\d{2} S\d{3} N\d{3} C\d{2} )");
Match match;
do
{
match = regex.Match(receiveBuffer.ToString());
if (match.Success)
{
//"Process" the significant chunk of data
Console.WriteLine(match.Captures[0].Value);
I would just look for the newline characters and replace the odd ones with a space.
I finally found a solution to my problem. The reason I had put my Event Handler in the loop was because I was under the impression that this handler had to be fired as long as the program was listening to the port (I was thinking inside the loop). I put the line above the loop and it is now showing one reading as a reading comes in so that is working perfectly. I also changed my Handler method a bit. I first used the _serialPort.Read() function to actually tell me how many bytes the whole packet contained. Once I knew that, I set the data buffer to that number. The new handler looks like this:
private static void Port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
int i = 0;
string dtString="";
Byte [] dataBuffer = new Byte[37];
Thread.Sleep(500); // wait data ready
while (_serialPort.BytesToRead > 0) // if data ready in buffer
{
dataBuffer[i] = Convert.ToByte(_serialPort.ReadByte()); // read data serial & saving to array byte
i++; // counter buffer
}
dtString = ASCIIEncoding.ASCII.GetString(dataBuffer); // convert array byte to string
Console.Write("Data" + dtString + " " + "was received at:" + " " + DateTime.Now);
}
So what I did differently here is set the buffer to the bytes I wanted to receive. Create a while loop with a counter that counted up to my buffer's indicated byte number , read those bytes, translate to string and than display them. Thank you very much for all your help DataMiser, it was very useful and appreciated.
You're welcome :)
Glad you got it working.
Don't forget to mark your thread resolved. Use the thread tools at the top of the page to do so.