private byte[][] ReceiveData(uint requested_channel) { byte[][] packs = new byte[2][]; uint[,] addresses = AFE_ChannelAddresses(requested_channel); //first 'address' is the fpga register address for readout of the histogram, second is the fpga number, third is the channel number on that fpga System.Net.Sockets.Socket febSocket = febClient.TNETSocket_prop; for (int packNum = 0; packNum < 2; packNum++) { int lret, old_available = 0; do { febSocket.Send(PP.GetBytes("rdbr " + addresses[packNum, 0].ToString("X") + " 400\r\n")); //This is a request command for the FEB to perform 1024 reads at the specified address, and send that data in binary System.Threading.Thread.Sleep(100); //Pause for a moment to allow the data to show up in the send buffer while (febSocket.Available > old_available) //Wait until the FEB has all the data to send { old_available = febSocket.Available; System.Threading.Thread.Sleep(50); } packs[packNum] = new byte[febSocket.Available]; lret = febSocket.Receive(packs[packNum], packs[packNum].Length, System.Net.Sockets.SocketFlags.None); System.Console.WriteLine("Read: {0} / {1} bytes", lret, old_available); } while (lret != old_available || old_available < 2048); //2048 is the minimum byte length for an incoming histogram; each one is actually >=2049 due to the '>' sent by the board } return(packs); }
public void SendStr(string t) { if (_ClientOpen) { try { if (TNETSocket.Available > 0) { byte[] buf = new byte[TNETSocket.Available]; TNETSocket.Receive(buf); } // ? why does this not work? SW.WriteLine(t); //byte[] b = PP.GetBytes(t + Convert.ToChar((byte)0x0d)); byte[] b = PP.GetBytes(t + "\r\n"); IAsyncResult sendResult = TNETSocket.BeginSend(b, 0, b.Length, SocketFlags.None, null, null); sendResult.AsyncWaitHandle.WaitOne(); //TNETSocket.Send(b); //Thread.Sleep(20); } catch (System.IO.IOException) { _ClientOpen = false; } } }
//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); } }
//The optional writeType allows for "dwr" to be used for voltage registers to apply calibrations. public static void WriteReg(UInt32 v, ref Mu2e_Register reg, ref TcpClient myClient, string writeType = "wr") { ushort addr = (ushort)(reg.addr + (reg.fpga_index * reg.fpga_offset_mult)); ushort upper_addr = (ushort)(reg.upper_addr + (reg.fpga_index * reg.fpga_offset_mult)); ushort lower_addr = (ushort)(reg.lower_addr + (reg.fpga_index * reg.fpga_offset_mult)); if (myClient.Connected) { NetworkStream TNETStream = myClient.GetStream(); //StreamWriter SW = new StreamWriter(TNETStream); //StreamReader SR = new StreamReader(TNETStream); if (reg.width <= 16) { if (v >= Math.Pow(2, 16)) { Exception e = new Exception("write val greater than possible"); throw e; } string sv = Convert.ToString(v, 16); string lout = writeType + " " + Convert.ToString(addr, 16) + " " + sv + "\r\n"; byte[] buf = PP.GetBytes(lout); TNETStream.Write(buf, 0, buf.Length); System.Threading.Thread.Sleep(5); } else if (reg.width > 16) { //if (v >= Math.Pow(2, 16)) !stupid! what if we want to write a zero? { uint vu = v % (uint)(Math.Pow(2, 16)); string sv = Convert.ToString(vu, 16); string lout = writeType + " " + Convert.ToString(upper_addr, 16) + " " + sv + "\r\n"; byte[] buf = PP.GetBytes(lout); TNETStream.Write(buf, 0, buf.Length); } //else { string sv = "0"; string lout = writeType + " " + Convert.ToString(upper_addr, 16) + " " + sv + "\r\n"; byte[] buf = PP.GetBytes(lout); TNETStream.Write(buf, 0, buf.Length); } System.Threading.Thread.Sleep(5); { uint vl = v & (uint)(0xffff); string sv = Convert.ToString(vl, 16); string lout = writeType + " " + Convert.ToString(lower_addr, 16) + " " + sv + "\r\n"; byte[] buf = PP.GetBytes(lout); TNETStream.Write(buf, 0, buf.Length); } } reg.val = v; } else { Exception e = new Exception("can't scan without a connected FEB client"); throw (e); } }
public void SendStr(string t) { if (_ClientOpen) { if (TNETSocket.Available > 0) { Thread.Sleep(1); byte[] buf = new byte[TNETSocket.Available]; TNETSocket.Receive(buf); } // ? why does this not work? SW.WriteLine(t); //byte[] b = PP.GetBytes(t + Convert.ToChar((byte)0x0d)); byte[] b = PP.GetBytes(t + "\r"); TNETSocket.Send(b); } }
private byte[][] ReceiveData(uint requested_channel) { byte[][] packs = new byte[read_addrs.Length][]; //uint[,] addresses = AFE_ChannelAddresses(requested_channel); //first 'address' is the fpga register address for readout of the histogram, second is the fpga number, third is the channel number on that fpga System.Net.Sockets.Socket febSocket = febClient.TNETSocket_prop; for (int packNum = 0; packNum < read_addrs.Length; packNum++) { int lret, old_available = 0; do { old_available = 0; if (febSocket.Available > 0) { febSocket.Receive(new byte[febSocket.Available], febSocket.Available, System.Net.Sockets.SocketFlags.None); //Receive any junk it was previously trying to send } ResetReadAddresses(); //Set the read-pointer addresses back to 0 //400 to 800 for new "Big histogramming" febSocket.Send(PP.GetBytes("rdbr " + read_addrs[packNum].ToString("X") + " 800\r\n")); //This is a request command for the FEB to perform 2048 (1024) reads at the specified address, and send that data in binary System.Threading.Thread.Sleep(10); //Pause for a moment to allow the data to show up in the send buffer while (febSocket.Available > old_available) //Wait until the FEB has all the data to send { old_available = febSocket.Available; System.Threading.Thread.Sleep(10); } packs[packNum] = new byte[febSocket.Available]; lret = febSocket.Available; IAsyncResult rResult = febSocket.BeginReceive(packs[packNum], 0, packs[packNum].Length, System.Net.Sockets.SocketFlags.None, null, null); rResult.AsyncWaitHandle.WaitOne(1000); if (!rResult.IsCompleted) { Console.WriteLine("Reached timeout when reading histogram"); } System.Console.WriteLine("Expected {0} bytes for hitogram", lret); //lret = febSocket.Receive(packs[packNum], packs[packNum].Length, System.Net.Sockets.SocketFlags.None); //System.Console.WriteLine("Read: {0} / {1} bytes", lret, old_available); } while (lret != old_available || old_available != 4 * num_bins); //The 4 is because 32-bit bins -> 4 bytes per bin } return(packs); }
//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 ReadReg(ref Mu2e_Register reg, ref TcpClient myClient) { ushort addr = (ushort)(reg.addr + (reg.fpga_index * reg.fpga_offset_mult)); ushort upper_addr = (ushort)(reg.upper_addr + (reg.fpga_index * reg.fpga_offset_mult)); ushort lower_addr = (ushort)(reg.lower_addr + (reg.fpga_index * reg.fpga_offset_mult)); try { if (myClient.Connected) { NetworkStream TNETStream = myClient.GetStream(); //StreamWriter SW = new StreamWriter(TNETStream); //StreamReader SR = new StreamReader(TNETStream); if (reg.width <= 16) { string lin = "rd " + Convert.ToString(addr, 16) + "\r\n"; byte[] buf = PP.GetBytes(lin); TNETStream.Write(buf, 0, buf.Length); System.Threading.Thread.Sleep(5); if (myClient.Available > 0) { byte[] rec_buf = new byte[myClient.Available]; Thread.Sleep(1); int ret_len = TNETStream.Read(rec_buf, 0, rec_buf.Length); reg.prev_val = reg.val; UInt32 t; double dv; BufVal(rec_buf, out t, out dv); reg.val = t; reg.dv = dv; } else { } } else if (reg.width > 16) { string lin = "rd " + Convert.ToString(upper_addr, 16) + "\r\n"; byte[] buf = PP.GetBytes(lin); TNETStream.Write(buf, 0, buf.Length); System.Threading.Thread.Sleep(10); UInt32[] tv = new UInt32[2]; if (myClient.Available > 0) { byte[] rec_buf = new byte[myClient.Available]; Thread.Sleep(1); int ret_len = TNETStream.Read(rec_buf, 0, rec_buf.Length); reg.prev_val = reg.val; UInt32 t; double dv; BufVal(rec_buf, out t, out dv); tv[0] = t; } lin = "rd " + Convert.ToString(lower_addr, 16) + "\r\n"; buf = PP.GetBytes(lin); TNETStream.Write(buf, 0, buf.Length); System.Threading.Thread.Sleep(10); if (myClient.Available > 0) { byte[] rec_buf = new byte[myClient.Available]; Thread.Sleep(1); int ret_len = TNETStream.Read(rec_buf, 0, rec_buf.Length); reg.prev_val = reg.val; UInt32 t; double dv; BufVal(rec_buf, out t, out dv); tv[1] = t; } reg.val = (tv[0] * 256 * 256) + tv[1]; } } else { } } catch { } }
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; }
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; }