private void tmr_Reconn_Tick(object sender, EventArgs e) { if (Convert.ToInt32(tmr_Reconn.Tag) == 1) { return; } tmr_Reconn.Tag = 1; CLog.SaveLog("Line[" + LineName + "] Reconnect Start!"); m_Printer.StopConnect(); Application.DoEvents(); try { m_Printer.StartConnect(strIP, (Int32)iSN); } catch (Exception ecp) { //连不上 } if (!m_Printer.GetConnected())//!GetConnected(ipPrinter)) { //MessageBox.Show("Connect to printer failed!"); CLog.SaveLog("Line[" + LineName + "] Reconnect failed!"); } else { tmr_Reconn.Enabled = false; tmr_CheckConn.Enabled = true; btn_Connect.Text = "Disconnect"; btn_Connect.Enabled = true; CLog.SaveLog("Line[" + LineName + "] Reconnect OK!"); } tmr_Reconn.Tag = 0; }
private void Form1_Load(object sender, EventArgs e) { if (IsStartListening) { return; } //启动线程打开监听 // StartSockst = new Thread(new ThreadStart(listensocket)); // StartSockst.Start(); ReadSetting(); beginListen(); LineForms = new List <LinePageForm>(); btn_Refresh.PerformClick(); if (m_bAutoNewline) { ReadLineInfo(); } CLog.SaveLog("\r\n----------------------------------"); LB_Ver.Text = "Version:" + Application.ProductVersion.ToString(); //createlistenserver(); }
private void btn_PrintONOFF_Click(object sender, EventArgs e) { bool bStatus = m_Printer.GetIsPrinting(); //GetPrinted(ipPrinter); if (bStatus) { CLog.SaveLog("Line[" + LineName + "] Stop print"); if (m_Printer.StopPrint() == 0) { MessageBox.Show("Stop print message failed."); CLog.SaveLog("Line[" + LineName + "] Stop print failed"); return; } btn_RefreshMsgNames.Enabled = true; btn_PrintONOFF.Text = "Print"; } else { byte[] msgname = new byte[128]; CLog.SaveLog("Line[" + LineName + "] Start print"); string sName = cbobox_MsgList.Text.Substring(0, cbobox_MsgList.Text.Length - 4); if (m_Printer.StartPrint(sName) == 0) { MessageBox.Show("Start print message failed."); CLog.SaveLog("Line[" + LineName + "] Start print failed"); return; } btn_RefreshMsgNames.Enabled = false; btn_PrintONOFF.Text = "Stop"; } }
/* * private void btn_Refresh_Click(object sender, EventArgs e) * { * SaveLog("Refresh, start scan printers"); * * startWait("Scan devices"); * * int handle1 = CreatePrinter(); * Application.DoEvents(); * SetClient(handle1, System.Text.Encoding.ASCII.GetBytes( m_pIPClient), m_iClientCount); * * int count = ScanPrinter(handle1); * * Application.DoEvents(); * StringBuilder pin = new StringBuilder(255 * 20); * GetPrinterInfos(handle1, count ,pin); * String strClient = pin.ToString(); * String[] strClients = null; * Application.DoEvents(); * strClients = strClient.Split(new Char[] { ',' }); * //} * * lstBox_Printers.Items.Clear(); * //lstBox_Printers.DataSource = strClients; * for(int i=0;i<strClients.Length;i++) * lstBox_Printers.Items.Add(strClients[i]); * * DelPrinter(handle1); * StopWait(); * SaveLog("scan end, find [" + count.ToString() + "] printers."); * } */ public LinePageForm newLinePage(string sLineName) { MainTables.TabPages.Add(sLineName); MainTables.SelectTab(MainTables.TabPages.Count - 1); LinePageForm lineform = new LinePageForm(); lineform.FormBorderStyle = FormBorderStyle.None; lineform.Dock = DockStyle.Fill; lineform.TopLevel = false; lineform.Parent = MainTables.SelectedTab; lineform.LineName = sLineName; lineform.mainForm = this; lineform.bNeedAutoConnect = m_bAutoConnect; lineform.bNeedAutoPrint = m_bAutoPrint; lineform.Show(); LineForms.Add(lineform); currLinePage = lineform; createAllLineName(); CLog.SaveLog("Create Line[" + sLineName + "]"); return(lineform); }
private void btn_RenameLine_Click(object sender, EventArgs e) { if (MainTables.SelectedIndex < 0) { return; } string sLineName = MainTables.TabPages[MainTables.SelectedIndex].Text; string sName = Input("Please input the new Product-Line Name:", sLineName); if (sName.Length < 1) { return; } MainTables.TabPages[MainTables.SelectedIndex].Text = sName; LinePageForm lpf = getPage(sLineName); lpf.LineName = sName; createAllLineName(); CLog.SaveLog("Rename line [" + sLineName + "] to [" + sName + "]"); }
private void btn_Refresh_Click(object sender, EventArgs e) { CLog.SaveLog("Refresh, start scan printers"); startWait("Scan devices"); CPrinter cScanPrinter = new CPrinter(); Application.DoEvents(); cScanPrinter.SetIPSection(m_pIPClient); cScanPrinter.sUdpServerIP = m_sServerIp; int count = cScanPrinter.ScanPrinter(); lstBox_Printers.Items.Clear(); if (count > 0) { string[] sPrinters = cScanPrinter.GetPrinterInfos(); Application.DoEvents(); for (int i = 0; i < sPrinters.Length; i++) { lstBox_Printers.Items.Add(sPrinters[i]); } } StopWait(); CLog.SaveLog("scan end, find [" + count.ToString() + "] printers."); }
public void listensocket() { while (true) // 持续不断的监听客户端的连接请求; { // 开始监听客户端连接请求,Accept方法会阻断当前的线程; Socket sokConnection; //IPEndPoint endpoint = new IPEndPoint(IPAddress.Parse(m_sServerIp), ServerPort); //sokConnection.Bind(endpoint); //sokConnection.Listen(10); try { sokConnection = socketWatch.Accept(); } catch (SocketException se) { CLog.SaveLog("客户端关闭!"); CLog.SaveException("---Listen---", se.Message); CLog.SaveLog("-----------------------------"); return; } //Socket sokConnection = socketWatch.Accept(); // 一旦监听到一个客户端的请求,就返回一个与该客户端通信的 套接字; // 将与客户端连接的 套接字 对象添加到集合中; dictSkt.Add(sokConnection.RemoteEndPoint.ToString(), sokConnection); CLog.SaveLog("客户端连接成功![" + sokConnection.RemoteEndPoint.ToString() + "]"); Thread thr = new Thread(RecMsg); thr.IsBackground = true; thr.Start(sokConnection); } }
private void beginListen() { // 创建负责监听的套接字,注意其中的参数; socketWatch = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); // 获得文本框中的IP对象; IPAddress address = IPAddress.Parse(GetLocalIp());//IPAddress.Parse(txtIp.Text.Trim()); // 创建包含ip和端口号的网络节点对象; IPEndPoint endPoint = new IPEndPoint(address, ServerPort); try { // 将负责监听的套接字绑定到唯一的ip和端口上; socketWatch.Bind(endPoint); } catch (SocketException se) { MessageBox.Show("监听绑定异常:" + se.Message); CLog.SaveLog("监听绑定异常:" + se.Message); return; } // 设置监听队列的长度; socketWatch.Listen(10); // 创建负责监听的线程; threadWatch = new Thread(listensocket); threadWatch.IsBackground = true; threadWatch.Start(); CLog.SaveLog("服务器启动监听成功!"); }
private void btn_Connect_Click(object sender, EventArgs e) { // Application.DoEvents(); // SetClient(ipPrinter, System.Text.Encoding.Default.GetBytes(mainForm.m_pIPClient), mainForm.m_iClientCount); bool bStatus = m_Printer.GetConnected();//GetConnected(ipPrinter); if (bStatus) { if (m_Printer.StopConnect() == 0)// StopConnect(ipPrinter) != 0) { MessageBox.Show("Disconnect to printer failed!"); CLog.SaveLog("Line[" + LineName + "] disconnect error!"); } else { tmr_CheckConn.Enabled = false; btn_Connect.Text = "Connect"; CLog.SaveLog("Line[" + LineName + "] disconnected."); } } else { m_Printer.StartConnect(strIP, (Int32)iSN); //StartConnect(ipPrinter, iSN, iIP) ; if (!m_Printer.GetConnected())//!GetConnected(ipPrinter)) { MessageBox.Show("Connect to printer failed!"); CLog.SaveLog("Line[" + LineName + "] connect error!"); } else { btn_Connect.Text = "Disconnect"; CLog.SaveLog("Line[" + LineName + "] connected."); btn_RefreshMsgNames_Click(sender, e); bool bPrintStatus = m_Printer.GetIsPrinting(); //GetPrinted(ipPrinter); if (!bPrintStatus) { btn_RefreshMsgNames.Enabled = true; btn_PrintONOFF.Text = "Print"; } else { btn_RefreshMsgNames.Enabled = false; btn_PrintONOFF.Text = "Stop"; } tmr_CheckConn.Enabled = true; } } }
void RecMsg(object sokConnectionparn) { Socket sokClient = sokConnectionparn as Socket; while (true) { // 定义一个2M的缓存区; byte[] arrMsgRec = new byte[1024 * 1024 * 2]; // 将接受到的数据存入到输入 arrMsgRec中; int length = -1; try { length = sokClient.Receive(arrMsgRec); // 接收数据,并返回数据的长度; } catch (SocketException se) { CLog.SaveLog("异常(Receive Message):" + se.Message); CLog.SaveException("---Receive Message---", se.Message); // 从 通信套接字 集合中删除被中断连接的通信套接字; dictSkt.Remove(sokClient.RemoteEndPoint.ToString()); break; } catch (Exception e) { CLog.SaveLog("异常(Receive Message):" + e.Message); CLog.SaveException("---Receive Message---", e.Message); // 从 通信套接字 集合中删除被中断连接的通信套接字; dictSkt.Remove(sokClient.RemoteEndPoint.ToString()); break; } string strMsg = System.Text.Encoding.ASCII.GetString(arrMsgRec, 0, length); try { DealCMD(strMsg, sokClient); } catch (SocketException se) { CLog.SaveLog("异常(DealCMD):" + se.Message); CLog.SaveException("---DealCMD---", se.Message); } catch (Exception e) { CLog.SaveLog("异常(DealCMD):" + e.Message); CLog.SaveException("---DealCMD---", e.Message); } } }
private void tmr_CheckConn_Tick(object sender, EventArgs e) { bool bc1 = m_Printer.ISConnected1(); bool bc2 = m_Printer.ISConnected2(); if (bc1 == false || bc2 == false) {//断线 //重连 btn_Connect.Text = "ReConnect now..."; btn_Connect.Enabled = false; Application.DoEvents(); CLog.SaveLog("Line[" + LineName + "] disconnected [Client disconnected]."); tmr_Reconn.Enabled = true; tmr_CheckConn.Enabled = false; } }
private void btn_RefreshMsgNames_Click(object sender, EventArgs e) { CLog.SaveLog("Line[" + LineName + "] Refresh Message list."); mainForm.startWait("Refresh message list"); Application.DoEvents(); try { //StringBuilder sbNames = new StringBuilder(255 * 100); //GetMsgNameList(ipPrinter, sbNames); string sNames = m_Printer.GetMsgNameList(); String strNames = sNames;// sbNames.ToString(); String[] strName = null; //for (int i = 0; i < count; i++) //{ strName = strNames.Split(new Char[] { ',' }); cbobox_MsgList.Items.Clear(); int index = 0; while (true) { if (strName[index].Length > 0) { cbobox_MsgList.Items.Add(strName[index]); } else { break; } index++; } } catch (Exception ex) { CLog.SaveException("---Get MessageList---", ex.Message); //iErrCode = Constant.ERR_OBJECT_DISPOSED_EXCEPTION; } if (cbobox_MsgList.Items.Count > 0) { cbobox_MsgList.SelectedIndex = 0; } mainForm.StopWait(); }
private void btn_Assign_Click(object sender, EventArgs e) { if (MainTables.TabCount < 1) { MessageBox.Show("Please create product-line first."); return; } if (lstBox_Printers.Items.Count < 1) { MessageBox.Show("Please refresh devices."); return; } if (lstBox_Printers.SelectedIndex < 0) { lstBox_Printers.SelectedIndex = 0; } String str; str = "Are your sure add "; str += lstBox_Printers.Items[lstBox_Printers.SelectedIndex].ToString(); str += " to ["; str += MainTables.TabPages[MainTables.SelectedIndex].Text; str += "]?"; DialogResult dr; dr = MessageBox.Show(str, "", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1); if (dr == DialogResult.No) { return; } LinePageForm lPage = getPage(MainTables.TabPages[MainTables.SelectedIndex].Text); lPage.setIPstr(lstBox_Printers.Items[lstBox_Printers.SelectedIndex].ToString()); CLog.SaveLog("Add [" + lstBox_Printers.Items[lstBox_Printers.SelectedIndex].ToString() + "] to [" + MainTables.TabPages[MainTables.TabIndex].Text + "]"); //MainTables.TabPages[MainTables.TabIndex]. }
public int SendStrings(string strStrings) { CLog.SaveLog("Line[" + LineName + "] Send Strings[" + strStrings + "]"); //byte[] btsendStrings = System.Text.Encoding.ASCII.GetBytes(strStrings); int irlt = Constant.ERR_ERROR; try { irlt = m_Printer.SendDynamicString(strStrings); if (irlt == Constant.ERR_OK) { DispMsg(strStrings);//rTxtbox_Send.AppendText(strStrings);// .Text = rTxtbox_Send.Text. Insert(strStrings); } }catch (Exception ex) { CLog.SaveException("---Send Dynamic String Exception---", ex.Message); irlt = Constant.ERR_SEND_STRINGS; } return(irlt); }
private void btn_DelLine_Click(object sender, EventArgs e) { if (MainTables.SelectedIndex < 0) { return; } string sLineName = MainTables.TabPages[MainTables.SelectedIndex].Text; DialogResult rlt = MessageBox.Show("Are you sure delete this product-line[" + sLineName + "]?" , "", MessageBoxButtons.YesNo); if (rlt == DialogResult.No) { return; } MainTables.TabPages.Remove(MainTables.TabPages[MainTables.SelectedIndex]); LinePageForm lpf = getPage(sLineName); LineForms.Remove(lpf); if (MainTables.TabPages.Count == 0) { btn_DelLine.Enabled = false; btn_RenameLine.Enabled = false; } else { btn_DelLine.Enabled = true; btn_RenameLine.Enabled = true; } createAllLineName(); CLog.SaveLog("Delete line [" + sLineName + "]"); }
private void frmMain_FormClosed(object sender, FormClosedEventArgs e) { //bCloseListen = true; CLog.SaveLog("-------------Close-----------"); }