private void Button_Send_Click(object sender, EventArgs e) { if (textBox_command.Text + textBox_param.Text != "") { String sendStrHex; if (checkBox_hexCommand.Checked) { sendStrHex = textBox_command.Text; } else { sendStrHex = Accessory.ConvertStringToHex(textBox_command.Text); } if (checkBox_hexParam.Checked) { sendStrHex += textBox_param.Text; } else { sendStrHex += Accessory.ConvertStringToHex(textBox_param.Text); } try { serialPort1.Write(Accessory.ConvertHexToByteArray(sendStrHex), 0, sendStrHex.Length / 3); } catch (Exception ex) { MessageBox.Show("Error sending to port " + serialPort1.PortName + ": " + ex.Message); } var outStr = Accessory.ConvertHexToString(sendStrHex); _logger.AddText(outStr, (byte)DataDirection.Sent, DateTime.Now); } }
private void Button_WRITE_Click(object sender, EventArgs e) { if (textBox_command.Text + textBox_param.Text != "") { string outStr; if (checkBox_hexCommand.Checked) { outStr = textBox_command.Text; } else { outStr = Accessory.ConvertStringToHex(textBox_command.Text); } if (checkBox_hexParam.Checked) { outStr += textBox_param.Text; } else { outStr += Accessory.ConvertStringToHex(textBox_param.Text); } if (outStr != "") { _logger.AddText(Accessory.ConvertHexToString(outStr), (byte)DataDirection.Sent, DateTime.Now); textBox_command.AutoCompleteCustomSource.Add(textBox_command.Text); textBox_param.AutoCompleteCustomSource.Add(textBox_param.Text); var outStream = Accessory.ConvertHexToByteArray(outStr); WriteTCP(outStream); } } Timer1_Tick(this, EventArgs.Empty); }
private void Button_Send_Click(object sender, EventArgs e) { byte[] buff = { }; if (textBox_command.Text + textBox_command.Text != "") { var sendStrHex = ""; if (checkBox_hexCommand.Checked) { sendStrHex = textBox_command.Text; } else { sendStrHex = Accessory.ConvertStringToHex(textBox_command.Text); } if (checkBox_hexParam.Checked) { sendStrHex += textBox_param.Text; } else { sendStrHex += Accessory.ConvertStringToHex(textBox_param.Text); } if (sendStrHex != "") { LptOpen(comboBox_portname1.SelectedItem.ToString()); if (!_isConnected) { MessageBox.Show("Error opening port " + comboBox_portname1.SelectedItem); comboBox_portname1.Enabled = true; } else { if (checkBox_hexTerminal.Checked) { SetText(">> " + sendStrHex + "\r\n"); } else { SetText(">> " + Accessory.ConvertHexToString(sendStrHex) + "\r\n"); } textBox_command.AutoCompleteCustomSource.Add(textBox_command.Text); textBox_param.AutoCompleteCustomSource.Add(textBox_param.Text); try { _lptPort.Write(Accessory.ConvertHexToByteArray(sendStrHex), 0, sendStrHex.Length / 3); } catch (Exception ex) { MessageBox.Show("Error sending to port " + comboBox_portname1.SelectedItem + ": " + ex.Message); } } } } }
private void CheckBox_hexParam_CheckedChanged(object sender, EventArgs e) { if (checkBox_hexParam.Checked) { textBox_param.Text = Accessory.ConvertStringToHex(textBox_param.Text); } else { textBox_param.Text = Accessory.ConvertHexToString(textBox_param.Text); } }
private void checkBox_hexCommand_CheckedChanged(object sender, EventArgs e) { if (checkBox_hexCommand.Checked == true) { textBox_command.Text = Accessory.ConvertStringToHex(textBox_command.Text); } else { textBox_command.Text = Accessory.ConvertHexToString(textBox_command.Text); } }
private void Button_WRITE_Click(object sender, EventArgs e) { if (Selected_Printer != null) { if (textBox_command.Text + textBox_param.Text != "") { string outStr; if (checkBox_hexCommand.Checked) { outStr = textBox_command.Text; } else { outStr = Accessory.ConvertStringToHex(textBox_command.Text); } if (checkBox_hexParam.Checked) { outStr += textBox_param.Text; } else { outStr += Accessory.ConvertStringToHex(textBox_param.Text); } if (outStr != "") { timer1.Enabled = false; _logger.AddText(Accessory.ConvertHexToString(outStr), (byte)DataDirection.Sent, DateTime.Now); textBox_command.AutoCompleteCustomSource.Add(textBox_command.Text); textBox_param.AutoCompleteCustomSource.Add(textBox_param.Text); if (Selected_Printer.GenericWrite(Accessory.ConvertHexToByteArray(outStr))) { ReadUSB(); } else { _logger.AddText("Write failure", (byte)DataDirection.Error, DateTime.Now, TextLogger.TextLogger.TextFormat.PlainText); } timer1.Enabled = true; } } } else { Button_CLOSE_Click(this, EventArgs.Empty); } }
private void button_Send_Click(object sender, EventArgs e) { if (textBox_command.Text != "" || textBox_param.Text != "") { string outStr = ""; string sendStrHex = ""; if (checkBox_hexCommand.Checked == true) { sendStrHex = textBox_command.Text; } else { sendStrHex = Accessory.ConvertStringToHex(textBox_command.Text); } if (checkBox_hexParam.Checked == true) { sendStrHex += textBox_param.Text; } else { sendStrHex += Accessory.ConvertStringToHex(textBox_param.Text); } try { serialPort1.Write(Accessory.ConvertHexToByteArray(sendStrHex), 0, sendStrHex.Length / 3); } catch (Exception ex) { MessageBox.Show("Error sending to port " + serialPort1.PortName + ": " + ex.Message); } if (checkBox_hexTerminal.Checked == true) { outStr = sendStrHex; } else { outStr = Accessory.ConvertHexToString(sendStrHex); } collectBuffer(outStr, Port1DataOut); } }
private void button_Send_Click(object sender, EventArgs e) { if (comboBox_Printer.Items.Count > 0 && comboBox_Printer.SelectedItem.ToString() != "") { if (textBox_command.Text + textBox_param.Text != "") { string outStr; if (checkBox_hexCommand.Checked) { outStr = textBox_command.Text; } else { outStr = Accessory.ConvertStringToHex(textBox_command.Text); } if (checkBox_hexParam.Checked) { outStr += textBox_param.Text; } else { outStr += Accessory.ConvertStringToHex(textBox_param.Text); } if (outStr != "") { if (checkBox_hexTerminal.Checked) { collectBuffer(outStr, Port1DataOut); } else { collectBuffer(Accessory.ConvertHexToString(outStr), Port1DataOut); } textBox_command.AutoCompleteCustomSource.Add(textBox_command.Text); textBox_param.AutoCompleteCustomSource.Add(textBox_param.Text); RawPrinterSender.SendRAWToPrinter(comboBox_Printer.SelectedItem.ToString(), Accessory.ConvertHexToByteArray(outStr)); } } } }
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; } }
private async void Button_sendFile_ClickAsync(object sender, EventArgs e) { if (_sendComing > 0) { _sendComing++; } else if (_sendComing == 0) { if (textBox_fileName.Text != "" && textBox_sendNum.Text != "" && ushort.TryParse(textBox_sendNum.Text, out ushort repeat) && ushort.TryParse(textBox_delay.Text, out ushort delay) && ushort.TryParse(textBox_strDelay.Text, out ushort strDelay)) { _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++) { string outStr; long length = 0; if (repeat > 1) { outStr = "\r\nSend cycle " + (n + 1).ToString() + "/" + repeat.ToString() + "\r\n"; } else { outStr = ""; } _logger.AddText(outStr, (byte)DataDirection.Sent, 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); } if (!checkBox_hexFileOpen.Checked) //binary data read { if (radioButton_byByte.Checked) //byte-by-byte { 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); } try { for (var m = 0; m < tmpBuffer.Length; m++) { serialPort1.Write(tmpBuffer, m, 1); progressBar1.Value = (n * tmpBuffer.Length + m) * 100 / (repeat * tmpBuffer.Length); if (strDelay > 0) { await TaskEx.Delay(strDelay); } if (_sendComing > 1) { m = tmpBuffer.Length; } byte[] outByte = new[] { tmpBuffer[m] }; outStr = Accessory.ConvertByteArrayToString(tmpBuffer); _logger.AddText(outStr, (byte)DataDirection.Sent, DateTime.Now); } } catch (Exception ex) { _logger.AddText("Error sending to port " + serialPort1.PortName + ": " + ex.Message, (byte)DataDirection.Error, DateTime.Now, TextLogger.TextLogger.TextFormat.PlainText); } } else //stream { var tmpBuffer = new byte[length]; progressBar1.Value = 0; try { tmpBuffer = File.ReadAllBytes(textBox_fileName.Text); } catch (Exception ex) { MessageBox.Show("\r\nError reading file " + textBox_fileName.Text + ": " + ex.Message); } try { for (var m = 0; m < tmpBuffer.Length; m++) { serialPort1.Write(tmpBuffer, m, 1); progressBar1.Value = (n * tmpBuffer.Length + m) * 100 / (repeat * tmpBuffer.Length); } } catch (Exception ex) { _logger.AddText("Error sending to port " + serialPort1.PortName + ": " + ex.Message, (byte)DataDirection.Error, DateTime.Now, TextLogger.TextLogger.TextFormat.PlainText); } progressBar1.Value = 100; outStr = Accessory.ConvertByteArrayToString(tmpBuffer); _logger.AddText(outStr, (byte)DataDirection.Sent, DateTime.Now); } } else //hex text read { if (radioButton_byString.Checked) //String-by-string { string[] tmpBuffer = { }; try { tmpBuffer = File.ReadAllLines(textBox_fileName.Text); } catch (Exception ex) { MessageBox.Show("\r\nError reading file " + textBox_fileName.Text + ": " + ex.Message); } for (var m = 0; m < tmpBuffer.Length; m++) { tmpBuffer[m] = Accessory.CheckHexString(tmpBuffer[m]); } try { for (var m = 0; m < tmpBuffer.Length; m++) { var s = Accessory.ConvertHexToByteArray(tmpBuffer[m]); serialPort1.Write(s, 0, s.Length); outStr = Accessory.ConvertHexToString(tmpBuffer[m]); _logger.AddText(outStr, (byte)DataDirection.Sent, DateTime.Now); progressBar1.Value = (n * tmpBuffer.Length + m) * 100 / (repeat * tmpBuffer.Length); if (strDelay > 0) { await TaskEx.Delay(strDelay); } if (_sendComing > 1) { m = tmpBuffer.Length; } } } catch (Exception ex) { _logger.AddText("Error sending to port " + serialPort1.PortName + ": " + ex.Message, (byte)DataDirection.Error, DateTime.Now, TextLogger.TextLogger.TextFormat.PlainText); } } else if (radioButton_byByte.Checked) //byte-by-byte { var tmpBuffer = ""; try { length = new FileInfo(textBox_fileName.Text).Length; tmpBuffer = File.ReadAllText(textBox_fileName.Text); } catch (Exception ex) { MessageBox.Show("\r\nError reading file " + textBox_fileName.Text + ": " + ex.Message); } tmpBuffer = Accessory.CheckHexString(tmpBuffer); try { for (var m = 0; m < tmpBuffer.Length; m += 3) { var outByte = tmpBuffer.Substring(m, 3); serialPort1.Write(Accessory.ConvertHexToByteArray(outByte), 0, 1); progressBar1.Value = (n * tmpBuffer.Length + m) * 100 / (repeat * tmpBuffer.Length); if (strDelay > 0) { await TaskEx.Delay(strDelay); } if (_sendComing > 1) { m = tmpBuffer.Length; } outStr = Accessory.ConvertHexToString(outByte); _logger.AddText(outStr, (byte)DataDirection.Sent, DateTime.Now); } } catch (Exception ex) { MessageBox.Show("Error sending to port " + serialPort1.PortName + ": " + ex.Message); } } else //raw stream { var tmpBuffer = ""; progressBar1.Value = 0; try { tmpBuffer = Accessory.CheckHexString(File.ReadAllText(textBox_fileName.Text)); } catch (Exception ex) { MessageBox.Show("\r\nError reading file " + textBox_fileName.Text + ": " + ex.Message); } try { for (var m = 0; m < tmpBuffer.Length; m += 3) { serialPort1.Write(Accessory.ConvertHexToByteArray(tmpBuffer.Substring(m, 3)), 0, 1); progressBar1.Value = (n * tmpBuffer.Length + m) * 100 / (repeat * tmpBuffer.Length); } } catch (Exception ex) { _logger.AddText("Error sending to port " + serialPort1.PortName + ": " + ex.Message, (byte)DataDirection.Error, DateTime.Now, TextLogger.TextLogger.TextFormat.PlainText); } progressBar1.Value = 100; outStr = Accessory.ConvertHexToString(tmpBuffer); _logger.AddText(outStr, (byte)DataDirection.Sent, DateTime.Now); } } if (repeat > 1 && delay > 0) { 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; } }
private async void Button_sendFile_ClickAsync(object sender, EventArgs e) { if (_sendComing > 0) { _sendComing++; } else if (_sendComing == 0) { int repeat = 1, delay = 1, strDelay = 1; if (textBox_fileName.Text != "" && textBox_sendNum.Text != "" && int.TryParse(textBox_sendNum.Text, out repeat) && int.TryParse(textBox_delay.Text, out delay) && int.TryParse(textBox_strDelay.Text, out strDelay)) { _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++) { string outStr; long length = 0; try { length = new FileInfo(textBox_fileName.Text).Length; } catch (Exception ex) { MessageBox.Show("\r\nError opening file " + textBox_fileName.Text + ": " + ex.Message); } if (!checkBox_hexFileOpen.Checked) //binary data read { if (radioButton_byByte.Checked) //byte-by-byte { 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); } LptOpen(comboBox_portname1.SelectedItem.ToString()); if (!_isConnected) { MessageBox.Show("Error opening port " + comboBox_portname1.SelectedItem); comboBox_portname1.Enabled = true; return; } try { for (var m = 0; m < tmpBuffer.Length; m++) { _lptPort.Write(tmpBuffer, m, 1); progressBar1.Value = (n * tmpBuffer.Length + m) * 100 / (repeat * tmpBuffer.Length); if (strDelay > 0) { await TaskEx.Delay(strDelay); } //ReadLPT(); outStr = ">> "; if (checkBox_hexTerminal.Checked) { outStr += Accessory.ConvertByteArrayToHex(tmpBuffer); } else { outStr += Accessory.ConvertHexToString( Accessory.ConvertByteArrayToHex(tmpBuffer)); } outStr += "\r\n"; SetText(outStr); if (_sendComing > 1) { m = tmpBuffer.Length; } } } catch (Exception ex) { MessageBox.Show("Error sending to port " + comboBox_portname1.SelectedItem + ": " + ex.Message); } } 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); } LptOpen(comboBox_portname1.SelectedItem.ToString()); if (!_isConnected) { MessageBox.Show("Error opening port " + comboBox_portname1.SelectedItem); comboBox_portname1.Enabled = true; return; } try { _lptPort.Write(tmpBuffer, 0, tmpBuffer.Length); //ReadLPT(); outStr = ">> "; if (checkBox_hexTerminal.Checked) { outStr += Accessory.ConvertByteArrayToHex(tmpBuffer); } else { outStr += Accessory.ConvertHexToString( Accessory.ConvertByteArrayToHex(tmpBuffer)); } outStr += "\r\n"; SetText(outStr); progressBar1.Value = n * 100 / (repeat * tmpBuffer.Length); } catch (Exception ex) { MessageBox.Show("Error sending to port " + comboBox_portname1.SelectedItem + ": " + ex.Message); } } } else //hex text read { if (radioButton_byString.Checked) //String-by-string { string[] tmpBuffer = { }; try { length = new FileInfo(textBox_fileName.Text).Length; tmpBuffer = File.ReadAllText(textBox_fileName.Text).Replace("\n", "").Split('\r'); } catch (Exception ex) { MessageBox.Show("\r\nError reading file " + textBox_fileName.Text + ": " + ex.Message); } LptOpen(comboBox_portname1.SelectedItem.ToString()); if (!_isConnected) { MessageBox.Show("Error opening port " + comboBox_portname1.SelectedItem); comboBox_portname1.Enabled = true; return; } try { for (var m = 0; m < tmpBuffer.Length; m++) { tmpBuffer[m] = Accessory.CheckHexString(tmpBuffer[m]); _lptPort.Write(Accessory.ConvertHexToByteArray(tmpBuffer[m]), 0, tmpBuffer[m].Length / 3); outStr = ">> "; if (checkBox_hexTerminal.Checked) { outStr += tmpBuffer[m]; } else { outStr += Accessory.ConvertHexToString(tmpBuffer[m]); } outStr += "\r\n"; if (strDelay > 0) { await TaskEx.Delay(strDelay); } //ReadLPT(); if (_sendComing > 1) { m = tmpBuffer.Length; } SetText(outStr); progressBar1.Value = (n * tmpBuffer.Length + m) * 100 / (repeat * tmpBuffer.Length); } } catch (Exception ex) { MessageBox.Show("Error sending to port " + comboBox_portname1.SelectedItem + ": " + ex.Message); } if (repeat > 1) { outStr = "\r\nSend cycle " + (n + 1) + "/" + repeat + "\r\n"; } else { outStr = ""; } //if (checkBox_hexTerminal.Checked) outStr += tmpBuffer; //else outStr += ConvertHexToString(tmpBuffer.ToString()); } else if (radioButton_byByte.Checked) //byte-by-byte { var tmpBuffer = ""; try { length = new FileInfo(textBox_fileName.Text).Length; tmpBuffer = File.ReadAllText(textBox_fileName.Text); } catch (Exception ex) { MessageBox.Show("\r\nError reading file " + textBox_fileName.Text + ": " + ex.Message); } tmpBuffer = Accessory.CheckHexString(tmpBuffer); LptOpen(comboBox_portname1.SelectedItem.ToString()); if (!_isConnected) { MessageBox.Show("Error opening port " + comboBox_portname1.SelectedItem); comboBox_portname1.Enabled = true; return; } try { for (var m = 0; m < tmpBuffer.Length; m += 3) { _lptPort.Write(Accessory.ConvertHexToByteArray(tmpBuffer.Substring(m, 3)), 0, 1); outStr = ">> "; if (checkBox_hexTerminal.Checked) { outStr += tmpBuffer.Substring(m, 3); } else { outStr += Accessory.ConvertHexToString(tmpBuffer.Substring(m, 3)); } outStr += "\r\n"; if (strDelay > 0) { await TaskEx.Delay(strDelay); } //ReadLPT(); progressBar1.Value = (n * tmpBuffer.Length + m) * 100 / (repeat * tmpBuffer.Length); if (_sendComing > 1) { m = tmpBuffer.Length; } SetText(outStr); progressBar1.Value = (n * tmpBuffer.Length + m) * 100 / (repeat * tmpBuffer.Length); } } catch (Exception ex) { MessageBox.Show("Error sending to port " + comboBox_portname1.SelectedItem + ": " + ex.Message); } if (repeat > 1) { outStr = " Send cycle " + (n + 1) + "/" + repeat + ">> "; } else { outStr = ""; } } else //raw stream { var tmpBuffer = ""; try { length = new FileInfo(textBox_fileName.Text).Length; tmpBuffer = Accessory.CheckHexString(File.ReadAllText(textBox_fileName.Text)); } catch (Exception ex) { MessageBox.Show("\r\nError reading file " + textBox_fileName.Text + ": " + ex.Message); } LptOpen(comboBox_portname1.SelectedItem.ToString()); if (!_isConnected) { MessageBox.Show("Error opening port " + comboBox_portname1.SelectedItem); comboBox_portname1.Enabled = true; return; } try { _lptPort.Write(Accessory.ConvertHexToByteArray(tmpBuffer), 0, tmpBuffer.Length / 3); //ReadLPT(); } catch (Exception ex) { MessageBox.Show("Error sending to port " + comboBox_portname1.SelectedItem + ": " + ex.Message); } if (repeat > 1) { outStr = " Send cycle " + (n + 1) + "/" + repeat + ">> "; } else { outStr = ">> "; } if (checkBox_hexTerminal.Checked) { outStr += tmpBuffer; } else { outStr += Accessory.ConvertHexToString(tmpBuffer); } outStr += "\r\n"; SetText(outStr); progressBar1.Value = n * 100 / (repeat * 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; } }
private void button_sendFile_Click(object sender, EventArgs e) { if (SendComing > 0) { SendComing++; } else if (SendComing == 0) { UInt16 repeat = 1, delay = 1, strDelay = 1; if (textBox_fileName.Text != "" && textBox_sendNum.Text != "" && UInt16.TryParse(textBox_sendNum.Text, out repeat) && UInt16.TryParse(textBox_delay.Text, out delay) && UInt16.TryParse(textBox_strDelay.Text, out strDelay)) { 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 (int n = 0; n < repeat; n++) { string outStr; long length = 0; if (repeat > 1) { outStr = "\r\nSend cycle " + (n + 1).ToString() + "/" + repeat.ToString() + "\r\n"; } else { outStr = ""; } try { length = new FileInfo(textBox_fileName.Text).Length; } catch (Exception ex) { MessageBox.Show("\r\nError opening file " + textBox_fileName.Text + ": " + ex.Message); } if (!checkBox_hexFileOpen.Checked) //binary data read { if (radioButton_byByte.Checked) //byte-by-byte { byte[] 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); } try { for (int m = 0; m < tmpBuffer.Length; m++) { serialPort1.Write(tmpBuffer, m, 1); progressBar1.Value = (n * tmpBuffer.Length + m) * 100 / (repeat * tmpBuffer.Length); if (strDelay > 0) { Accessory.DelayMs(strDelay); } if (SendComing > 1) { m = tmpBuffer.Length; } } if (checkBox_hexTerminal.Checked) { outStr += Accessory.ConvertByteArrayToHex(tmpBuffer); } else { outStr += Accessory.ConvertHexToString(Accessory.ConvertByteArrayToHex(tmpBuffer)); } collectBuffer(outStr, Port1DataOut); } catch (Exception ex) { MessageBox.Show("Error sending to port " + serialPort1.PortName + ": " + ex.Message); } } else //stream { byte[] 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); } try { for (int m = 0; m < tmpBuffer.Length; m++) { serialPort1.Write(tmpBuffer, m, 1); progressBar1.Value = (n * tmpBuffer.Length + m) * 100 / (repeat * tmpBuffer.Length); } } catch (Exception ex) { MessageBox.Show("Error sending to port " + serialPort1.PortName + ": " + ex.Message); } if (checkBox_hexTerminal.Checked) { outStr += Accessory.ConvertByteArrayToHex(tmpBuffer); } else { outStr += Accessory.ConvertHexToString(Accessory.ConvertByteArrayToHex(tmpBuffer)); } collectBuffer(outStr, Port1DataOut); } } else //hex text read { if (radioButton_byString.Checked) //String-by-string { String[] tmpBuffer = { }; try { length = new FileInfo(textBox_fileName.Text).Length; tmpBuffer = File.ReadAllText(textBox_fileName.Text).Replace("\n", "").Split('\r'); } catch (Exception ex) { MessageBox.Show("\r\nError reading file " + textBox_fileName.Text + ": " + ex.Message); } for (int m = 0; m < tmpBuffer.Length; m++) { tmpBuffer[m] = Accessory.CheckHexString(tmpBuffer[m]); } try { for (int m = 0; m < tmpBuffer.Length; m++) { serialPort1.Write(Accessory.ConvertHexToByteArray(tmpBuffer[m]), 0, tmpBuffer[m].Length / 3); if (checkBox_hexTerminal.Checked) { outStr += tmpBuffer[m]; } else { outStr += Accessory.ConvertHexToString(tmpBuffer[m].ToString()); } collectBuffer(outStr, Port1DataOut); progressBar1.Value = (n * tmpBuffer.Length + m) * 100 / (repeat * tmpBuffer.Length); if (strDelay > 0) { Accessory.DelayMs(strDelay); } if (SendComing > 1) { m = tmpBuffer.Length; } } } catch (Exception ex) { MessageBox.Show("Error sending to port " + serialPort1.PortName + ": " + ex.Message); } //if (checkBox_hexTerminal.Checked) outStr += tmpBuffer; //else outStr += ConvertHexToString(tmpBuffer.ToString()); } else if (radioButton_byByte.Checked) //byte-by-byte { String tmpBuffer = ""; try { length = new FileInfo(textBox_fileName.Text).Length; tmpBuffer = File.ReadAllText(textBox_fileName.Text); } catch (Exception ex) { MessageBox.Show("\r\nError reading file " + textBox_fileName.Text + ": " + ex.Message); } tmpBuffer = Accessory.CheckHexString(tmpBuffer); try { for (int m = 0; m < tmpBuffer.Length; m += 3) { serialPort1.Write(Accessory.ConvertHexToByteArray(tmpBuffer.Substring(m, 3)), 0, 1); progressBar1.Value = (n * tmpBuffer.Length + m) * 100 / (repeat * tmpBuffer.Length); if (strDelay > 0) { Accessory.DelayMs(strDelay); } if (SendComing > 1) { m = tmpBuffer.Length; } } if (checkBox_hexTerminal.Checked) { outStr += tmpBuffer; } else { outStr += Accessory.ConvertHexToString(tmpBuffer); } collectBuffer(outStr, Port1DataOut); } catch (Exception ex) { MessageBox.Show("Error sending to port " + serialPort1.PortName + ": " + ex.Message); } } else //raw stream { String tmpBuffer = ""; try { length = new FileInfo(textBox_fileName.Text).Length; tmpBuffer = Accessory.CheckHexString(File.ReadAllText(textBox_fileName.Text)); } catch (Exception ex) { MessageBox.Show("\r\nError reading file " + textBox_fileName.Text + ": " + ex.Message); } try { for (int m = 0; m < tmpBuffer.Length; m += 3) { serialPort1.Write(Accessory.ConvertHexToByteArray(tmpBuffer.Substring(m, 3)), 0, 1); progressBar1.Value = (n * tmpBuffer.Length + m) * 100 / (repeat * tmpBuffer.Length); } } catch (Exception ex) { MessageBox.Show("Error sending to port " + serialPort1.PortName + ": " + ex.Message); } if (checkBox_hexTerminal.Checked) { outStr += tmpBuffer; } else { outStr += Accessory.ConvertHexToString(tmpBuffer); } collectBuffer(outStr, Port1DataOut); } } if (repeat > 1) { Accessory.DelayMs(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; } }
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)) { 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); } if (!checkBox_hexFileOpen.Checked) //binary data read { if (radioButton_byByte.Checked) //byte-by-byte { 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 m = 0; m < tmpBuffer.Length; m++) { byte[] outByte = { tmpBuffer[m] }; if (WriteTCP(outByte)) { progressBar1.Value = (n * tmpBuffer.Length + m) * 100 / (repeat * tmpBuffer.Length); if (strDelay > 0) { await TaskEx.Delay(strDelay); } var inStream = ReadTCP(); if (inStream.Length > 0) { if (checkBox_saveInput.Checked) { if (checkBox_hexTerminal.Checked) { File.AppendAllText(textBox_saveTo.Text, Accessory.ConvertByteArrayToHex(inStream, inStream.Length), Encoding.GetEncoding(Settings.Default.CodePage)); } else { File.AppendAllText(textBox_saveTo.Text, Encoding.GetEncoding(Settings.Default.CodePage) .GetString(inStream), Encoding.GetEncoding(Settings.Default.CodePage)); } } _logger.AddText( Encoding.GetEncoding(Settings.Default.CodePage).GetString(inStream), (byte)DataDirection.Received, DateTime.Now); } } else { _logger.AddText("Write Failure", (byte)DataDirection.Error, DateTime.Now, TextLogger.TextLogger.TextFormat.PlainText); } outStr = Encoding.GetEncoding(Settings.Default.CodePage).GetString(tmpBuffer); _logger.AddText(outStr, (byte)DataDirection.Sent, DateTime.Now); if (SendComing > 1) { m = tmpBuffer.Length; } } } else //stream { 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); } if (WriteTCP(tmpBuffer)) { var inStream = ReadTCP(); if (inStream.Length > 0) { if (checkBox_saveInput.Checked) { if (checkBox_hexTerminal.Checked) { File.AppendAllText(textBox_saveTo.Text, Accessory.ConvertByteArrayToHex(inStream, inStream.Length), Encoding.GetEncoding(Settings.Default.CodePage)); } else { File.AppendAllText(textBox_saveTo.Text, Encoding.GetEncoding(Settings.Default.CodePage).GetString(inStream), Encoding.GetEncoding(Settings.Default.CodePage)); } } _logger.AddText( Encoding.GetEncoding(Settings.Default.CodePage).GetString(inStream), (byte)DataDirection.Received, DateTime.Now); } } else { outErr = "Write Failure"; } outStr = Encoding.GetEncoding(Settings.Default.CodePage).GetString(tmpBuffer); _logger.AddText(outStr, (byte)DataDirection.Sent, DateTime.Now); _logger.AddText(outErr, (byte)DataDirection.Error, DateTime.Now, TextLogger.TextLogger.TextFormat.PlainText); progressBar1.Value = n * 100 / (repeat * tmpBuffer.Length); } } else //hex text read { if (radioButton_byString.Checked) //String-by-string { string[] tmpBuffer = { }; try { length = new FileInfo(textBox_fileName.Text).Length; tmpBuffer = File.ReadAllText(textBox_fileName.Text).Replace("\n", "").Split('\r'); } catch (Exception ex) { MessageBox.Show("\r\nError reading file " + textBox_fileName.Text + ": " + ex.Message); } for (var m = 0; m < tmpBuffer.Length; m++) { tmpBuffer[m] = Accessory.CheckHexString(tmpBuffer[m]); if (WriteTCP(Accessory.ConvertHexToByteArray(tmpBuffer[m]))) { if (checkBox_hexTerminal.Checked) { outStr = tmpBuffer[m]; } else { outStr = Accessory.ConvertHexToString(tmpBuffer[m]); } if (strDelay > 0) { await TaskEx.Delay(strDelay); } var inStream = ReadTCP(); if (inStream.Length > 0) { if (checkBox_saveInput.Checked) { if (checkBox_hexTerminal.Checked) { File.AppendAllText(textBox_saveTo.Text, Accessory.ConvertByteArrayToHex(inStream, inStream.Length), Encoding.GetEncoding(Settings.Default.CodePage)); } else { File.AppendAllText(textBox_saveTo.Text, Encoding.GetEncoding(Settings.Default.CodePage) .GetString(inStream), Encoding.GetEncoding(Settings.Default.CodePage)); } } _logger.AddText( Encoding.GetEncoding(Settings.Default.CodePage).GetString(inStream), (byte)DataDirection.Received, DateTime.Now); } } else //?????????????? { outErr = "Write failure"; } if (SendComing > 1) { m = tmpBuffer.Length; } _logger.AddText(outStr, (byte)DataDirection.Sent, DateTime.Now); _logger.AddText(outErr, (byte)DataDirection.Error, DateTime.Now, TextLogger.TextLogger.TextFormat.PlainText); progressBar1.Value = (n * tmpBuffer.Length + m) * 100 / (repeat * tmpBuffer.Length); } } else if (radioButton_byByte.Checked) //byte-by-byte { var tmpBuffer = ""; try { length = new FileInfo(textBox_fileName.Text).Length; tmpBuffer = File.ReadAllText(textBox_fileName.Text); } catch (Exception ex) { MessageBox.Show("\r\nError reading file " + textBox_fileName.Text + ": " + ex.Message); } tmpBuffer = Accessory.CheckHexString(tmpBuffer); for (var m = 0; m < tmpBuffer.Length; m += 3) { if (WriteTCP(Accessory.ConvertHexToByteArray(tmpBuffer.Substring(m, 3)))) { if (checkBox_hexTerminal.Checked) { outStr = tmpBuffer.Substring(m, 3); } else { outStr = Accessory.ConvertHexToString(tmpBuffer.Substring(m, 3)); } if (strDelay > 0) { await TaskEx.Delay(strDelay); } var inStream = ReadTCP(); if (inStream.Length > 0) { if (checkBox_saveInput.Checked) { if (checkBox_hexTerminal.Checked) { File.AppendAllText(textBox_saveTo.Text, Accessory.ConvertByteArrayToHex(inStream, inStream.Length), Encoding.GetEncoding(Settings.Default.CodePage)); } else { File.AppendAllText(textBox_saveTo.Text, Encoding.GetEncoding(Settings.Default.CodePage) .GetString(inStream), Encoding.GetEncoding(Settings.Default.CodePage)); } } _logger.AddText( Encoding.GetEncoding(Settings.Default.CodePage).GetString(inStream), (byte)DataDirection.Received, DateTime.Now); } } else { outErr += "Write Failure\r\n"; } if (SendComing > 1) { m = tmpBuffer.Length; } _logger.AddText(outStr, (byte)DataDirection.Sent, DateTime.Now); _logger.AddText(outErr, (byte)DataDirection.Error, DateTime.Now, TextLogger.TextLogger.TextFormat.PlainText); progressBar1.Value = (n * tmpBuffer.Length + m) * 100 / (repeat * tmpBuffer.Length); } } else //stream { var tmpBuffer = ""; try { length = new FileInfo(textBox_fileName.Text).Length; tmpBuffer = Accessory.CheckHexString(File.ReadAllText(textBox_fileName.Text)); } catch (Exception ex) { MessageBox.Show("\r\nError reading file " + textBox_fileName.Text + ": " + ex.Message); } if (WriteTCP(Accessory.ConvertHexToByteArray(tmpBuffer))) { var inStream = ReadTCP(); if (inStream.Length > 0) { if (checkBox_saveInput.Checked) { if (checkBox_hexTerminal.Checked) { File.AppendAllText(textBox_saveTo.Text, Accessory.ConvertByteArrayToHex(inStream, inStream.Length), Encoding.GetEncoding(Settings.Default.CodePage)); } else { File.AppendAllText(textBox_saveTo.Text, Encoding.GetEncoding(Settings.Default.CodePage).GetString(inStream), Encoding.GetEncoding(Settings.Default.CodePage)); } } _logger.AddText( Encoding.GetEncoding(Settings.Default.CodePage).GetString(inStream), (byte)DataDirection.Received, DateTime.Now); } } else { _logger.AddText("Write Failure\r\n", (byte)DataDirection.Error, DateTime.Now, TextLogger.TextLogger.TextFormat.PlainText); } outStr = Accessory.ConvertHexToString(tmpBuffer); _logger.AddText(outStr, (byte)DataDirection.Sent, DateTime.Now); progressBar1.Value = n * 100 / (repeat * 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; } }
private async void button_sendFile_ClickAsync(object sender, EventArgs e) { if (SendComing > 0) { SendComing++; } else if (SendComing == 0) { UInt16 repeat = 1, delay = 1, strDelay = 1; if (textBox_fileName.Text != "" && textBox_sendNum.Text != "" && UInt16.TryParse(textBox_sendNum.Text, out repeat) && UInt16.TryParse(textBox_delay.Text, out delay) && UInt16.TryParse(textBox_strDelay.Text, out strDelay)) { SendComing = 1; button_Send.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 (int n = 0; n < repeat; n++) { string outStr = ""; string outErr = ""; long length = 0; if (repeat > 1) { collectBuffer(" Send cycle " + (n + 1).ToString() + "/" + repeat.ToString() + ">> ", 0); } try { length = new FileInfo(textBox_fileName.Text).Length; } catch (Exception ex) { MessageBox.Show("\r\nError opening file " + textBox_fileName.Text + ": " + ex.Message); } if (comboBox_Printer.Items.Count > 0 && comboBox_Printer.SelectedItem.ToString() != "") { //binary file read if (!checkBox_hexFileOpen.Checked) { //byte-by-byte if (radioButton_byByte.Checked) { byte[] 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 (int l = 0; l < tmpBuffer.Length; l++) { byte[] outByte = { tmpBuffer[l] }; if (checkBox_hexTerminal.Checked) { outStr = Accessory.ConvertByteArrayToHex(tmpBuffer, tmpBuffer.Length); } else { outStr = Encoding.GetEncoding(RawPrnControl.Properties.Settings.Default.CodePage).GetString(tmpBuffer); } collectBuffer(outStr, Port1DataOut); if (RawPrinterSender.SendRAWToPrinter(comboBox_Printer.SelectedItem.ToString(), outByte)) { progressBar1.Value = (n * tmpBuffer.Length + l) * 100 / (repeat * tmpBuffer.Length); if (strDelay > 0) { await TaskEx.Delay(strDelay); } } else { collectBuffer("Byte " + l.ToString() + ": Write Failure", Port1Error); } if (SendComing > 1) { l = tmpBuffer.Length; } } } //stream else { byte[] 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); } int r = 0; while (r < 10 && !RawPrinterSender.SendRAWToPrinter(comboBox_Printer.SelectedItem.ToString(), tmpBuffer)) { collectBuffer("USB write retry " + r.ToString(), Port1Error); await TaskEx.Delay(100); r++; } if (r >= 10) { outErr = "Block write failure"; } if (checkBox_hexTerminal.Checked) { outStr = Accessory.ConvertByteArrayToHex(tmpBuffer, tmpBuffer.Length); } else { outStr = Encoding.GetEncoding(RawPrnControl.Properties.Settings.Default.CodePage).GetString(tmpBuffer); } if (outErr != "") { collectBuffer(outErr + ": start", Port1Error); } collectBuffer(outStr, Port1DataOut); if (outErr != "") { collectBuffer(outErr + ": end", Port1Error); } progressBar1.Value = ((n * tmpBuffer.Length) * 100) / (repeat * 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 (int l = 0; l < tmpBuffer.Length; l++) { if (tmpBuffer[l] != "") { tmpBuffer[l] = Accessory.CheckHexString(tmpBuffer[l]); collectBuffer(outStr, Port1DataOut); if (RawPrinterSender.SendRAWToPrinter(comboBox_Printer.SelectedItem.ToString(), Accessory.ConvertHexToByteArray(tmpBuffer[l]))) { if (checkBox_hexTerminal.Checked) { outStr = tmpBuffer[l]; } else { outStr = Accessory.ConvertHexToString(tmpBuffer[l]); } if (strDelay > 0) { await TaskEx.Delay(strDelay); } } else //?????????????? { outErr = "String" + l.ToString() + ": Write failure"; } if (SendComing > 1) { l = tmpBuffer.Length; } collectBuffer(outErr, Port1Error); progressBar1.Value = (n * tmpBuffer.Length + l) * 100 / (repeat * tmpBuffer.Length); } } } //byte-by-byte if (radioButton_byByte.Checked) { string tmpStrBuffer = ""; try { tmpStrBuffer = Accessory.CheckHexString(File.ReadAllText(textBox_fileName.Text)); } catch (Exception ex) { MessageBox.Show("Error reading file " + textBox_fileName.Text + ": " + ex.Message); } byte[] tmpBuffer = new byte[tmpStrBuffer.Length / 3]; tmpBuffer = Accessory.ConvertHexToByteArray(tmpStrBuffer); for (int l = 0; l < tmpBuffer.Length; l++) { byte[] outByte = { tmpBuffer[l] }; if (checkBox_hexTerminal.Checked) { outStr = Accessory.ConvertByteArrayToHex(tmpBuffer, tmpBuffer.Length); } else { outStr = Encoding.GetEncoding(RawPrnControl.Properties.Settings.Default.CodePage).GetString(tmpBuffer); } collectBuffer(outStr, Port1DataOut); if (RawPrinterSender.SendRAWToPrinter(comboBox_Printer.SelectedItem.ToString(), outByte)) { progressBar1.Value = (n * tmpBuffer.Length + l) * 100 / (repeat * tmpBuffer.Length); if (strDelay > 0) { await TaskEx.Delay(strDelay); } } else { collectBuffer("Byte " + l.ToString() + ": Write Failure", Port1Error); } if (SendComing > 1) { l = tmpBuffer.Length; } } } //stream else { string tmpStrBuffer = ""; try { tmpStrBuffer = Accessory.CheckHexString(File.ReadAllText(textBox_fileName.Text)); } catch (Exception ex) { MessageBox.Show("Error reading file " + textBox_fileName.Text + ": " + ex.Message); } byte[] tmpBuffer = new byte[tmpStrBuffer.Length / 3]; tmpBuffer = Accessory.ConvertHexToByteArray(tmpStrBuffer); int r = 0; while (r < 10 && !RawPrinterSender.SendRAWToPrinter(comboBox_Printer.SelectedItem.ToString(), tmpBuffer)) { collectBuffer("USB write retry " + r.ToString(), Port1Error); await TaskEx.Delay(100); r++; } if (r >= 10) { outErr = "Block write failure"; } if (checkBox_hexTerminal.Checked) { outStr = Accessory.ConvertByteArrayToHex(tmpBuffer, tmpBuffer.Length); } else { outStr = Encoding.GetEncoding(RawPrnControl.Properties.Settings.Default.CodePage).GetString(tmpBuffer); } if (outErr != "") { collectBuffer(outErr + " start", Port1Error); } collectBuffer(outStr, Port1DataOut); if (outErr != "") { collectBuffer(outErr + " end", Port1Error); } progressBar1.Value = ((n * tmpBuffer.Length) * 100) / (repeat * tmpBuffer.Length); } } } if (repeat > 1) { await TaskEx.Delay(delay); } if (SendComing > 1) { n = repeat; } } button_Send.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; } }