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