public void LoadFile(string filename) { if (!File.Exists(filename)) { filename = workDir + @"\" + filename; } if (!File.Exists(filename)) { Console.WriteLine("文件不存在: " + filename); return; } string code = File.ReadAllText(filename); uart.SendCode(port, code); }
public static void Run(Uart uart, string port) { string code = ""; while (true) { Console.Write("# "); string newLine = Console.ReadLine(); if (newLine == "<<<") { break; } code += "\n" + newLine; } Console.Write("是否运行代码(y/n): "); if (Console.ReadLine() != "n") { uart.SendCode(port, code); } }
public static void Run(Uart uart, string port) { Console.WriteLine("已进入shell模式, 输入exit可以退出shell模式."); uart.newLinePrev = "# "; while (true) { Console.Write("# "); string newLine = Console.ReadLine(); if (newLine == "exit" || newLine == "quit") { Console.WriteLine("退出shell模式."); break; } if (newLine != "") { uart.SendCode(port, newLine); } } uart.newLinePrev = "> "; }
public void Run() { watcher = new FileSystemWatcher { Path = workDir, IncludeSubdirectories = true, //全局文件监控,包括子目录 EnableRaisingEvents = true //启用文件监控 }; watcher.Created += new FileSystemEventHandler(OnCreated); watcher.Deleted += new FileSystemEventHandler(OnDeleted); watcher.Changed += new FileSystemEventHandler(OnChanged); timer.Interval = 1000; timer.AutoReset = true; timer.Elapsed += UploadFile; timer.Start(); Console.WriteLine("当前已经进入开发模式, 修改的文件将自动传输到设备."); while (true) { Console.WriteLine("按 C 键退出开发模式, 按 R 重启设备."); char devKey = Console.ReadKey(true).KeyChar; if (devKey == 'c') { Console.WriteLine("已退出开发模式"); Stop(); break; } if (devKey == 'r') { Console.WriteLine("重启设备"); uart.SendCode(port, "E.reboot();"); } } }
public static void Run(Uart uart, string port, string[] args) { string cmd = "list"; if (args.Length <= 1) { cmd = args[0]; } else { cmd = args[1]; } switch (cmd) { case "ll": case "ls": case "dir": case "list": uart.SendCode(port, @"(function(){var list=require('Storage').list();console.log(list.join('\n'));})();"); break; case "free": uart.SendCode(port, "(_=>{"); uart.SendCode(port, "mem=process.memory();"); uart.SendCode(port, "usage=mem.usage*mem.blocksize + 'B';"); uart.SendCode(port, "total=mem.total*mem.blocksize + 'B';"); uart.SendCode(port, "s=require('Storage').getFree() + 'B';"); uart.SendCode(port, "console.log('storage free: ', s);"); uart.SendCode(port, "console.log('memory used: ' + usage + '/' + total);"); uart.SendCode(port, "})();"); break; case "clear": uart.SendCode(port, "require('Storage').eraseAll();E.reboot();"); break; case "delete": case "remove": if (args.Length <= 1) { Console.WriteLine("请输入Storage的名称"); return; } uart.SendCode(port, "require('Storage').erase('" + args[2] + "')"); break; case "cat": case "get": case "read": if (args.Length <= 1) { Console.WriteLine("请输入Storage的名称"); return; } uart.SendCode(port, "console.log(require('Storage').read('" + args[2] + "'))"); break; case "save": case "write": if (args.Length <= 3) { Console.WriteLine("Storage的名称或内容不能为空"); return; } uart.SendCode(port, "require('Storage').write('" + args[2] + "','" + args[3] + "')"); break; } }
/// <summary> /// 运行串口相关命令 /// </summary> public void UartCommand() { string cmd = GetParam(0, ""); switch (cmd) { case "restart": case "reboot": uart.SendCode(port, "E.reboot();"); break; case "reset": uart.SendCode(port, "reset(true);"); break; case "blink": new Upload(workDir, uart, port).WriteBlinkCode(GetParam(1, "NodeMCU.D4")); break; case "flash": Flash.Write(port, GetParam(1, "")); break; case "rm": case "del": uart.SendCode(port, "require('Storage').erase('" + GetParamOrReadLine(1, "请输入Storage的名称: ") + "')"); break; case "ll": case "ls": case "dir": case "free": case "storage": Storage.Run(uart, port, args); break; case "get": case "cat": uart.SendCode(port, "console.log(require('Storage').read('" + GetParamOrReadLine(1, "请输入文件名: ") + "'))"); break; case "exec": case "run": uart.SendCode(port, GetParamOrReadLine(1, "请输入代码")); break; case "load": new Upload(workDir, uart, port).LoadFile(GetParamOrReadLine(1, "请输入要加载的文件名: ")); break; case "upload": new Upload(workDir, uart, port).Path(GetParam(1, "")); break; case "dev": new DevMode(workDir, uart, port).Run(); break; case "boot": new Upload(workDir, uart, port).SendBootCodeFromFile(GetParamOrReadLine(1, "请输入要启动的文件名: ")); break; case "connect": new Upload(workDir, uart, port).ConnectWifi(GetParamOrReadLine(1, "请输入wifi名称: "), GetParamOrReadLine(2, "请输入wifi密码: ")); break; case "status": new Upload(workDir, uart, port).GetStatus(); break; case "shell": ShellMode.Run(uart, port); break; case "<<<": InputMode.Run(uart, port); break; case "hex": uart.SendHex(port, String.Join(" ", args).Replace("hex ", "")); break; default: Console.WriteLine("命令不存在: " + cmd); break; } }