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); } } }
void Func_ProgramClose() { if (main_com.serialport.IsOpen == true) { //COM_Op.Close(main_com.serialport); bool res = COM_Op.button_COMOpen_Click(main_com.serialport); SetComStatus(res); } fp.TryDeleteDll(); if (fp.is_active == true) { fp.Close(); } if (etcp.is_active == true) { etcp.Close(); } RunEXE.Close(); notifyIcon.Dispose();//释放notifyIcon1的所有资源,以保证托盘图标在程序关闭时立即消失 //后台线程,不需要关闭了 //thread_com_recv.Abort(); //thread_Calx_output.Abort(); //thread_net.Abort(); main_com.thread_txt_update.Abort(); //必须要关闭该线程,否则关闭窗体时会失败 Func_PropertiesSettingsSave(); Dbg.WriteLine("Form Close done"); //System.Environment.Exit(0); //MessageBox.Show("是否关闭KCOM", Func_GetStack("Attention"), MessageBoxButtons.YesNo, MessageBoxIcon.Warning); }
public void DataHandle(byte[] com_recv_buffer, int com_recv_buff_size, bool send_to_tcp) { if (cfg.cmdline_mode == true) //命令行处理时,需要把特殊符号去掉 { cmdline.HandlerRecv(com_recv_buffer, com_recv_buff_size, ref txt.receive); Update_TextBox(txt.receive, tyShowOp.EQUAL); } string SerialIn = ""; //把接收到的数据转换为字符串放在这里 if (cfg.ascii_rcv == false) //十六进制接收,则需要转换为ASCII显示 { for (int i = 0; i < com_recv_buff_size; i++) { SerialIn += Func.GetHexHighLow(com_recv_buffer[i], 0); SerialIn += Func.GetHexHighLow(com_recv_buffer[i], 1) + " "; } } else { if (com_recv_buffer[com_recv_buff_size - 1] == 0x00) //发现接收数据的最后一个字符经常会是0x00,过滤掉 { com_recv_buff_size--; } if (com_recv_buff_size == 0) { return; } if (com_recv_buffer[com_recv_buff_size - 1] == '\r') //最后一个是'\r'也要去掉,否则textbox容易崩 { com_recv_buff_size--; } if (com_recv_buff_size == 0) { return; } int i; for (i = 0; i < com_recv_buff_size; i++) { byte current_byte = com_recv_buffer[i]; if ((SCOM.run_ecmd == true) && (RunEXE.bytes_run_exe_code.Length != 0)) { if (current_byte == RunEXE.bytes_run_exe_code[RunEXE.run_exe_code_cmp]) { RunEXE.run_exe_code_cmp++; RunEXE.run_exe_code_equal++; } else { RunEXE.run_exe_code_cmp = 0; RunEXE.run_exe_code_equal = 0; } if (RunEXE.run_exe_code_equal == RunEXE.bytes_run_exe_code.Length) { RunEXE.Run_EXE(); RunEXE.run_exe_code_cmp = 0; RunEXE.run_exe_code_equal = 0; } } if ((current_byte == '\n') && (last_byte != '\r')) //只有'\n'没有'\r',则追加进去 { byte add_byte = (byte)'\r'; SerialIn += Func.Byte_To_String(add_byte); //System.Text.Encoding.ASCII.GetString(arrayx) } last_byte = current_byte; if ((current_byte == 0x00) || (current_byte > 0x7F)) //收到非ASCII码要显示一下 { if (cfg.fliter_ileagal_char == false) { SerialIn += " ~"; SerialIn += Func.GetHexHighLow(current_byte, 0); SerialIn += Func.GetHexHighLow(current_byte, 1); SerialIn += "~ "; } } else { if (Properties.Settings.Default._add_Time == 0) //不加时间戳 { //SerialIn = System.Text.Encoding.ASCII.GetString(com_recv_buffer, 0, com_recv_buff_size); //SerialIn += System.Text.Encoding.ASCII.GetString(array); SerialIn += Func.Byte_To_String(current_byte); } else if (Properties.Settings.Default._add_Time == 1) //时间戳加在前面 { if (recv_need_add_time == true) { recv_need_add_time = false; SerialIn += "[" + DateTime.Now.ToString("yy/MM/dd HH:mm:ss.fff") + "]"; } //SerialIn += System.Text.Encoding.ASCII.GetString(array); SerialIn += Func.Byte_To_String(current_byte); if (com_recv_buffer[i] == '\n') { recv_need_add_time = true; } } else //时间戳加在后面 { //SerialIn += System.Text.Encoding.ASCII.GetString(array); SerialIn += Func.Byte_To_String(current_byte); if (com_recv_buffer[i] == '\n') { SerialIn = SerialIn.Substring(0, SerialIn.Length - 2);//把'\r'和'\n'去掉 SerialIn += "["; SerialIn += DateTime.Now.ToString("yy/MM/dd HH:mm:ss.fff"); SerialIn += "]\r\n"; } } } } } if (log_file_name != null) { StreamWriter sw_log_file = File.AppendText(log_file_name); //在目标文件原有内容后面追加内容 string TimeShow; int currentYear = DateTime.Now.Year; int currentMonth = DateTime.Now.Month; int currentDay = DateTime.Now.Day; int currentHour = DateTime.Now.Hour; int currentMinute = DateTime.Now.Minute; int currentSecond = DateTime.Now.Second; if (Properties.Settings.Default._add_Time == 0) { if (currentMinute != LastLogFileTime) { TimeShow = "\r\n\r\n--------------------------" + "_Y" + currentYear.ToString() + "_M" + currentMonth.ToString() + "_D" + currentDay.ToString() + "_H" + currentHour.ToString() + "_M" + currentMinute.ToString() + "_S" + currentSecond.ToString() + "--------------------------\r\n\r\n"; sw_log_file.Write(TimeShow); LastLogFileTime = currentMinute; } } sw_log_file.Write(SerialIn); sw_log_file.Flush(); //清空缓冲区 sw_log_file.Close(); //关闭关键 } if (SerialIn.Length > 0) { record.rcv_bytes += (uint)SerialIn.Length; if (cfg.cursor_fixed == true) { txt.temp += SerialIn; } else { txt.receive += SerialIn; //在接收文本中添加串口接收数据 if (cfg.limiet_rcv_lenght == true) //限定接收文本的长度,防止logfile接收太多东西,KCOM死掉 { if (txt.receive.Length >= MAX_RECV_TEXT_LENGTH) { #if false //回滚式地限定长度 textBox_ComRec.Text = _func.String_Roll(textBox_ComRec.Text, MAX_RECV_TEXT_LENGTH); #elif false //直接清空 textBox_ComRec.Text = "[KCOM: reset the recv text!]\r\n"; #else //放到垃圾桶上 txt.backup = txt.receive; txt.receive = "[KCOM: reset the recv text!]\r\n"; Update_TextBox(txt.receive, tyShowOp.EQUAL); #endif } } Update_TextBox(SerialIn, tyShowOp.ADD); } } if ((SerialIn.Length > 0) && (fm.etcp.is_active == true) && (send_to_tcp == true)) { fm.etcp.SendData(Encoding.ASCII.GetBytes(SerialIn)); //串口接收到的数据,发送给网络端 } }
private void button_RunEXE_Click(object sender, EventArgs e) { RunEXE.Run_EXE(); }
/***************************FastPrint END**************************/ /***************************Run_EXE START**************************/ private void button_SelectEXE_Click(object sender, EventArgs e) { RunEXE.SetDefaultExePath(button_SelectEXE); }
void FormMain_Load(object sender, EventArgs e) //窗体加载函数 { /*************恢复参数 Start****************/ int _parameter1 = Properties.Settings.Default._parameter1; checkBox_Color.Checked = Parameter.GetBoolFromParameter(_parameter1, Parameter._BitShift_anti_color); checkBox_LimitRecLen.Checked = Parameter.GetBoolFromParameter(_parameter1, Parameter._BitShift_max_recv_length); checkBox_Cmdline.Checked = Parameter.GetBoolFromParameter(_parameter1, Parameter._BitShift_cmdline_chk); checkBox_Backgroup.Checked = Parameter.GetBoolFromParameter(_parameter1, Parameter._BitShift_run_in_backgroup); checkBox_ClearRecvWhenFastSave.Checked = Parameter.GetBoolFromParameter(_parameter1, Parameter._BitShift_clear_data_when_fastsave); checkBox_Fliter.Checked = Parameter.GetBoolFromParameter(_parameter1, Parameter._BitShift_messy_code_fliter); checkBox_MidMouseClear.Checked = Parameter.GetBoolFromParameter(_parameter1, Parameter._BitShift_middle_mouse_clear); checkBox_esc_clear_data.Checked = Parameter.GetBoolFromParameter(_parameter1, Parameter._BitShift_esc_clear); checkBox_ASCII_Rcv.Checked = Parameter.GetBoolFromParameter(_parameter1, Parameter._BitShift_ascii_receive); checkBox_ASCII_Snd.Checked = Parameter.GetBoolFromParameter(_parameter1, Parameter._BitShift_ascii_send); etcp.is_server = Parameter.GetBoolFromParameter(_parameter1, Parameter._BitShift_netcom_is_server); textBox_custom_baudrate.Text = Properties.Settings.Default.user_baudrate; button_FastSavePath.Text = "Fast save path: " + Properties.Settings.Default.fastsave_path; Func_Set_AddTime_Color(); TextFont_Change(); /*************恢复参数 End******************/ Dbg.Init(textBox_Message); /*************网络初始化 Start****************/ label_ShowIP.Text = etcp.ShowLocalIP(); etcp.Init(); Func_NetCom_ChangeFont(etcp.is_server); /*************网络初始化 End******************/ /*************串口初始化 Start****************/ COM_Op.ControlModule_Init(comboBox_COMNumber, comboBox_COMBaudrate, comboBox_COMCheckBit, comboBox_COMDataBit, comboBox_COMStopBit, 0, main_com.serialport); Form_COM_Init(); Form_SCOM_Init(); /*************串口初始化 End******************/ /*************FastPrint start****************/ fp.Init(Properties.Settings.Default.fp_hex0_path, Properties.Settings.Default.fp_hex1_path); /*************FastPrint End******************/ /****************eCMD start******************/ RunEXE.Init(textBox_RunExeCode, button_SelectEXE); /****************eCMD End********************/ string current_com_str = ""; if (comboBox_COMNumber.SelectedIndex != -1) { current_com_str = comboBox_COMNumber.SelectedItem.ToString(); } Set_Form_Text("", current_com_str); FormMain_SizeChanged(null, null); }