Пример #1
0
	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();
		}
	}