public void HandleKeyData(SerialPort com, Keys keyData) { Key_To_ASCII(keyData); Dbg.WriteLine(false, "SEND>>"); while (true) { if (Console_FIFO_Chk() == true) { Byte ascii_code = Console_FIFO_Output(); Dbg.WriteLine(false, "{0:X}", ascii_code); if (ascii_code != 0) { string str = ""; str += (char)ascii_code; try { com.Write(str); } catch (Exception ex) { MessageBox.Show(ex.Message + Dbg.GetStack(), "Warning!"); } } } else { break; } } }
static public bool Open(SerialPort sp) { Dbg.WriteLine("PortName:{0}", sp.PortName); Dbg.WriteLine("Baudrate:{0}", sp.BaudRate); Dbg.WriteLine("Parity:{0}", sp.Parity); Dbg.WriteLine("Data:{0}", sp.DataBits); Dbg.WriteLine("Stop:{0}", sp.StopBits); if ((sp.PortName == "null") || (sp.BaudRate == 1) || (sp.Parity == Parity.Space) || (sp.DataBits == 1)) { MessageBox.Show("Please choose the COM port" + Dbg.GetStack(), "Attention!"); return(false); } try { sp.Open(); sp.DiscardInBuffer(); sp.DiscardOutBuffer(); } catch (Exception ex) { //DebugIF.Assert(false, "###TODO: Why can not open COM " + ex.Message); MessageBox.Show(ex.Message + Dbg.GetStack(), "Attention!"); return(false); } return(true); }
void button_Cal_Click(object sender, EventArgs e) { int i; textBox_Console.Text = ""; long one = 1; try { //UInt32 ans = Convert.ToUInt32(textBox_bit.Text); //输入十进制 UInt32 ans = Convert.ToUInt32(textBox_bit.Text, 16); //输入十六 textBox_Console.Text += "DEC:" + ans.ToString() + "\r\n"; for (i = 0; i < 32; i++) { if (((one << i) & ans) != 0) { if (i < 10) { textBox_Console.Text += "0" + i.ToString() + ":1"; } else { textBox_Console.Text += "" + i.ToString() + ":1"; } } else { if (i < 10) { textBox_Console.Text += "0" + i.ToString() + ":0"; } else { textBox_Console.Text += "" + i.ToString() + ":0"; } } if ((i % 4) == 3) { if (i != 31) { textBox_Console.Text += "\r\n"; } } else { textBox_Console.Text += " "; } } } catch { MessageBox.Show("Input error" + Dbg.GetStack(), "Error!"); } }
void RcvData_Handle(byte[] buffer, int length) { string str = System.Text.Encoding.ASCII.GetString(sync_rcv_buffer, 0, sync_rcv_count); Dbg.WriteLine("SyncRcv handle. Len:% Str:%\n", sync_rcv_count, str); string append_str = ""; append_str += "[" + DateTime.Now.ToString("yy/MM/dd HH:mm:ss.fff") + "]"; append_str += "SyncCOM Rcv: " + sync_rcv_count.ToString() + "\r\n"; append_str += str; append_str += "\r\n\r\n"; Update_TextBox(append_str, COM.tyShowOp.APPEND); if (SCOM.run_ecmd == false) { if (main_port.IsOpen == true) //通过串口1透传出去 { try { main_port.Write(sync_rcv_buffer, 0, sync_rcv_count); } catch (Exception ex) { MessageBox.Show(ex.Message + Dbg.GetStack(), "#Sync send data error!"); } } } else if (str == RunEXE.str_run_exe_code) { RunEXE.Run_EXE(); } else { string ext_name = Func.Get_ExternName(str); if ((ext_name != null) && (Func.Char_String_compare(ext_name.ToCharArray(), "cmd", 3) == true)) { string str_cmd = Func.Get_FileName(str); Dbg.WriteLine("ext name:%", ext_name); Dbg.WriteLine("file name:%", str_cmd); Update_TextBox("Run CMD\r\n", COM.tyShowOp.APPEND); str = RunEXE.RunCMD(str_cmd); Update_TextBox(str, COM.tyShowOp.APPEND); } } }
bool Console_FIFO_Input(Byte code) { if (console_key_input - console_key_output < CONSOLE_KEY_FIFO_MAX) { consoke_key_fifo[console_key_input] = code; console_key_input++; return(true); } else { MessageBox.Show("FIFO is full" + Dbg.GetStack(), "Warning!"); return(false); } }
Byte Console_FIFO_Output() { Byte KEY; if (console_key_input - console_key_output > 0) { KEY = consoke_key_fifo[console_key_output]; console_key_output++; return(KEY); } else { MessageBox.Show("FIFO is empty" + Dbg.GetStack(), "Warning!"); return(0xFF); } }
static public void comboBox_COMBaudrate_SelectedIndexChanged(ComboBox _comboBox_COMBaudrate, SerialPort sp) { Update_SerialBaudrate(sp, _comboBox_COMBaudrate); //在串口运行的时候更改波特率,串口关闭时候修改的时候直接在按钮函数里改就行了 if (sp.IsOpen == true) { try { sp.Close(); sp.Open(); } catch (Exception ex) { MessageBox.Show("Can't re-open the COM port " + ex.Message + Dbg.GetStack(), "Attention!"); } } }
static public void Run_EXE() { if (File.Exists(@RunEXE.str_default_exe_path) == false) { MessageBox.Show("Invalid default exe or name" + Dbg.GetStack(), "ERROR"); } else { //MessageBox.Show("Run exe:" + Properties.Settings.Default._default_exe, "Information"); System.Diagnostics.ProcessStartInfo pinfo = new System.Diagnostics.ProcessStartInfo(); pinfo.UseShellExecute = true; pinfo.FileName = RunEXE.str_default_exe_path; //启动进程 System.Diagnostics.Process p = System.Diagnostics.Process.Start(pinfo); } }
void button_FastSave_Click(object sender, EventArgs e) { if (File.Exists(@Properties.Settings.Default.fastsave_path) == false) { MessageBox.Show("Invalid FastSave path or name" + Dbg.GetStack(), "ERROR"); return; } DialogResult messageResult; SaveFileDialog Savefile = new SaveFileDialog(); //定义新的文件保存位置控件 Savefile.FileName = Properties.Settings.Default.fastsave_path; while (true) { messageResult = DialogResult.OK; try { StreamWriter sw_fast_save = File.CreateText(Savefile.FileName); sw_fast_save.Write(textBox_ComRec.Text); //写入文本框中的内容 sw_fast_save.Flush(); //清空缓冲区 sw_fast_save.Close(); //关闭关键 } catch (Exception ex) //RetryCancel { messageResult = MessageBox.Show(ex.Message + Dbg.GetStack(), "ERROR", MessageBoxButtons.RetryCancel); } if (messageResult != DialogResult.Retry) { break; } } timer_ColorShow.Enabled = true; button_FastSave.BackColor = Color.Yellow; if (checkBox_ClearRecvWhenFastSave.Checked == true) { main_com.ClearRec(); } }
public void Send() { if (fm.etcp.is_active == true) //网络连接上了 { fm.etcp.SendData(Encoding.ASCII.GetBytes(txt.send)); return; } const uint max_recv_length = 65535; if (txt.send.Length == 0) { MessageBox.Show("Please input data" + Dbg.GetStack(), "Warning!"); return; } if (txt.send.Length > max_recv_length) { MessageBox.Show("Data too long" + Dbg.GetStack(), "Warning!"); return; } if (cfg.ascii_snd == true) //ASCII发送 { try { //string ss = textBox_COM.Text.Trim(); serialport.Write(txt.send); record.snd_bytes += (uint)txt.send.Length; } catch (Exception ex) { MessageBox.Show(ex.Message + Dbg.GetStack(), "Warning!"); } } else//16进制发送 { //转换16进制的string成byte[] byte[] bb = new byte[max_recv_length]; string com_send_text = txt.send; int length_bb = 0; com_send_text += " "; int n = com_send_text.Length; char[] chahArray = new char[n]; chahArray = com_send_text.ToCharArray();//将字符串转换为字符数组 /* 需要检查输入的合法性 */ for (int i = 0; i < n; i++)//搜索是否有非法字符 { if ((chahArray[i] != '0') && (chahArray[i] != '1') && (chahArray[i] != '2') && (chahArray[i] != '3') && (chahArray[i] != '4') && (chahArray[i] != '5') && (chahArray[i] != '6') && (chahArray[i] != '7') && (chahArray[i] != '8') && (chahArray[i] != '9') && (chahArray[i] != 'A') && (chahArray[i] != 'B') && (chahArray[i] != 'C') && (chahArray[i] != 'D') && (chahArray[i] != 'E') && (chahArray[i] != 'F') && (chahArray[i] != 'a') && (chahArray[i] != 'b') && (chahArray[i] != 'c') && (chahArray[i] != 'd') && (chahArray[i] != 'e') && (chahArray[i] != 'f') && (chahArray[i] != ' ')) { MessageBox.Show("Error input format!" + Dbg.GetStack(), "Warning!"); return; } } /* 需要检查输入的合法性 */ for (int i = 2; i < n; i++) //找出所有空格 { //0x3F if (chahArray[i] == ' ') { uint hex_h = Func.CharToByte(chahArray[i - 2]); //3 uint hex_l = Func.CharToByte(chahArray[i - 1]); //F byte hex = (byte)(((uint)hex_h) << 4 | hex_l); if (hex_h == 0xFF || hex_l == 0xFF) { continue; } bb[length_bb] = hex; length_bb++; } } try { serialport.Write(bb, 0, length_bb); record.snd_bytes += (uint)length_bb; } catch (Exception ex) { MessageBox.Show(ex.Message + Dbg.GetStack(), "Warning!"); } } }
unsafe public bool Start() { string cpu0_hex_path = hex0_path; string cpu1_hex_path = hex1_path; if ((File.Exists(@cpu0_hex_path) == false) || (File.Exists(@cpu1_hex_path) == false)) { MessageBox.Show("HEX not exist! " + cpu0_hex_path + " " + cpu1_hex_path + Dbg.GetStack(), "Warning!"); return(false); } FileInfo fi = new FileInfo(cpu0_hex_path); last_hex_time = fi.LastWriteTime; //记录上一次打开HEX的时间 Dbg.WriteLine("First. Create:" + fi.CreationTime.ToString() + " Write:" + fi.LastWriteTime + " Access:" + fi.LastAccessTime); char[] pwd_hex0 = hex0_path.ToCharArray(); char[] pwd_hex1 = hex1_path.ToCharArray(); Dbg.WriteLine("pwd_hex0:{0}", hex0_path); Dbg.WriteLine("pwd_hex1:{0}", hex1_path); //将内嵌的资源释放到临时目录下 if (File.Exists(fast_printf_dll_address) == false) { FileStream str = new FileStream(fast_printf_dll_address, FileMode.OpenOrCreate); str.Write(Properties.Resources.FastPrintf, 0, Properties.Resources.FastPrintf.Length); str.Close(); } mi_fa = new memory_desc(); mi_fb = new memory_desc(); mi_fa.pBuff = (byte *)Marshal.AllocHGlobal(MEMORY_MAX_BANK * MEMORY_BANK_SIZE); mi_fb.pBuff = (byte *)Marshal.AllocHGlobal(MEMORY_MAX_BANK * MEMORY_BANK_SIZE); mi_fa.bank = (memory_bank *)Marshal.AllocHGlobal(sizeof(memory_bank) * MEMORY_MAX_BANK); mi_fb.bank = (memory_bank *)Marshal.AllocHGlobal(sizeof(memory_bank) * MEMORY_MAX_BANK); hex2bin_mount(ref mi_fa); hex2bin_mount(ref mi_fb); if (hex2bin_read_hex(pwd_hex0, ref mi_fa) == 0) { MessageBox.Show("###open hex0 fail!", "Error!"); Close(); return(false); } if (hex2bin_read_hex(pwd_hex1, ref mi_fb) == 0) { MessageBox.Show("###open hex1 fail!", "Error!"); return(false); } input_data = (byte *)Marshal.AllocHGlobal(1024 * 1024); output_data = (byte *)Marshal.AllocHGlobal(1024 * 1024); is_active = true; return(true); }