I am reading from hardware device continuosly, the problem is the device stop sending data, i think is problem of my datareceive event but i am not sure of
that.
serialPort.PortName = porta;
serialPort.BaudRate = 115200;
serialPort.DataBits = 8;
serialPort.ReadBufferSize = 409600;
serialPort.Open();
CrearTramaConect();
serialPort.Write(TramaConect, 0, TramaConect.Length);
void serialPort_DataReceived(object s, SerialDataReceivedEventArgs e)
{
while (serialPort.BytesToRead >= 14)
{
for (int i = 0; i < 14; i++)
{
data[i] = (byte)serialPort.ReadByte();
}
if (data[13] == (byte)'F')
{
data.ToList().ForEach(b => recievedData.Enqueue(b));
processData();
LineReceived(this, new LineReceivedEventArgs(packet));
}else{
while ((charend != (byte)'F') && serialPort.BytesToRead != 0))
charend = (byte)serialPort.ReadByte();
}
}
}
public event LineReceivedEventHandler LineReceived;
sp1.LineReceived += new LineReceivedEventHandler(sp1_LineReceived);
void sp1_LineReceived(object sender, LineReceivedEventArgs Args)
{
this.Invoke((MethodInvoker)(()=>cargarDatosEnSerie(sender,Args)));
}
private void cargarDatosEnSerie(object sender, LineReceivedEventArgs Args)
{
this.Invoke((MethodInvoker)(() => addPoints(new Dato(Args.packet, false, false, 1, false, false,j))) );
dataGridView1.Update();
label1.Text = "numero de puntos: " + j;
this.Refresh();
}
byte[] packet;
private byte[] processData()
{
if(recievedData.Count>=14)
packet = Enumerable.Range(0, 14).Select(i => recievedData.Dequeue()).ToArray();
return packet;
}
[EDIT] From comment
That is the data (frame) i receive (always 14 bytes):
68 -->'D'
65 -->'A'
84 -->'T'
0
127 Xh
254 Xl
0
0
115 Yh
51 Yl
5
0
196
70 -->'F'
i do the job with this, (127254) is my X value and 11551 my Y value, i use these to plot it in a chart,my hardware now is sending frames continuosly
x = 1 y = 0,001 'D' 'A' 'T'.............'F'
x = 2 y = 0,002 'D' 'A' 'T'.............'F'
x = 3 y = 0,003 'D' 'A' 'T'.............'F'
All data with this 'D' 'A' 'T'.............'F' frame format is interesting for me, and continuosly read.
<pre lang="C#">thanks George, i have a problem with that
<pre><pre lang="c#">
if (serialPort.BytesToRead > 13) // For example
{
string data = serialPort.ReadLine();
if (data.ToCharArray().Count() == 13)
{
dataArray = Encoding.ASCII.GetBytes(data);
dataArray.ToList().ForEach(b =&gt; recievedData.Enqueue(b));
processData();
LineReceived(this, new LineReceivedEventArgs(dataArray));
}
}
with this line i think
dataArray = Encoding.ASCII.GetBytes(data);
dato: 68---65---84---1---3---63---1---1---1---122---1---1---63---
dato: 68---65---84---1---3---63---1---1---1---123---1---1---63---
dato: 68---65---84---1---3---63---1---1---1---124---1---1---63---
dato: 68---65---84---1---3---63---1---1---1---125---1---1---63---
dato: 68---65---84---1---3---63---1---1---1---126---1---1---63---
dato: 68---65---84---1---3---63---1---1---1---127---1---1---63---
dato: 68---65---84---1---3---63---1---1---1---63---1---1---107--- this will be 128
dato: 68---65---84---1---3---63---1---1---1---63---1---1---106---
dato: 68---65---84---1---3---63---1---1---1---63---1---1---105---
dato: 68---65---84---1---3---63---1---1---1---63---1---1---104---
dato: 68---65---84---1---3---63---1---1---1---63---1---1---111---
dato: 68---65---84---1---3---63---1---1---1---63---1---1---110---
dato: 68---65---84---1---3---63---1---1---1---63---1---1---109---