/// <summary> /// Edits a line /// </summary> /// <param name="Server">Reference to the telnet server</param> /// <param name="Tempfile">The location of the temp file</param> /// <param name="LineNo">Line number to edit</param> private static void EditLine(TelnetServer Server, string Tempfile, int LineNo) { List(Server, Tempfile, LineNo, LineNo); Server.Print(Tools.ZeroFill(LineNo, 8, ' ') + ":*", true); string NewLine = Server.Input(); if (NewLine == "\x03") { return; } Edlin.UpdateLine(Tempfile, LineNo, NewLine); }
/// <summary> /// Actually starts the demo /// </summary> /// <param name="Server">Reference to the Telnet Server</param> public static void Start(TelnetServer Server) { // Configures the onboard button. If this fails, it's probably already in use by another app. InputPort Button = null; try { Button = new InputPort(ONBOARD_SW1, false, Port.ResistorMode.Disabled); } catch (Exception e) { Server.Color(TelnetServer.Colors.LightRed); Server.Print("Exception " + e.Message + " given. Were the onboard Button already configured?"); Server.Color(TelnetServer.Colors.White); return; } // Configures the onboard LED. If this fails, it's probably already in use by another app. OutputPort Led = null; try { Led = new OutputPort(ONBOARD_LED, false); } catch (Exception e) { Button.Dispose(); // The button is already defined Server.Color(TelnetServer.Colors.LightRed); Server.Print("Exception " + e.Message + " given. Were the onboard LED already configured?"); Server.Color(TelnetServer.Colors.White); return; } // Disables echoing of keypresses Server.EchoEnabled = false; // Clears the screen Server.ClearScreen(); // Draws a Netduino Plus in ANSI/ASCII art Server.Print("\xda\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xbf"); Server.Print("\xb3 \xfe\xfe\xfe\xfe\xfe\xfe\xfe\xfe \xfe\xfe\xfe\xfe\xfe\xfe\xfe\xfe\xb3"); Server.Print("\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb \xdc \xb3"); Server.Print("\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb NETDUINO \xb3"); Server.Print("\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb PLUS \xb3"); Server.Print("\xb3 ..\xb3"); Server.Print("\xdb\xdb \xdb\xdb ::\xb3"); Server.Print("\xb3 \xdb\xdb\xdb\xdb\xdb\xdb \xb3"); Server.Print("\xdb\xdb\xdb\xdb\xdb \xdb\xdb\xdb\xdb\xdb\xdb \xb3"); Server.Print("\xdb\xdb\xdb\xdb\xdb \xdb\xdb\xdb\xdb\xdb\xdb \xfe\xfe\xfe\xfe\xfe\xfe \xfe\xfe\xfe\xfe\xfe\xfe\xb3"); Server.Print("\xc0\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xdb\xdb\xdb\xdb\xdb\xdb\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xd9"); Server.Print("", true); Server.Print("Push the Netduino Plus onboard button, or press a key to close this app"); // We only update the screen if the LastState is different from the current state bool LastState = false; while (Server.IsConnected && Server.Input(1, false) == "") { // We need to update if (Button.Read() == LastState) { // Lets record the last state LastState = !Button.Read(); Led.Write(LastState); // Draws the button if (LastState) { Server.Color(TelnetServer.Colors.HighIntensityWhite); } else { Server.Color(TelnetServer.Colors.Gray); } Server.Locate(7, 26, true); Server.Print("\xdb\xdb", true, true); // Draws the LED if (LastState) { Server.Color(TelnetServer.Colors.LightBlue); } else { Server.Color(TelnetServer.Colors.Gray); } Server.Locate(3, 29, true); Server.Print("\xdc", true); // Brings back the cursor to the last line Server.Locate(14, 1); } } // Releases the pins Button.Dispose(); Led.Dispose(); // Enables echo again and clears the screen if (Server.IsConnected) { Server.EchoEnabled = true; Server.ClearScreen(); } }
/// <summary> /// Starts editting a file /// </summary> /// <param name="Server">Reference to the telnet server</param> /// <param name="Filename">File to edit</param> private static void EditFile(TelnetServer Server, string Filename) { if (!File.Exists(Filename)) { throw new Exception("File does not exist: " + Filename); } // Makes a working file string Tempfile = ReplaceExt(Filename, ".$$$"); string Bakfile = ReplaceExt(Filename, ".bak"); File.Copy(Filename, Tempfile, true); // Detecting line endings FileStream Stream = File.OpenRead(Tempfile); int Lines = 0; int Byte = 0; for (long Pos = 0; Pos < Stream.Length; ++Pos) { Byte = Stream.ReadByte(); if (Byte == 10) { ++Lines; } } Stream.Close(); // Appairently the file doesn't end with a line ending if (Byte != 10) { ++Lines; } // Okay, we know the amount of lines Server.Print(Lines.ToString() + " lines. Type ? for help."); // Lets start the main loop while (true) { // Shows a prompt and asks for a command Server.Print("*", true); string[] Arguments = SplitCommandline(Server.Input()); if (Arguments.Length == 0) { continue; } switch (Arguments[Arguments.Length - 1].Substring(0, 1).ToUpper()) { case "Q": // Quits, without saving File.Delete(Tempfile); return; case "E": // Quits, with saving if (File.Exists(Bakfile)) { File.Delete(Bakfile); } File.Move(Filename, Bakfile); File.Move(Tempfile, Filename); return; case "L": // Lists some lines if (Arguments.Length > 2) { List(Server, Tempfile, int.Parse(Arguments[0]), int.Parse(Arguments[1])); } else if (Arguments.Length > 1) { List(Server, Tempfile, int.Parse(Arguments[0]), Lines); } else { List(Server, Tempfile, 1, Lines); } break; case "?": // Shows help EdlinHelp(Server); break; default: // Is this a line number, or unknown entry? int LineNo = -1; try { LineNo = int.Parse(Arguments[0]); } catch { } if (LineNo == -1) { Server.Print("Entry error"); } else if (LineNo > Lines) { Server.Print("Line out of range (1 to " + Lines.ToString() + ")"); } else { EditLine(Server, Tempfile, LineNo); } break; } } }
/// <summary> /// Starts the serial client /// </summary> /// <param name="Server">Reference to the telnet server</param> /// <param name="Com">COM-port</param> /// <param name="Baud">Baudrate</param> /// <param name="SParity">Parity</param> /// <param name="Databits">Databits</param> /// <param name="Stopbits">Stopbits</param> private static void Start(TelnetServer Server, string Com, string Baud = "9600", string SParity = "NONE", string Databits = "8", string Stopbits = "1") { // Parses parity Parity PParity; switch (SParity.ToUpper()) { case "EVEN": PParity = Parity.Even; break; case "MARK": PParity = Parity.Mark; break; case "NONE": PParity = Parity.None; break; case "ODD": PParity = Parity.Odd; break; case "SPACE": PParity = Parity.Space; break; default: throw new ArgumentException("Parity " + SParity + " unknown. Known values are EVEN, MARK, NONE, ODD, SPACE"); } // Parses Stopbits StopBits Stop; switch (Stopbits.ToUpper()) { case "0": Stop = StopBits.None; break; case "1": Stop = StopBits.One; break; case "1.5": Stop = StopBits.OnePointFive; break; case "2": Stop = StopBits.Two; break; default: throw new ArgumentException("Stopbits " + Stopbits + " unknown. Known values are 0, 1, 1.5, 2"); } // Configures the serial port SerialPort Port = new SerialPort( portName: Com.ToUpper(), baudRate: int.Parse(Baud), parity: PParity, dataBits: int.Parse(Databits), stopBits: Stop ); Server.Print("Connecting to " + Com + "..."); Port.DiscardInBuffer(); Port.Open(); Server.Print("Connected. Press Ctrl+C to close the connection"); Server.EchoEnabled = false; while (true) { byte[] Data = Tools.Chars2Bytes(Server.Input(1, false).ToCharArray()); if (Data.Length > 0) { if (Data[0] == 3) { break; // Ctrl+C } Port.Write(Data, 0, Data.Length); } if (Port.BytesToRead > 0) { byte[] Buffer = new byte[Port.BytesToRead]; Port.Read(Buffer, 0, Buffer.Length); Server.Print(new string(Tools.Bytes2Chars(Buffer)), true); } } Server.EchoEnabled = true; Port.Close(); Server.Print("", false, true); Server.Print("Connection closed"); }