private void sendString(string dataString, bool appendCRLF) { if (dataString.Length == 0) { return; } if (radioButtonASCII.Checked) { if (appendCRLF) { dataString += "\r\n"; } if (checkBoxEcho.Checked) { textBoxDisplay.AppendText(dataString); textBoxDisplay.SelectionStart = textBoxDisplay.Text.Length; textBoxDisplay.ScrollToCaret(); } if (logFile != null) { logFile.Write(dataString); } byte[] unicodeBytes = Encoding.Unicode.GetBytes(dataString); byte[] asciiBytes = Encoding.Convert(Encoding.Unicode, Encoding.ASCII, unicodeBytes); Pk2.DataDownload(asciiBytes, 0, asciiBytes.Length); } else {// hex data int numBytes = 0; if (dataString.Length > (MaxHexLength - 1)) { numBytes = ((MaxHexLength + 1) / 3); } else { numBytes = dataString.Length / 3; dataString = dataString.Substring(0, (numBytes * 3)); } byte[] hexBytes = new byte[numBytes]; for (int i = 0; i < numBytes; i++) { hexBytes[i] = (byte)Utilities.Convert_Value_To_Int("0x" + dataString.Substring((3 * i), 2)); } dataString = "TX: " + dataString + "\r\n"; textBoxDisplay.AppendText(dataString); textBoxDisplay.SelectionStart = textBoxDisplay.Text.Length; textBoxDisplay.ScrollToCaret(); if (logFile != null) { logFile.Write(dataString); } Pk2.DataDownload(hexBytes, 0, hexBytes.Length); } }
private void timer1_Tick_1(object sender, EventArgs e) { if (ResOk == 1) { // textBox1.Text = textBox1.Text + 1; ResOk = 0; if (Nom_Send < 306000) { // Pk2.DataDownload(n, Nom_Send, Nom_Send+1); Pk2.DataDownload(n, Nom_Send, Nom_Send + 33); Nom_Send = Nom_Send + 33; textBox1.Text = Nom_Send.ToString(); } } }
public void OnKeyPress(object sender, KeyPressEventArgs e) { string hexChars = "0123456789ABCDEF"; if (textBoxString1.ContainsFocus | textBoxString2.ContainsFocus | textBoxString3.ContainsFocus | textBoxString4.ContainsFocus) { // ignore typing in textboxes return; } // check for copy/cut if ((e.KeyChar == 3) || (e.KeyChar == 24)) { textBoxDisplay.Copy(); return; } if (radioButtonDisconnect.Checked) { // don't do anything else if not connected return; } textBoxDisplay.Focus(); if (radioButtonHex.Checked) { // hex mode string charTyped = e.KeyChar.ToString(); // get typed char charTyped = charTyped.ToUpper(); if (charTyped.IndexOfAny(hexChars.ToCharArray()) == 0) { // valid Hex character if (labelTypeHex.Visible) { // first nibble already typed - send byte string dataString = labelTypeHex.Text.Substring(11, 1) + charTyped; labelTypeHex.Text = "Type Hex : "; labelTypeHex.Visible = false; byte[] hexByte = new byte[1]; hexByte[0] = (byte)Utilities.Convert_Value_To_Int("0x" + dataString); dataString = "TX: " + dataString + "\r\n"; textBoxDisplay.AppendText(dataString); textBoxDisplay.SelectionStart = textBoxDisplay.Text.Length; textBoxDisplay.ScrollToCaret(); if (logFile != null) { logFile.Write(dataString); } Pk2.DataDownload(hexByte, 0, hexByte.Length); } else { // show first nibble labelTypeHex.Text = "Type Hex : " + charTyped + "_"; labelTypeHex.Visible = true; } } else { // other char - clear typed hex labelTypeHex.Text = "Type Hex : "; labelTypeHex.Visible = false; } } else { // ASCII mode // check for paste if (e.KeyChar == 22) { textBoxDisplay.SelectionStart = textBoxDisplay.Text.Length; //cursor at end TextBox tempBox = new TextBox(); tempBox.Multiline = true; tempBox.Paste(); do { int pasteLength = tempBox.Text.Length; if (pasteLength > 60) { pasteLength = 60; } sendString(tempBox.Text.Substring(0, pasteLength), false); tempBox.Text = tempBox.Text.Substring(pasteLength); // wait according to the baud rate so we don't overflow the download buffer float baud = float.Parse((comboBoxBaud.SelectedItem.ToString())); baud = (1F / baud) * 12F * (float)pasteLength; // to ensure we don't overflow, give each byte 12 bits baud *= 1000F; // baud is now in ms. Thread.Sleep((int)baud); } while (tempBox.Text.Length > 0); tempBox.Dispose(); return; } string charTyped = e.KeyChar.ToString(); if (charTyped == "\r") { charTyped = "\r\n"; } sendString(charTyped, false); } }