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;
        }
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
用于TCP,串口代码调试让您爱不释手的专业串口调试软件,使用完全免费! 本软件可以在Win95/98、Win2000、WinNT、WinXP下面运行.软件功能主要为: 1.接收从串口进来的数据并在窗口显示. 2.所接收到的数据数据显示方式可以选择为字符方式或者HEX方式 4.中文显示无乱码,且不影响速度 5.串口波特率可以选择为110bps-256000bps.(波特率>115200时需要硬件支持) 6.可以即时显示存在的串口号.如果您增加了usb转串口等设备,串口号也会在列表中出现. 7.可以选择“5、6、7、8”四种数据长度. 8.可以选择为“1、1.5、2”三种停止位.(1.5停止位需要硬件支持) 8.第9位数据可以选择为“无、奇校验、偶校验、1、0”四种方式. 9.可以选择“无流控、软流控、硬流控、自定义”四种流控方式. 10.串口设置和字符串操作等设置在程序关闭时自动保存,打开时自动载入. 11.可以在接收窗口按键即发送该键值. 12.可以在字符串输入框输入您想发送的字符串,并发送. 13.可以在字符串输入框输入您想发送的HEX数据串,数据的值从00到FF,没有任何限制. 14.可以定时重复发送数据,并可以设置发送时间间隔. 15.可以在发送字符串时选择发送新行,即自动加上回车换行. 16.可以显示当前串口的CTS、DSR、RLSL(CD)信号线的状态. 17.可以自由控制当前串口的DTR、RTS信号线的输出状态. 18.可以打开一个文本文件或者一个二进制文件预览其内容,查看方式可以是文本或者HEX方式. 19.可以打开一个文本文件或者一个二进制文件并以当前波特率发送到串口. 20.可以保存窗口内容到一个文本文件,文件名取自当前时间,保存在当前目录. 21.可以即时显示发送的字节数和接收到的字节数,按清除窗口将会清零. 22.带有功能强大的扩展功能:多条字符串发送定义和网上查找串口资料等. 23.可以定义最多32条预备发送的字符串,每条字符串可以定义为HEX数据串或者字符串方式.在每一条数据的左边打勾就表示这是一条hex数据串. 24.点击字符串右边的标号即可以发送这条定义好的字符串. 25.可以设置为循环发送你定义过的多条字符串,并且可以设置发送时间间隔. 26.在串口资料栏您可以从mcu51网站或者Google查找有关串口技术资料. 27.在串口资料栏您可以进入技术讨论bbs,在此发表您的高见或者提出您的问题和需求. 28.在产品信息栏您可以获得现时最新的产品信息. 29.这是个绿色软件,单个文件即可执行,不会给您的机器增加任何负担. 此版本使用C++Builder编写,相对于上一版本SSCOM2.0,主要改进在: 1.程序更稳定可靠,修改了一些报错信息.使用更加人性化。 2.修改了避免显示汉字乱码的算法,快了许多. 3.hex数据输入的错误兼容性. 4.发送字符串可以加发回车换行. 5.可以保存窗口内容到文件. 6.发送和接收的字符数统计更准确. 7.不再接收到一定数量字符数就清屏,因为发现即使收到很多内容也不会溢出,速度仍然很快. 8,可以打开二进制文件并发送,从前只能发文本文件. 9.打开文件后可以用asc方式或者hex方式预览文件中前4K内容. 10.发送文件前告诉操作者需要发送多少时间.免得久等. 11.新增功能强大的扩展功能,多达32条自定义字符串操作,程序关闭时这些字符串会自动保存,下次开机时再载入,每条可以定义为HEX数据串或者ASC字符串,按后边的数字按钮可以发送.也可以自动循环发送定义过的字符串. 12.新增串口设置自动保存. 13.加入了网络支持功能,用户很方便讨论问题和找到技术支持. 另外要说明的是,我学习C++Builder才一个星期时间,仓促完成这个串口软件的升级,未免可能有些差错,在此先声明一下,您可以免费使用和自由传播本软件,但是我不对本软件所造成的任何损失负责!如果你使用了本软件,即表明您愿意接收这一条款。如果你不能接受,请立即将其删除! SSCOM系列版本发布以来,拷贝达10几万份,网友们纷纷来信提出各种建议,在此谢谢大家对我的支持!欢迎继续提出您的建议,我将会在下一版本里改进!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值