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) { 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; } }