private void SerialPort_MessageReceived(object sender, SerialPortLib.MessageReceivedEventArgs args) { busyReceiving = true; ParseSerialData(args.Data); busyReceiving = false; }
public Display (SerialPortLib sp) { comm = sp; Clear(); }
private void SerialPort_MessageReceived(object sender, SerialPortLib.MessageReceivedEventArgs args) { ParseSerialData(args.Data); }
protected void DeterminePortConnections () { /* * There can be up to 3 serial devices connected: * Spectrometer board * TEC board * Display * * This code attempts to determine which device is * on which port (/dev/ttyUSBn where n = 0, 1 or 2 * * The Spectrometer board defaults to 921600 baud, but * we want to run it at 115200 baud. * To change the baud rate to 115200, we use the following * Open the port at 921600 baud * Send *PARA:BAUD 115\r * Send *PARA:SAVE\r * Send *RST\r * open up the port at 115200 baud * * The TEC board runs at 115200 baud * The display board runs at 19200 baud * * To test a serial port for a device, we can use the following algorithm: * Open the port at 115200 baud * Send *para:tectemp?\r * Read the response * If there is a response, it can be one of the following: * 1) a NACK which indicates the board is the Spectrometer board * 2) the TEC temperature which indicates the board is the TEC board * If there is no response, this indicates one of the following scenarios: * 1) The device is the display, which will interpret the command as text to display and not * return anything, plus it runs at 19200 baud * 2) The device is the Spectrometer board, running at 921600 baud * * The C# code is very flaky at 921600 baud, so we have to use C++ or Java code to * test and reprogram the Spectrometer board for 115200 baud */ foreach (SerialPortInfo spi in dictSerialPorts.Values) { spi.portName = ""; } string[] portNames = SerialPort.GetPortNames (); Dictionary<string, bool> dictPorts = new Dictionary<string, bool> (portNames.Length); foreach (string name in portNames) { SerialPortLib sc = new SerialPortLib (name, 115200); if (sc.Open ()) { dictPorts [name] = false; //System.Threading.Thread.Sleep (100); for (int i = 0; (i < 5) && (dictPorts[name] == false); i++) { sc.Write ("*para:tectemp?\r"); byte[] resp; if (sc.Read (out resp, 60, 300)) { if (resp [0] == NACK) { dictSerialPorts[SPEC].portName = name; specPort = name; dictPorts [name] = true; Console.WriteLine ("Found spec board on port " + name); } else { try { string text = System.Text.ASCIIEncoding.ASCII.GetString (resp); Console.WriteLine ("Received " + text); string[] words = text.Split (new char[]{' '}); for (int wordIndex = 0; wordIndex < words.Length; wordIndex++) { string wordUC = words[wordIndex].ToUpper(); if (wordUC.Contains("TEC")) { if (wordIndex + 1 < words.Length) { wordUC = words[wordIndex + 1].ToUpper(); if (wordUC.Contains("TEMPERATURE")) { dictSerialPorts[TEC].portName = name; tecPort = name; dictPorts [name] = true; Console.WriteLine ("Found tec board on port " + name); break; } } } } } catch (System.FormatException) { } } } else { Console.WriteLine ("No response"); // No response. Could be a spectrometer board running at 921600 baud // or the display, which wouldn't return anything } } sc.Close(); if (dictPorts [name] == false) { // The port wasn't assigned to a Spectrometer or TEC board in the above code // This means the port could be attached to a pectrometer board running at 921600 baud // or the display, which wouldn't return anything // See if it is the spectrometer board running at 921600 SerialPortLib port = new SerialPortLib (name, 921600); if (port.Open ()) { port.Write ("*IDN?\r"); string buf; if (port.Read (out buf, 50, 200)) { Console.WriteLine ("SerialPortLib Read returned " + buf); if (buf.Contains ("JETI_PIC_VERSA")) { Console.WriteLine ("Found spec board on port " + name + " running at 921600 baud"); dictSerialPorts[SPEC].portName = name; specPort = name; dictPorts [name] = true; // We now want to reprogram the spec board to run at 115200 baud port.Write ("*PARA:BAUD 115\r"); port.Read (out buf, 50, 200); port.Write ("*PARA:SAVE\r"); port.Read (out buf, 50, 500); port.Write ("*RST\r"); } } port.Close (); } } /* string response; if (sc.ReadPort(out response, 40, 200)) { if (response.Contains("JETI_PIC_VERSA")) { Console.WriteLine("Found spec board on port " + name); specPort = name; } } */ } } foreach (string name in dictPorts.Keys) { if (dictPorts [name] == false) { dictSerialPorts[DISPLAY].portName = name; displayPort = name; Console.WriteLine ("Assigning " + name + " to the display"); } } System.Text.StringBuilder sb = new System.Text.StringBuilder (); foreach (string key in dictSerialPorts.Keys) { //Console.WriteLine("Next key in dictSerialPorts is " + key); SerialPortInfo spi = dictSerialPorts[key]; if (spi.portName != "") { sb.AppendFormat ("{0} found on {1}\r", key, spi.portName); } } /* if (specPort != null) { sb.AppendFormat ("Spec. Board found on {0}\r", specPort); } if (tecPort != null) { sb.AppendFormat ("TEC Board found on {0}\r", tecPort); } if (displayPort != null) { sb.AppendFormat ("Display found on {0}\r", displayPort); } */ if (sb.Length > 0) { AddOutputText(sb.ToString()); //textviewOutput.Buffer.Text += sb.ToString(); } }
protected void InitDisplay () { if (dictSerialPorts [DISPLAY].portName != "") { displayComm = new SerialPortLib (dictSerialPorts [DISPLAY].portName, dictSerialPorts [DISPLAY].baudRate); if (!displayComm.Open ()) { MessageBox.Show ("Failed to open Display port " + dictSerialPorts [DISPLAY].portName); return; } } if (displayComm != null && displayComm.IsOpen ()) { display = new MPFQA.Display(displayComm); display.Add(" RTA M-PFQA", true); display.Add("Initializing", true); /* // Clear the display displayComm.Write (new byte[]{0xFE, 0x58}); displayComm.Write ("RTA M-PFQA\nReady"); */ displayComm.GetReadHandler += new SerialPortLib.ReadHandler (DisplayKeypadHandler); } }
protected void InitTEC () { if (dictSerialPorts [TEC].portName != "") { tecComm = new SerialPortLib (dictSerialPorts [TEC].portName, dictSerialPorts [TEC].baudRate); if (!tecComm.Open ()) { MessageBox.Show ("Failed to open TEC board port " + dictSerialPorts [TEC].portName); return; } } if (tecComm != null && tecComm.IsOpen ()) { SetTecTemp(); } }
protected void InitSpectrometer () { if (dictSerialPorts [SPEC].portName != "") { specComm = new SerialPortLib (dictSerialPorts [SPEC].portName, dictSerialPorts [SPEC].baudRate); if (!specComm.Open ()) { MessageBox.Show ("Failed to open Spec Board port " + dictSerialPorts [SPEC].portName); return; } } if (specComm != null && specComm.IsOpen ()) { string response; System.Text.StringBuilder sb = new System.Text.StringBuilder(); Console.WriteLine ("Reset device"); specComm.Write ("*RST\r"); System.Threading.Thread.Sleep (2000); if (specComm.Read (out response, 100, 1000)) { // returns something like Softwarereset PIC24H !!PIC_Versa256 VERSION 2.610 26.06.12 Console.WriteLine (response); } specComm.Clear (); System.Threading.Thread.Sleep (500); Console.WriteLine ("Get device ID"); specComm.Write ("*IDN?\r"); if (specComm.Read (out response, 100, 200)) { // returns something like JETI_PIC_VERSA Console.WriteLine ("Read " + response); } specComm.Clear (); // Get the number of pixels (Y data points) the board returns specComm.Write ("*PARA:PIX?\r"); if (specComm.Read (out response, 50, 200)) { string[] words = response.Split (new char[]{':'}); if (words.Length == 2) { pixelCount = Convert.ToInt32 (words [1]); sb.AppendFormat("Pixel Count = {0}\r", pixelCount); //textviewOutput.Buffer.Text += "Pixel Count = " + pixelCount.ToString () + "\r"; } } // Read the calibration coefficients for (int i = 0; i < 5; i++) { specComm.Write ("*para:fit" + i.ToString () + "?\r"); if (specComm.Read (out response, 60, 200)) { string[] words = response.Split (new char[]{':'}); if (words.Length == 2) { calibCoeffs [i] = Convert.ToDouble (words [1]); sb.AppendFormat("Calibration Coeff {0} = {1}\r", i, calibCoeffs[i]); //textviewOutput.Buffer.Text += "Calibration Coeff " + i.ToString () + " = " + calibCoeffs [i].ToString () + "\r"; } } } // Calculate the X Axis using the calibration coefficients just read CalculateXAxis (); if (xAxis.Length > 0) { sb.AppendFormat("X Axis starts with {0} and ends with {1}\r", xAxis[0], xAxis[xAxis.Length - 1]); //textviewOutput.Buffer.Text += String.Format("X Axis starts with {0} and ends with {1}\r", xAxis[0], xAxis[xAxis.Length - 1]); } /* System.Text.StringBuilder sb = new System.Text.StringBuilder("X Axis\r"); for (int i = 0; i < pixelCount; i++) { sb.AppendFormat("X[{0}] = {1}\r", i, xAxis[i]); //textviewOutput.Buffer.Text += "X[" + i.ToString () + "] = " + xAxis [i].ToString () + "\r"; } textviewOutput.Buffer.Text += sb.ToString(); */ // Set integration time sb.AppendFormat("Setting integration time to {0}\r", integrationTime); //textviewOutput.Buffer.Text += "Setting integration time to " + integrationTime.ToString() + "\r"; specComm.Write ("*PARA:TINT " + integrationTime.ToString () + "\r"); specComm.Read (out response, 20, 200); // Set the scan delay to 0 sb.Append("Setting scan delay to 0\r"); //textviewOutput.Buffer.Text += "Setting scan delay to 0\r"; specComm.Write ("*PARA:SDELAY 0\r"); specComm.Read (out response, 20, 200); if (sb.Length > 0) { AddOutputText(sb.ToString()); } /* // TESTING ONLY!!! { SplineInterpolateFile("/home/pi/Dev/RTA/TestSpectra/PQ-0111.txt"); } */ } }
private void SerialPort_ConnectionStatusChanged(object sender, SerialPortLib.ConnectionStatusChangedEventArgs args) { logger.Debug("Serial Port Connected = {0}", args.Connected); if (args.Connected) { serialPort.SendMessage(ackRequest); } else { logger.Debug("W800Rf32 is offline"); OnConnectionStatusChanged(new ConnectionStatusChangedEventArgs(false)); } }