 
 
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;using System.IO;using System.Net;using System.Net.Sockets;using System.Data.SqlClient;using System.IO.Ports;using csLTDMC;using System.Threading;using WindowsFormsApplication2;namespace 视觉{  public partial class main : Form  {    public main()    {      InitializeComponent();    }    private ushort _CardID = 0;    axis axis = new axis();    yundong axis_x;    yundong axis_y;    yundong axis_z;    yundong axis_folder;    yundong axis_move1;    yundong axis_move2;    public string time;    //传输 ip地址与端口号    private IPEndPoint ServerInfo;    private Socket ClientSocket;    string[] protName = SerialPort.GetPortNames();    //信息接收缓存    private Byte[] MsgBuffer;    //信息发送存储    private Byte[] MsgSend;    Thread Up_piece;    private void Form1_Load(object sender, EventArgs e)    {      #region 初始化窗体      pnlOPContainer.Controls.Clear();//移除所有控件      you frmAddUser = new you();      frmAddUser.TopLevel = false;      frmAddUser.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;      pnlOPContainer.Controls.Add(frmAddUser);      frmAddUser.Show();      main form1 = new main();      main main = new main();      lei.sizi sizi = new lei.sizi();      sizi.controllInitializeSize(main);      sizi.controlAutoSize(main);      //  sizi.controllInitializeSize(frmAddUser);      // sizi.controlAutoSize(frmAddUser);      #endregion      #region 初始化      axis.ka();      axis.qigang(false, 4);      axis.qigang(false, 5);      try      { //定义一个IPV4,TCP模式的Socket        ClientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);        MsgBuffer = new Byte[65535];        MsgSend = new Byte[65535];        //允许子线程刷新数据        //服务端IP和端口信息设定,这里的IP可以是127.0.0.1,可以是本机局域网IP,也可以是本机网络IP        ServerInfo = new IPEndPoint(IPAddress.Parse("192.168.2.201"), Convert.ToInt32("6666"));        //客户端连接服务端指定IP端口,Sockket        Thread.Sleep(5);        ClientSocket.Connect(ServerInfo);        //将用户登录信息发送至服务器,由此可以让其他客户端获知        //  ClientSocket.Send(Encoding.ASCII.GetBytes("ccd61"));        // Thread.Sleep(5);        //开始从连接的Socket异步读取数据。接收来自服务器,其他客户端转发来的信息        //AsyncCallback引用在异步操作完成时调用的回调方法        //    ClientSocket.Receive(MsgBuffer, 0, SocketFlags.None);      }      catch (Exception u)      {        throw u;      }      #endregion      try      {        #region 设置        axis_x = new yundong(_CardID, 0, 0, 1, 1, 1, 0, 0);//打码轴         axis_y = new yundong(_CardID, 1, 0, 1, 1, 1, 0, 0);//横轴        axis_z = new yundong(_CardID, 5, 0, 0, 0, 1, 0, 0); //旋转轴        axis_move1 = new yundong(_CardID, 3, 0, 0, 1, 0, 0, 0);//履带        axis_move2 = new yundong(_CardID, 4, 0, 0, 1, 0, 0, 0);//履带        axis_folder = new yundong(_CardID, 2, 0, 0, 1, 0, 0, 0);//加料轴        axis_x.SetReset(0, 20000, 0, 1);        axis_y.SetReset(0, 20000, 0, 1);        axis_z.SetReset(0, 5000, 1000, 0);        axis_folder.SetReset(0, 3000, 0, 1);        //axis_move1.SetReset(0, 6000, 1000, 0);        //axis_move2.SetReset(0, 6000, 1000, 0);        axis_x.SetRunpara(0.1, 0.1, 1000, 1000, 30000, 0.1);        axis_y.SetRunpara(0.1, 0.1, 1000, 1000, 30000, 0.1);        axis_z.SetRunpara(0.1, 0.1, 500, 500, 2000, 0.1);        axis_move1.SetRunpara(0.1, 0.1, 1000, 1000, 4000, 0.1);        axis_move2.SetRunpara(0.1, 0.1, 1000, 1000, 4000, 0.1);        axis_folder.SetRunpara(0.1, 0.1, 1000, 1000, 5000, 0.1);        #endregion      }      catch (Exception u)      {        throw u;      }    }    private void dingsheng_Click(object sender, EventArgs e)    {    }    private void label3_Click_1(object sender, EventArgs e)    {      if (this.label3.ImageIndex == 1)      {        this.label3.ImageIndex = 0;        LTDMC.dmc_write_sevon_pin(_CardID, 0, 0);//使能 卡号 轴号 低电平        LTDMC.dmc_write_sevon_pin(_CardID, 1, 0);//使能        LTDMC.dmc_write_sevon_pin(_CardID, 2, 0);//使能        LTDMC.dmc_write_sevon_pin(_CardID, 5, 0);//使能        MessageBox.Show("启动使能", "提示");        yundong.shineng = true;      }      else if (this.label3.ImageIndex == 0)      {        this.label3.ImageIndex = 1;        LTDMC.dmc_write_sevon_pin(_CardID, 0, 1);//使能 卡号 轴号 低电平        LTDMC.dmc_write_sevon_pin(_CardID, 1, 1);//使能        LTDMC.dmc_write_sevon_pin(_CardID, 2, 1);//使能        LTDMC.dmc_write_sevon_pin(_CardID, 5, 1);//使能        // MessageBox.Show("关闭使能", "提示", MessageBoxButtons.OK);        yundong.shineng = false;      }    }    private void 急停ToolStripMenuItem_Click(object sender, EventArgs e)    {      LTDMC.dmc_emg_stop(_CardID);    }    private void 复位ToolStripMenuItem_Click(object sender, EventArgs e)    {      LTDMC.dmc_write_outbit(0, 4, 1);      LTDMC.dmc_write_outbit(0, 5, 1);      axis_x.fuwei();      axis_y.fuwei();      axis_z.fuwei();    }    private void pnlOPContainer_Paint(object sender, PaintEventArgs e)    {    }    private void 启动ToolStripMenuItem_Click(object sender, EventArgs e)    {      MsgBuffer = new Byte[65535];      MsgSend = new Byte[65535];      string a = DateTime.Now.ToLongDateString();      string str1 = a;      int i = 0, length = 2;      str1 = str1.Remove(i, length);      string str2 = str1;      string[] strArray = str2.Split('年');      time = strArray[0]  ".";      a = DateTime.Now.ToLongDateString();      str1 = a;      i = 0;      length = 5;      str1 = str1.Remove(i, length);      str2 = str1;      strArray = str2.Split('月');      time = strArray[0]  ".";      a = DateTime.Now.ToLongDateString();      str1 = a;      i = 0;      length = 8;      str1 = str1.Remove(i, length);      str2 = str1;      strArray = str2.Split('日');      if (int.Parse(strArray[0].ToString()) < 10)      {        strArray[0] = "0"  strArray[0];      }      time = strArray[0];      DBHelper.ExcuteSql("delete from a");      DBHelper.ExcuteSql("delete from b");      Up_piece = new Thread(new ThreadStart(yun));//运动线程      Up_piece.Start();    }    public int zhuangtai;    public void yun()    {      while (true)      {        zhuangtai = 0;        if (csLTDMC.LTDMC.dmc_read_inbit(0, 0) == 0)        {          axis_move1.Vmove(1);          axis_move2.Vmove(1);          // axis.My_check_io_1(_CardID, 1);          while (true)          {            Thread.Sleep(10);            if (0 == LTDMC.dmc_read_inbit(_CardID, 1))      //传感器触发时输出的电平为低电平 单个传感器感应            {              break;            }            Application.DoEvents();          }          axis.qigang(true, 4);          Thread.Sleep(1000);          axis_move1.Vmovestop(3);          axis_move1.Vmovestop(4);          axis.qigang(true, 5);          zhou();          if (zhuangtai==1)          {            break;          }          axis.qigang(false, 5);          axis.qigang(false, 4);          axis_move1.Vmove(1);          axis_move2.Vmove(1);          //  axis.My_check_io_1(_CardID, 2);          while (true)          {            Thread.Sleep(10);            if (0 == LTDMC.dmc_read_inbit(_CardID, 2))      //传感器触发时输出的电平为低电平 单个传感器感应            {              break;            }            Application.DoEvents();          }          axis_move1.Vmovestop(3);          axis_move1.Vmovestop(4);          ushort[] Axis = { 0, 1, 5 };          int[] printstart = { 0, 0, 0 };          DataTable text = DBHelper.GetDataTableBySql("select * from canshu where moban='2'");          printstart[0] = int.Parse(text.Rows[0]["pz_x_1"].ToString());          printstart[1] = int.Parse(text.Rows[0]["pz_y_1"].ToString());          axis.Mulit_Position(Axis, printstart, 40000);   }      }    }    public int a = 0;    public string shuju;    public void zhou()    {      ushort[] Axis = { 0, 1, 5 };      int[] printstart = { 0, 0, 0 }; //打印起始点      int[] printstop = { 0, 0, 0 }; //打印结束点      int[] print = { 0, 0, 0 };   //      int[] print2 = { 0, 0, 0 };      int[] print3 = { 0, 0, 0 };      int[] home = { 0, 0, 0 };      int[] pz = { 0, 0, 0 };      //验证码      if (a > 6)      {        a = 0;      }      DataTable text1 = DBHelper.GetDataTableBySql("select id,ma from ma");      string ma = text1.Rows[a][1].ToString();      a ;      //if (a >= 1)      //{      //  DataTable text1 = DBHelper.GetDataTableBySql("select id from ma where ma='"  shuju  "'");      //  string ma1 = text1.Rows[0][0].ToString();      //  int ma2 = int.Parse(ma1)  1;      //  DataTable text2 = DBHelper.GetDataTableBySql("select ma from ma where id='"  ma2  "'");      //  string ma3 = text2.Rows[0][0].ToString();      //  if (ma3 == ma)      //  {      //  }      //  else      //  {      //    ma = ma3;      //  }      //}      //      DataTable text = DBHelper.GetDataTableBySql("select * from canshu where moban='2'");      pz[0] = int.Parse(text.Rows[0]["pz_x_1"].ToString());      pz[1] = int.Parse(text.Rows[0]["pz_y_1"].ToString());      //      axis.Mulit_Position(Axis, pz, 30000);      //2.发指令      decimal pzx = (decimal)pz[0] / 1000;      decimal pzy = (decimal)pz[1] / 1000;      decimal pzz = (decimal)pz[2] / 1000;      MsgSend = Encoding.ASCII.GetBytes("ccd61,"  pzx  ","  pzy);      Thread.Sleep(500);      ClientSocket.Send(MsgSend);           pz[0] = int.Parse(text.Rows[0]["pz_x_2"].ToString());      pz[1] = int.Parse(text.Rows[0]["pz_y_2"].ToString());      axis.Mulit_Position(Axis, pz, 30000);      pzx = (decimal)pz[0] / 1000;      pzy = (decimal)pz[1] / 1000;      pzz = (decimal)pz[2] / 1000;      MsgSend = Encoding.ASCII.GetBytes("ccd62,"  pzx  ","  pzy);      ClientSocket.Send(MsgSend);      Thread.Sleep(50);      MsgSend = Encoding.ASCII.GetBytes("ex99,"  "122.618,"  "106.276");      ClientSocket.Send(MsgSend);      ClientSocket.Receive(MsgBuffer, MsgBuffer.Length, SocketFlags.None);      byte[] byteArray = MsgBuffer;      string str = System.Text.Encoding.ASCII.GetString(byteArray);      string[] b = str.Split(',');      if (b[0] == "20")      {        MessageBox.Show("模板未识别");        LTDMC.dmc_emg_stop(_CardID);        axis.qigang(false, 4);        axis.qigang(false, 5);        axis_x.MyReset();        axis_y.MyReset();        axis_z.MyReset();        zhuangtai = 1;        return ;      }      MessageBox.Show(b[0]  "-"  b[1]  "-"  b[2]  "-"  b[3]);      // MessageBox.Show(b[1]);      // MessageBox.Show(b[2]);      // MessageBox.Show(b[3]);      double x1 = double.Parse(b[1]);      double y1 = double.Parse(b[2]);      double z1 = double.Parse(b[3]);      x1 = x1 * 1000;      y1 = y1 * 1000;      int x = (Int32)x1;      int y = (Int32)y1;      int chuankou = CsServial_Q560.PrintText(ma, serialPort1, protName[0]);//发送数据//COM3      // shuju = ma;      LTDMC.dmc_write_outbit(_CardID, 2, 1);      LTDMC.dmc_compare_clear_points(_CardID, 0); //清除所有一维比较点      getposition(ref printstart, ref print, ref printstop, x, y, z1);      int pos = print[0];      LTDMC.dmc_compare_add_point(0, 0, pos, 1, 8, 2);//触发结束位置 卡 轴 触发点 0:小于等于 1:大于等于  100ms脉冲 指定io      LTDMC.dmc_compare_set_config(_CardID, 0, 0, 0);//print_style 为0-1 xy 轴分别为2 3   卡号 轴号 使能0:禁止  1:使能 触发源      axis.Mulit_Position(Axis, printstart, 40000);//暂定运行速度为x轴速度//移动到打印起始点      csLTDMC.LTDMC.dmc_compare_set_config(0, 0, 1, 0); //触发使能  xy 轴分别为2 3   卡号 轴号 使能0:禁止  1:使能 触发源      axis.Mulit_Position(Axis, printstop, 100000);//打印       csLTDMC.LTDMC.dmc_compare_set_config(0, 0, 0, 0); //触发使能  xy 轴分别为2 3   卡号 轴号 使能0:禁止  1:使能 触发源      axis.Mulit_Position(Axis, printstart, 40000);    }    public int id;    public int pz_x_1;    public int pz_x_2;    public int pz_y_1;    public int pz_y_2;    public int position_x;    public int position_y;    public int position_x2;    public int position_y2;    public int position_x3;    public int position_y3;    public int added_x;    public int added_y;    public int angle;    public int speed;    public void getposition(ref int[] printstart, ref int[] print, ref int[] printstop, int rex, int rey, double reangle)    {      double z = 0 - 60000 / 360 * reangle;      // if (z%1>0.5)      // {      //   z ;      // }      DataTable text = DBHelper.GetDataTableBySql("select * from canshu where moban='2'");      position_x = int.Parse(text.Rows[0]["position_x"].ToString());      position_y = int.Parse(text.Rows[0]["position_y"].ToString());      print[0] = position_x  rex;      print[1] = position_y  rey;      print[2] = (int)z;      //print[2] = (int)((point.angle  reangle) / 90 * Config.Axis_Z_90_Pulse);      //printstart[0] = (int)Math.Cos(point.angle  reangle) * Config.Print_Length_add  print[0];      //printstart[1] = (int)Math.Sin(point.angle  reangle) * Config.Print_Length_add  print[1];      //printstart[2] = print[2];      //printstop[0] = 0 - (int)Math.Cos(point.angle  reangle) * Config.Print_Length_add  print[0];      //printstop[1] = 0 - (int)Math.Sin(point.angle  reangle) * Config.Print_Length_add  print[1];      //printstop[2] = print[2];      printstart[0] = print[0] - 60000;      printstart[1] = print[1];      printstart[2] = print[2];      printstop[0] = print[0]  100000;      printstop[1] = print[1];      printstop[2] = print[2];    }    private void 停止工作ToolStripMenuItem_Click(object sender, EventArgs e)    {    }    private void 位置清零ToolStripMenuItem_Click(object sender, EventArgs e)    {      LTDMC.dmc_set_position(_CardID, 0, 0);      LTDMC.dmc_set_position(_CardID, 1, 0);      LTDMC.dmc_set_position(_CardID, 5, 0);    }    private void 退出程序ToolStripMenuItem_Click(object sender, EventArgs e)    {      if (LTDMC.dmc_check_done(_CardID, 0) == 0 || LTDMC.dmc_check_done(_CardID, 1) == 0 || LTDMC.dmc_check_done(_CardID, 5) == 0)      {        MessageBox.Show("工作未停止");      }      Application.Exit();    }  }}

 
  
					
				
评论