public ExcelComms([NotNull] CellLocation cellLocation) { Requires.NotNull(cellLocation, nameof(cellLocation)); this.cellLocation = cellLocation; }
private static void Main([NotNull][ItemNotNull] string[] args) { if (args.Length != 3) { FatalError(Resources.Expected3Arguments); } var instances = GetExcelInstances(); if (instances.Count == 0) { FatalError(Resources.ExcelIsNotRunningPleaseOpenExcel); } var cellLocation = new CellLocation(workBookName: args[0], workSheetName: args[1], rangeName: args[2]); void GotResponse() { _ = Interlocked.Exchange(ref gotResponse, 1); } IScaleListener scaleListener = new ScaleListener(GotResponse); void SetResponse(string data) { Requires.NotNull(data, nameof(data)); scaleListener.Response = data; Console.WriteLine(Resources.ReceivedResponse0, scaleListener.Response); } ICommChannel scaleComms = new CommChannel(SetResponse); if (!scaleComms.OpenPort()) { FatalError(Resources.FailedToOpenSerialPortConnection); } void OnKeyPressed() { const string printCommand = "P\r"; Console.WriteLine(Resources.GotPrintCommand); scaleComms.WriteData(printCommand); } IKeyboardListener keyboardListener = new KeyboardListener(OnKeyPressed); void ListenToScale() { scaleListener.ListenToScale(); } var mainThread = new Thread(ListenToScale); var consoleKeyListener = new Thread(keyboardListener.ListenerKeyBoardEvent); consoleKeyListener.Start(); mainThread.Start(); while (true) { if (gotResponse == 1) { mainThread.Abort(); consoleKeyListener.Abort(); break; } } IExcelComms excel = new ExcelComms(cellLocation); if (!excel.TryFindWorkbookByName(out var workBook)) { FatalError(Resources.ExcelIsNotRunning); } if (!excel.TryWriteStringToWorksheet(workBook, scaleListener.Response)) { FatalError(Resources.FailedToWriteToSpreadsheet); } scaleComms.ClosePort(); }