//To just read trace data from the FEB and work only in memory, returns true if able to parse, false if not public static bool ReadFeb(ref Mu2e_FEB_client feb, ref SpillData spill, out long lret) { source_name = feb.name; if (feb.TNETSocket.Available > 0) { byte[] junk = new byte[feb.TNETSocket.Available]; feb.stream.Read(junk, 0, feb.TNETSocket.Available); } lret = 0; byte[] mem_buff; byte[] b = PP.GetBytes("RDB 1\r\n"); //get the spill header (and 1 word) & reset read pointers byte[] hdr_buf = new byte[18]; long spillwrdcnt = 0; //spill word count will be the total spill count for the FEB (which means BOTH FPGAs) feb.TNETSocket.Send(b); Thread.Sleep(10); if (feb.TNETSocket.Available > 0) //if we can snag the wordcount from the spill header, then we know how much we should be reading { feb.TNETSocket.Receive(hdr_buf); spillwrdcnt = (hdr_buf[0] * 256 * 256 * 256 + //spillwrdcnt is how many words there are for ALL fpgas in a given spill hdr_buf[1] * 256 * 256 + hdr_buf[2] * 256 + hdr_buf[3]); mem_buff = new byte[spillwrdcnt * 2]; //each word = 2 bytes, we are going to allocate a buffer in memory to read the FEB data into that is the correct size. int bytesread = 0; int bytesleft = (int)spillwrdcnt * 2; try { b = PP.GetBytes("RDB\r\n"); feb.TNETSocket.Send(b); Thread.Sleep(10); do { int bytes_now = feb.stream.Read(mem_buff, bytesread, feb.TNETSocket.Available); bytesread += bytes_now; bytesleft -= bytes_now; Thread.Sleep(100); } while (feb.stream.DataAvailable); lret = bytesread; } catch (System.IO.IOException) { feb.Close(); //IO exception thrown if socket was forcibly closed by FEB, so lets tell the C# FEB client it is closed } //something went wrong skip spill spill = new SpillData(); bool parse_ok = spill.ParseInput(mem_buff); return(parse_ok); } else { return(false); } }
public static void ReadFeb(string BoardName, Socket s, out long l) //out List<byte> buf, { List <byte> buf; source_name = BoardName; PP.myMain.timer1.Enabled = false; byte[] b = new byte[5]; b = PP.GetBytes("rdb\r\n"); time_start = DateTime.Now; l = 0; s.Send(b); buf = new List <byte>(); Thread.Sleep(100); //wait to get the first packet while (s.Available == 0) { Thread.Sleep(5); } Thread.Sleep(50); int old_available = 0; while (old_available < s.Available) { Thread.Sleep(20); PP.myRun.UpdateStatus("Started recieving " + old_available + " bytes"); old_available = s.Available; } byte[] rec_buf = new byte[s.Available]; int ret_len = s.Receive(rec_buf); for (int i = 0; i < ret_len; i++) { buf.Add(rec_buf[i]); l++; } PP.myRun.UpdateStatus("Ended recieving " + buf.LongCount() + " bytes"); Thread.Sleep(5); //spill word count is the first 4 bytes int ind = 0; Int64 t = 0; t = (Int64)(buf[0] * 256 * 256 * 256 + buf[1] * 256 * 256 + buf[2] * 256 + buf[3]); long SpillWordCount = t * 2; long RecWordCount = buf.LongCount <byte>(); DateTime start_rec = DateTime.Now; TimeSpan MaxTimeSpan = TimeSpan.FromSeconds(2); while (RecWordCount < SpillWordCount) { Thread.Sleep(2); if (DateTime.Now > start_rec.Add(MaxTimeSpan)) { //timeout SpillWordCount = RecWordCount; } if (s.Available > 0) { byte[] rec_buf2 = new byte[s.Available]; int ret_len2 = s.Receive(rec_buf); for (int i = 0; i < ret_len; i++) { buf.Add(rec_buf[i]); l++; } RecWordCount = buf.LongCount <byte>(); DateTime so_far_time = DateTime.Now; TimeSpan elapsed = so_far_time.Subtract(start_rec); Console.WriteLine("read... " + buf.LongCount <byte>().ToString() + " out of " + SpillWordCount.ToString() + " in " + elapsed.TotalMilliseconds + " ms"); } } time_read_done = DateTime.Now; SpillData new_spill = new SpillData(); new_spill.ParseInput(buf); PP.myRun.Spills.AddLast(new_spill); while (PP.myRun.Spills.Count > 3) { SpillData aSpill; aSpill = PP.myRun.Spills.First(); if (aSpill.IsDisplayed == false) { PP.myRun.Spills.Remove(aSpill); } else { return; } } if (PP.myRun != null) { if (PP.myRun.sw != null) { if (SaveEnabled) { Save(buf); } } } AllDone = true; PP.myMain.timer1.Enabled = true; }
//To save trace data (from test beam spills) to file public static void ReadFeb(ref Mu2e_FEB_client feb, /*TcpClient feb_client Socket feb_socket,*/ out long lret) //out List<byte> buf, { lret = 0; //Socket feb_socket = feb.TNETSocket; //NetworkStream feb_stream = feb.stream; source_name = feb.name; //PP.myMain.SpillTimer.Enabled = false; //PP.myRun.ACTIVE = false; //Halt the status queries to the boards (single socket atm, so we cannot get data and status concurrently) if (feb.TNETSocket.Available > 0) { byte[] junk = new byte[feb.TNETSocket.Available]; feb.stream.Read(junk, 0, feb.TNETSocket.Available); } time_start = DateTime.Now; byte[] mem_buff; byte[] b = PP.GetBytes("RDB 1\r\n"); //get the spill header (and 1 word) & reset read pointers byte[] hdr_buf = new byte[18]; long spillwrdcnt = 0; //spill word count will be the total spill count for the FEB (which means BOTH FPGAs) feb.TNETSocket.Send(b); Thread.Sleep(50); if (feb.TNETSocket.Available > 0) //if we can snag the wordcount from the spill header, then we know how much we should be reading { PP.myRun.READING = true; Console.WriteLine("+ READ"); feb.TNETSocket.Receive(hdr_buf); spillwrdcnt = (hdr_buf[0] * 256 * 256 * 256 + //spillwrdcnt is how many words there are for ALL fpgas in a given spill hdr_buf[1] * 256 * 256 + hdr_buf[2] * 256 + hdr_buf[3]); mem_buff = new byte[spillwrdcnt * 2]; //each word = 2 bytes, we are going to allocate a buffer in memory to read the FEB data into that is the correct size. int bytesread = 0; int bytesleft = (int)spillwrdcnt * 2; try { b = PP.GetBytes("RDB\r\n"); feb.TNETSocket.Send(b); Thread.Sleep(100); int readattempts = 0; do { int bytes_now = feb.stream.Read(mem_buff, bytesread, feb.TNETSocket.Available); bytesread += bytes_now; bytesleft -= bytes_now; if (bytes_now == 0) { readattempts++; } else { readattempts = 0; } Console.WriteLine(" READ " + readattempts); //Console.WriteLine(source_name + " got " + bytesread + " / " + spillwrdcnt * 2); Thread.Sleep(100); } while (feb.stream.DataAvailable || (bytesleft > 0 && readattempts < 3)); lret = bytesread; } catch (System.IO.IOException) { PP.myRun.UpdateStatus(feb.name + " took too long to respond, continuing."); } //something went wrong skip spill TimeSpan elapsed = DateTime.Now.Subtract(time_start); PP.myRun.UpdateStatus(source_name + " read: " + bytesread.ToString() + " bytes out of " + (spillwrdcnt * 2).ToString() + " bytes in " + elapsed.TotalMilliseconds + " ms"); time_read_done = DateTime.Now; PP.myRun.READING = false; Console.WriteLine("- READ"); if (PP.myRun.validateParse) { SpillData new_spill = new SpillData(); bool parse_ok = new_spill.ParseInput(mem_buff); if (parse_ok) { if (PP.myRun != null) { if (PP.myRun.SaveAscii) { Thread save = new Thread(() => Save(new_spill)); save.Start(); } else { string savename = feb.name; Thread save = new Thread(() => Save(mem_buff, savename)); //Spawn a thread that will take care of writing the data to file save.Start(); } } } else //if it fails to parse, don't bother trying to save the spill, but notify the user { System.Console.WriteLine(source_name + " failed to parse! Skipping save!"); //PP.myRun.UpdateStatus(source_name + " failed to parse! Skipping save!"); } } else { if (PP.myRun != null) { if (PP.myRun.SaveAscii) { SpillData new_spill = new SpillData(); bool parse_ok = new_spill.ParseInput(mem_buff); Thread save = new Thread(() => Save(new_spill)); save.Start(); } else { string savename = feb.name; Thread save = new Thread(() => Save(mem_buff, savename)); //Spawn a thread that will take care of writing the data to file save.Start(); } } } } ////SpillData new_spill = new SpillData(); ////bool parse_ok = new_spill.ParseInput(mem_buff/*sock_buf*/); //if (true)//(parse_ok) //{ // //PP.myRun.Spills.AddLast(new_spill); // //if (PP.myRun.Spills.Count > 2) // //{ // // if (PP.myRun.Spills.First.Value.IsDisplayed) { PP.myRun.Spills.Remove(PP.myRun.Spills.First.Next); } // // else { PP.myRun.Spills.RemoveFirst(); } // //} //} //else //{ } //PP.myMain.SpillTimer.Enabled = true; }
public static void ReadFeb(string BoardName, Socket s, out long l) //out List<byte> buf, { source_name = BoardName; PP.myMain.timer1.Enabled = false; byte[] b = new byte[5]; s.ReceiveTimeout = 500; if (s.Available > 0) { byte[] junk = new byte[s.Available]; s.Receive(junk); } b = PP.GetBytes("rdb\r\n"); time_start = DateTime.Now; l = 0; s.Send(b); Thread.Sleep(2); //wait to get the first packet while (s.Available == 0) { Thread.Sleep(2); } Thread.Sleep(10); int old_available = 0; byte[] rec_buf; byte[] mem_buf = new byte[5242879 * 8]; int mem_ind = 0; while (s.Available > old_available) { Thread.Sleep(20); old_available = s.Available; Thread.Sleep(20); } rec_buf = new byte[s.Available]; int lret = s.Receive(rec_buf, rec_buf.Length, SocketFlags.None); int tint = (int)(rec_buf[4] * 256 * 256 * 256 + rec_buf[5] * 256 * 256 + rec_buf[6] * 256 + rec_buf[7]); PP.myRun.UpdateStatus("Got " + rec_buf.Length + " bytes, enough for " + ((rec_buf.Length - 16) / 4112) + " tirg => " + (tint * 0x808 * 2 + 16).ToString() + ")"); Thread.Sleep(5); byte[] buf = new byte[lret]; rec_buf.CopyTo(buf, 0); //PP.myRun.UpdateStatus("Ended recieving " + buf.LongCount() + " bytes"); //Thread.Sleep(25); //spill word count is the first 4 bytes int ind = 0; Int64 t = 0; t = (Int64)(buf[0] * 256 * 256 * 256 + buf[1] * 256 * 256 + buf[2] * 256 + buf[3]); long SpillWordCount = t * 2; long RecWordCount = buf.LongCount <byte>(); DateTime start_rec = DateTime.Now; TimeSpan MaxTimeSpan = TimeSpan.FromSeconds(2); RecWordCount = buf.LongCount <byte>(); DateTime so_far_time = DateTime.Now; TimeSpan elapsed = so_far_time.Subtract(start_rec); Console.WriteLine("read... " + buf.LongCount <byte>().ToString() + " out of " + SpillWordCount.ToString() + " in " + elapsed.TotalMilliseconds + " ms"); time_read_done = DateTime.Now; SpillData new_spill = new SpillData(); bool parse_ok = new_spill.ParseInput(buf); if (parse_ok) { PP.myRun.Spills.AddLast(new_spill); if (PP.myRun.Spills.Count > 2) { if (PP.myRun.Spills.First.Value.IsDisplayed) { PP.myRun.Spills.Remove(PP.myRun.Spills.First.Next); } else { PP.myRun.Spills.RemoveFirst(); } } if (PP.myRun != null) { if (PP.myRun.sw != null) { if (SaveEnabled) { Save(buf); } } } } else { } AllDone = true; PP.myMain.timer1.Enabled = true; }