private void Button_OPEN_Click(object sender, EventArgs e) { for (var i = 0; i < CONNECTED_PRINTER_LIST.Count; i++) { if (CONNECTED_PRINTER_LIST[i].USB_SYMBOLIC_NAME.Equals(comboBox_Printer.Text)) { Selected_Printer = CONNECTED_PRINTER_LIST[i]; Selected_Printer.READ_TIMEOUT = 50; Selected_Printer.WRITE_TIMEOUT = 1000; if (Selected_Printer.OpenDevice()) { timer1.Enabled = true; button_Refresh.Enabled = false; button_Open.Enabled = false; comboBox_Printer.Enabled = false; button_closeport.Enabled = true; button_Send.Enabled = true; checkBox_printer.Enabled = false; checkBox_scanner.Enabled = false; //button_sendFile.Enabled = true; TextBox_fileName_TextChanged(this, EventArgs.Empty); } else { _logger.AddText("Port open failure", (byte)DataDirection.Error, DateTime.Now, TextLogger.TextLogger.TextFormat.PlainText); } return; } } }
private async void Button_sendFile_ClickAsync(object sender, EventArgs e) { if (SendComing > 0) { SendComing++; } else if (SendComing == 0) { timer1.Enabled = false; if (textBox_fileName.Text != "" && textBox_sendNum.Text != "" && ushort.TryParse(textBox_sendNum.Text, out var repeat) && ushort.TryParse(textBox_delay.Text, out var delay) && ushort.TryParse(textBox_strDelay.Text, out var strDelay)) { timer1.Enabled = false; SendComing = 1; button_Send.Enabled = false; button_closeport.Enabled = false; button_openFile.Enabled = false; button_sendFile.Text = "Stop"; textBox_fileName.Enabled = false; textBox_sendNum.Enabled = false; textBox_delay.Enabled = false; textBox_strDelay.Enabled = false; for (var n = 0; n < repeat; n++) { var outStr = ""; var outErr = ""; long length = 0; if (repeat > 1) { _logger.AddText(" Send cycle " + (n + 1) + "/" + repeat + ">> ", (byte)DataDirection.Info, DateTime.Now, TextLogger.TextLogger.TextFormat.PlainText); } try { length = new FileInfo(textBox_fileName.Text).Length; } catch (Exception ex) { MessageBox.Show("\r\nError opening file " + textBox_fileName.Text + ": " + ex.Message); } //binary file read if (!checkBox_hexFileOpen.Checked) { //byte-by-byte if (radioButton_byByte.Checked) { var tmpBuffer = new byte[length]; try { tmpBuffer = File.ReadAllBytes(textBox_fileName.Text); } catch (Exception ex) { MessageBox.Show("\r\nError reading file " + textBox_fileName.Text + ": " + ex.Message); } for (var l = 0; l < tmpBuffer.Length; l++) { byte[] outByte = { tmpBuffer[l] }; outStr = Encoding.GetEncoding(Settings.Default.CodePage).GetString(tmpBuffer); _logger.AddText(outStr, (byte)DataDirection.Sent, DateTime.Now); if (Selected_Printer.GenericWrite(outByte)) { progressBar1.Value = (n * tmpBuffer.Length + l) * 100 / (repeat * tmpBuffer.Length); if (strDelay > 0) { await TaskEx.Delay(strDelay); } ReadUSB(); } else { _logger.AddText("Byte " + l + ": Write Failure", (byte)DataDirection.Error, DateTime.Now, TextLogger.TextLogger.TextFormat.PlainText); } if (SendComing > 1) { l = tmpBuffer.Length; } } } //stream else { var tmpBuffer = new byte[length]; try { tmpBuffer = File.ReadAllBytes(textBox_fileName.Text); } catch (Exception ex) { MessageBox.Show("\r\nError reading file " + textBox_fileName.Text + ": " + ex.Message); } var l = 0; while (l < tmpBuffer.Length) { var bufsize = tmpBuffer.Length - l; if (bufsize > CePrinter.USB_PACK) { bufsize = CePrinter.USB_PACK; } var buf = new byte[bufsize]; for (var i = 0; i < bufsize; i++) { buf[i] = tmpBuffer[l]; l++; } var r = 0; if (Selected_Printer != null) { while (r < 10 && !Selected_Printer.GenericWrite(buf)) { _logger.AddText("USB write retry " + r, (byte)DataDirection.Error, DateTime.Now, TextLogger.TextLogger.TextFormat.PlainText); await TaskEx.Delay(100); Selected_Printer.CloseDevice(); Selected_Printer.OpenDevice(); r++; } } if (r >= 10) { outErr = "Block write failure"; } ReadUSB(); if (checkBox_hexTerminal.Checked) { outStr = Accessory.ConvertByteArrayToHex(buf, buf.Length); } else { outStr = Encoding.GetEncoding(Settings.Default.CodePage).GetString(buf); } if (outErr != "") { _logger.AddText(outErr + ": start", (byte)DataDirection.Error, DateTime.Now, TextLogger.TextLogger.TextFormat.PlainText); } _logger.AddText(outStr, (byte)DataDirection.Sent, DateTime.Now); if (outErr != "") { _logger.AddText(outErr + ": end", (byte)DataDirection.Error, DateTime.Now, TextLogger.TextLogger.TextFormat.PlainText); } progressBar1.Value = (n * tmpBuffer.Length + l) * 100 / (repeat * tmpBuffer.Length); if (SendComing > 1) { l = tmpBuffer.Length; } } } } //hex file read else { //String-by-string if (radioButton_byString.Checked) { string[] tmpBuffer = { }; try { tmpBuffer = File.ReadAllText(textBox_fileName.Text).Replace('\n', '\r') .Replace("\r\r", "\r").Split('\r'); } catch (Exception ex) { MessageBox.Show("\r\nError reading file " + textBox_fileName.Text + ": " + ex.Message); } for (var l = 0; l < tmpBuffer.Length; l++) { if (tmpBuffer[l] != "") { tmpBuffer[l] = Accessory.CheckHexString(tmpBuffer[l]); _logger.AddText(outStr, (byte)DataDirection.Sent, DateTime.Now); if (Selected_Printer.GenericWrite(Accessory.ConvertHexToByteArray(tmpBuffer[l])) ) { if (checkBox_hexTerminal.Checked) { outStr = tmpBuffer[l]; } else { outStr = Accessory.ConvertHexToString(tmpBuffer[l]); } if (strDelay > 0) { await TaskEx.Delay(strDelay); } ReadUSB(); } else //?????????????? { outErr = "String" + l + ": Write failure"; } if (SendComing > 1) { l = tmpBuffer.Length; } if (outErr != "") { _logger.AddText(outErr, (byte)DataDirection.Error, DateTime.Now, TextLogger.TextLogger.TextFormat.PlainText); } progressBar1.Value = (n * tmpBuffer.Length + l) * 100 / (repeat * tmpBuffer.Length); } } } //byte-by-byte if (radioButton_byByte.Checked) { var tmpStrBuffer = ""; try { tmpStrBuffer = Accessory.CheckHexString(File.ReadAllText(textBox_fileName.Text)); } catch (Exception ex) { MessageBox.Show("Error reading file " + textBox_fileName.Text + ": " + ex.Message); } var tmpBuffer = new byte[tmpStrBuffer.Length / 3]; tmpBuffer = Accessory.ConvertHexToByteArray(tmpStrBuffer); for (var l = 0; l < tmpBuffer.Length; l++) { byte[] outByte = { tmpBuffer[l] }; outStr = Encoding.GetEncoding(Settings.Default.CodePage).GetString(tmpBuffer); _logger.AddText(outStr, (byte)DataDirection.Sent, DateTime.Now); if (Selected_Printer.GenericWrite(outByte)) { progressBar1.Value = (n * tmpBuffer.Length + l) * 100 / (repeat * tmpBuffer.Length); if (strDelay > 0) { await TaskEx.Delay(strDelay); } ReadUSB(); } else { _logger.AddText("Byte " + l + ": Write Failure", (byte)DataDirection.Error, DateTime.Now, TextLogger.TextLogger.TextFormat.PlainText); } if (SendComing > 1) { l = tmpBuffer.Length; } } } //stream else { var tmpStrBuffer = ""; try { tmpStrBuffer = Accessory.CheckHexString(File.ReadAllText(textBox_fileName.Text)); } catch (Exception ex) { MessageBox.Show("Error reading file " + textBox_fileName.Text + ": " + ex.Message); } var tmpBuffer = new byte[tmpStrBuffer.Length / 3]; tmpBuffer = Accessory.ConvertHexToByteArray(tmpStrBuffer); var l = 0; while (l < tmpBuffer.Length) { var bufsize = tmpBuffer.Length - l; if (bufsize > CePrinter.USB_PACK) { bufsize = CePrinter.USB_PACK; } var buf = new byte[bufsize]; for (var i = 0; i < bufsize; i++) { buf[i] = tmpBuffer[l]; l++; } var r = 0; if (Selected_Printer != null) { while (r < 10 && !Selected_Printer.GenericWrite(buf)) { _logger.AddText("USB write retry " + r, (byte)DataDirection.Error, DateTime.Now, TextLogger.TextLogger.TextFormat.PlainText); await TaskEx.Delay(100); Selected_Printer.CloseDevice(); Selected_Printer.OpenDevice(); r++; } } if (r >= 10) { outErr = "Block write failure"; } ReadUSB(); outStr = Encoding.GetEncoding(Settings.Default.CodePage).GetString(buf); if (outErr != "") { _logger.AddText(outErr + " start", (byte)DataDirection.Error, DateTime.Now, TextLogger.TextLogger.TextFormat.PlainText); } _logger.AddText(outStr, (byte)DataDirection.Sent, DateTime.Now); if (outErr != "") { _logger.AddText(outErr + " end", (byte)DataDirection.Error, DateTime.Now, TextLogger.TextLogger.TextFormat.PlainText); } progressBar1.Value = (n * tmpBuffer.Length + l) * 100 / (repeat * tmpBuffer.Length); if (SendComing > 1) { l = tmpBuffer.Length; } } } } if (repeat > 1) { await TaskEx.Delay(delay); } if (SendComing > 1) { n = repeat; } } button_Send.Enabled = true; button_closeport.Enabled = true; button_openFile.Enabled = true; button_sendFile.Text = "Send file"; textBox_fileName.Enabled = true; textBox_sendNum.Enabled = true; textBox_delay.Enabled = true; textBox_strDelay.Enabled = true; } SendComing = 0; timer1.Enabled = true; } }