//#define ISU //#define SBE //#define ELMO //#define FLBB //#define OPT #define SED //#define PTH using System; using System.IO.Ports; using System.Text; using System.Threading; using Microsoft.SPOT; using Microsoft.SPOT.IO; using Microsoft.SPOT.Hardware; using GHI.Pins; namespace NXP752 { public class Program { private static SerialPort consolePort = new SerialPort("COM2", 115200, Parity.None, 8, StopBits.One); private static OutputPort openCommand = new OutputPort(G400.PD5, true); //valve "OpenCommandRtn" public static I2CDevice g400I2C = new I2CDevice(null); protected static StringBuilder sbTemp = new StringBuilder(512); private static byte[] byteTemp = new byte[1024]; #if ISU public static SerialPort sensorPort = new SerialPort("COM3", 19200, Parity.None, 8, StopBits.One); public static OutputPort PWR = new OutputPort(G400.PC26, false); #endif #if SED public static SerialPort sensorPort = new SerialPort("COM3", 19200, Parity.None, 8, StopBits.One); public static OutputPort SedTrapPwr1 = new OutputPort(G400.PB10, false); public static OutputPort SedTrapPwr2 = new OutputPort(G400.PB5, false); #endif #if FLBB public static SerialPort sensorPort = new SerialPort("COM5", 19200, Parity.None, 8, StopBits.One); public static OutputPort PWR = new OutputPort(G400.PC13, false); #endif #if OPT public static SerialPort sensorPort = new SerialPort("COM6", 9600, Parity.None, 8, StopBits.One); public static OutputPort PWR = new OutputPort(G400.PC3, false); #endif #if ELMO public static SerialPort sensorPort = new SerialPort("COM4", 19200, Parity.None, 8, StopBits.One); private static OutputPort PWR = new OutputPort(G400.PC19, false); private static Timer ADCTimer = null; #endif #if SBE private static OutputPort DR = new OutputPort(G400.PA28, true); private static OutputPort Mode = new OutputPort(G400.PC29, false); private static SerialPort sensorPort = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One); public static OutputPort PWR = new OutputPort(G400.PC27, false); #endif #if PTH public static SerialPort sensorPort = new SerialPort("COM5", 19200, Parity.None, 8, StopBits.One); public static OutputPort PWR = new OutputPort(G400.PC13, false); private static PTH.PTHData pthData; #endif private static byte[] consolePortReadBytes = new byte[64]; private static int consolePortBytesToRead = 0; private static int consolePortInBytesIndex = 0; private static byte[] sensorPortReadBytes = new byte[64]; private static int sensorPortBytesToRead = 0; private static int sensorPortInBytesIndex = 0; public static void Main() { InterruptPort ISU_FLT = new InterruptPort(G400.PC31, false, Port.ResistorMode.Disabled, Port.InterruptMode.InterruptEdgeBoth); ISU_FLT.OnInterrupt += new NativeEventHandler(ISU_FLT_OnInterrupt); OpenConsolePort(); init(); OutputPort I2C_SLT1; I2C_SLT1 = new OutputPort(G400.PB11, false); OutputPort I2C_SLT2; I2C_SLT2 = new OutputPort(G400.PB12, false); OutputPort I2C_SLT3; I2C_SLT3 = new OutputPort(G400.PB13, true); OutputPort I2C_SLT4; I2C_SLT4 = new OutputPort(G400.PB14, false); OutputPort I2C_SLT5; I2C_SLT5 = new OutputPort(G400.PB15, true); OutputPort I2C_SLT6; I2C_SLT6 = new OutputPort(G400.PB16, true); #if ELMO openValve(); ADCTimer = new Timer(new TimerCallback(ADCTimerCallback), null, 0, 10000); #endif #if SED SedTrap.init(); SedTrap.SedTrapSelect = 2; SedTrap.pwrOn(); //SedTrap.SedTrapSelect = 2; //SedTrap.pwrOn(); //SedTrap.SedTrapSelect = 2; /* while (true) { consolePortReadBytes[0] = 104; consolePortReadBytes[1] = 105; I2CUartBridge.i2cprint(Program.g400I2C, consolePortReadBytes, consolePortReadBytes.Length); Thread.Sleep(500); }*/ talktoSEDTRAP(); #endif #if PTH PTH.init(); while(true) { pthData=PTH.getPTH(); sbTemp.Clear(); sbTemp.Append("P="); sbTemp.Append(pthData.LPS331Pressure.ToString()); sbTemp.Append(" T="); sbTemp.Append(pthData.LPS331Temperature.ToString()); sbTemp.Append(" RH="); sbTemp.Append(pthData.SHT21Humidity.ToString()); Debug.Print(sbTemp.ToString()); SendLine(sbTemp); Thread.Sleep(1000); } #endif while (true) { //display menu //SendLine(sbTestConsoleMenu); //consolePort.Flush(); while ((consolePortBytesToRead = consolePort.BytesToRead) == 0) { sensorPortBytesToRead = sensorPort.BytesToRead; if (sensorPortBytesToRead > 0) { sensorPort.Read(sensorPortReadBytes, 0, sensorPortBytesToRead); consolePort.Write(sensorPortReadBytes, 0, sensorPortBytesToRead); //consolePort.Flush(); Array.Clear(sensorPortReadBytes, 0, sensorPortBytesToRead); } Thread.Sleep(50); } try { consolePort.Read(consolePortReadBytes, 0, consolePortBytesToRead); sensorPort.Write(consolePortReadBytes, 0, consolePortBytesToRead); //iModemPort.Flush(); Debug.Print(consolePortReadBytes[0].ToString()); Array.Clear(consolePortReadBytes, 0, consolePortBytesToRead); } catch { Debug.Print("Console Read Exception"); //TODO do something smarter here } //sbTemp.Clear(); //sbTemp.Append("You typed: "); //sbTemp.Append(UTF8Encoding.UTF8.GetChars(consolePortReadBytes)); //Debug.Print(sbTemp.ToString()); }; } //end main public static void OpenConsolePort() { if (!consolePort.IsOpen) { OutputPort BT_PWR; BT_PWR = new OutputPort(G400.PC4, false); BT_PWR.Write(true); //turn on power consolePort.Open(); } consolePort.DiscardInBuffer(); consolePort.DiscardOutBuffer(); consolePortReadBytes[0] = 0; } public static void init() { ON(); sensorPort.ReadTimeout = 5000; sensorPort.Open(); sensorPort.DiscardInBuffer(); sensorPort.DiscardOutBuffer(); } public static void ON() { //PWR.Write(true); //turn on power //Thread.Sleep(2000); } static void ISU_FLT_OnInterrupt(uint port, uint state, DateTime time) { Debug.Print("ISU FLT transition"); if (state == 1) { Debug.Print("ISU Power restored"); } if (state ==0) { Debug.Print("ISU Power DISABLED!!!"); } } public static void openValve() { //EngrLogger.writeToColumns(false, "Starting valve open"); openCommand.Write(false); //valveOpen = true; } public static void SendLine(StringBuilder sbLineToSend) { sbTemp.Clear(); sbTemp.Append(sbLineToSend); sbTemp.AppendLine(); Array.Clear(byteTemp, 0, byteTemp.Length); Array.Copy(UTF8Encoding.UTF8.GetBytes(sbTemp.ToString()), byteTemp, sbTemp.Length); consolePort.Write(byteTemp, 0, byteTemp.Length); } public static void talktoSEDTRAP() { //SedTrap.pwrOn(); while (consolePortReadBytes[0] != 96) //while != '`' { //display menu //SendLine(sbTestConsoleMenu); consolePort.Flush(); Array.Clear(consolePortReadBytes, 0, consolePortReadBytes.Length); while ((consolePortBytesToRead = consolePort.BytesToRead) == 0) { Thread.Sleep(50); /* if (SedTrap.SedTrapSelect == 1) { Program.g400I2C.Config = SedTrap.configSedTrap1; } else { Program.g400I2C.Config = SedTrap.configSedTrap2; } //* */ I2CUartBridge.get_i2c_buffer(Program.g400I2C); } try { consolePort.Read(consolePortReadBytes, 0, consolePortBytesToRead); /* if (SedTrap.SedTrapSelect == 1) { Program.g400I2C.Config = SedTrap.configSedTrap1; } else { Program.g400I2C.Config = SedTrap.configSedTrap2; }//*/ I2CUartBridge.i2cprint(Program.g400I2C, consolePortReadBytes, consolePortBytesToRead); /* if (consolePortReadBytes[0] == 33) { SedTrap.SedTrapSelect = 1; } if (consolePortReadBytes[0] == 64) { SedTrap.SedTrapSelect = 2; }//*/ } catch { Debug.Print("Console Read Exception"); //TODO do something smarter here } //sbTemp.Clear(); //sbTemp.Append("You typed: "); //sbTemp.Append(UTF8Encoding.UTF8.GetChars(consolePortReadBytes)); //Debug.Print(sbTemp.ToString()); Debug.Print(consolePortReadBytes[0].ToString()); } } public static void ADCTimerCallback(object state) { double pos=-1; pos = ADC2485.readBellowsPosition(); Debug.Print("pos = " + pos.ToString()); return; } } }