TCP客户端,解析Log文件,操作第三方软件点击某按钮,模拟鼠标左键按下 

代码功能:TCP客户端,解析Log文件,操作第三方软件点击某按钮,模拟鼠标左键按下 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Runtime.InteropServices;

namespace TestingMachineSoftware
{
    public partial class Form1 : Form
    {
         enum MouseEventFlag : uint
        {
            Move = 0x0001,
            LeftDown = 0x0002,
            LeftUp = 0x0004,
            RightDown = 0x0008,
            RightUp = 0x0010,
            MiddleDown = 0x0020,
            MiddleUp = 0x0040,
            XDown = 0x0080,
            XUp = 0x0100,
            Wheel = 0x0800,
            VirtualDesk = 0x4000,
            Absolute = 0x8000
        }
        [DllImport("User32.dll", EntryPoint = "SendMessage")]
        private static extern int SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, string lParam);
        [DllImport("user32.dll")]
        static extern bool SetCursorPos(int X,int Y);
        [DllImport("user32.dll")]
        static extern void mouse_event(MouseEventFlag flags,int dx,int dy,uint data,UIntPtr extraInfo);

        public Form1()
        {
            InitializeComponent();
            //Form form2 = new Form2();
            //form2.Show();
        }
        Socket socketSend;
        Thread clientListenThread = null;
        private void SelectResultAreaBtn_Click(object sender, EventArgs e)
        {
        }
          //模拟鼠标点击  (x,y)是要点击的位置 
        public static void MouseLeftClickEvent(int dx, int dy, uint data)
        {
            SetCursorPos(dx, dy);
            //System.Threading.Thread.Sleep(2 * 1000);
            mouse_event(MouseEventFlag.LeftDown, dx, dy, data, UIntPtr.Zero);
            mouse_event(MouseEventFlag.LeftUp, dx, dy, data, UIntPtr.Zero);
        }

        struct FileFormatMsg
        {
            string name;
            string pcbCode;
            string result;
        };
        string pathDebug = "E:\\项目资料\\软件log";
        private void SetLogFilePathBtn_Click(object sender, EventArgs e)
        {
            //folderBrowserDialog1.ShowDialog();
            string pathOfLogFile = folderBrowserDialog1.SelectedPath;
            
            //遍历这个路径下的所有文件,把文件名放到列表,把文件挨个打开,挨个查找
            DirectoryInfo root = new DirectoryInfo(pathDebug);
            FileInfo[] files = root.GetFiles();
            int fileNumber = files.Count();
            dataGridView1.Rows.Add(fileNumber);
            for (int i = 0; i < fileNumber; i++)
            {
                dataGridView1.Rows[i].Cells[0].Value = i + 1;
                dataGridView1.Rows[i].Cells[1].Value = files[i].Name;
            }
        }
        private string FindPCBCodeInAFile(string filePath)
        {
            //打开文件
            //创建 StreamReader 类的实例
            byte[] buffer = new byte[4096];
            char[] charBuffer = new char[4096];
            string lineString = null;
            string pcbString = null; 
            int lens = 0;
            FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read, 4096);
            StreamReader streamReader = new StreamReader(fileStream);
            while (true)
            {
                lineString = streamReader.ReadLine();
                if (lineString == null)
                    break;
                //在每一行中查找PCBASER: 后面的就是PCB码
                if (lineString.Contains("RESULT"))
                {
                    int pcbStartPosition = lineString.IndexOf("STATUS_");
                    //截取Position后面的字符串,就是PCB
                    pcbString = lineString.Substring(pcbStartPosition + 7);
                        //return pcbString;//此处是返回第个发现的PCB,要继续修改
                    break;
                }
                lens++;
            }
            return pcbString;
        }
        private void SearchDataBtn_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < dataGridView1.RowCount-1; i++)
            {
                string path = pathDebug + "\\" + dataGridView1.Rows[i].Cells[1].Value;
                dataGridView1.Rows[i].Cells[2].Value = FindPCBCodeInAFile(path);
                dataGridView1.Rows[i].Cells[3].Value = "OK";
                if (i == 10)
                {
                    int bbb = 0;
                }
            }
        }

        private void connectBtn_Click(object sender, EventArgs e)
        {
            if (connectBtn.Text == "断开")//关闭按钮按下
            {
                socketSend.Close();
                connectBtn.BackColor = Color.Red;
                connectBtn.Text = "连接";
                listBoxLog.Items.Add("已断开连接主控电脑...");
                sendMsgToServerBtn.Enabled = false;
                //销毁线程
                try
                {
                    //clientListenThread.Abort();
                }
                catch (ThreadAbortException ex)
                {

                }
                return;
            }
            string clientIP = clientIPTextBox.Text;
            try
            {
                System.Int32 clientPort = int.Parse(clientPortTextBox.Text);
                //创建负责通信的socket
                socketSend = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                IPAddress ip = IPAddress.Parse(clientIP);
                IPEndPoint point = new IPEndPoint(ip, clientPort);
                //获得要连接的远程服务器的IP的端口号
                socketSend.Connect(point);

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                listBoxLog.Items.Add("连接主控电脑失败!");
                return;
            }
            listBoxLog.Items.Add("连接主控电脑成功!");
            connectBtn.Text = "断开";
            connectBtn.BackColor = Color.Green;
            sendMsgToServerBtn.Enabled = true;
            //创建一个监听服务器消息的线程
            clientListenThread = new Thread(listenThreadFunc);
            clientListenThread.IsBackground = true;
            clientListenThread.Start();
        }
        /// <summary>
        /// 客户端给服务器发送消息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void sendMsgToServerBtn_Click(object sender, EventArgs e)
        {
            
            string str = this.textBox1.Text.Trim();//发送textBox1里面的内容
            byte[] buffer = System.Text.Encoding.UTF8.GetBytes(str);
            socketSend.Send(buffer);
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            socketSend.Close();
        }
        /// <summary>
        /// 监听线程函数,接受来自服务器的信息
        /// </summary>
        private void listenThreadFunc()
        {
            while (true)
            {
                byte[] buffer = new byte[1024 * 1024 * 3];
                //实际收到的有效字节数
                int r = socketSend.Receive(buffer);
                if (r == 0)
                {
                    break;
                }
                string s = Encoding.UTF8.GetString(buffer, 0, r);
                listBoxLog.Items.Add(socketSend.RemoteEndPoint + ":" + s);
                //收到START就点击
                if(s.Contains("START"))
                {
                    MouseLeftClickEvent(1260, 750, 0);//这个坐标就是软件的start按钮
                }
            }
           
                

            
        }

        private void ReadCallBack(IAsyncResult ar)
        {
            
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Control.CheckForIllegalCrossThreadCalls = false;
        }
    }
}

 

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页