上位机状态机开发之连接机械手 1 连接机械手 /// <summary> /// 连接机械手 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void materialButton1_Click ( object sender, EventArgs e) { // 连接机械手服务端 if ( CommunicateService. Instance. ConnectServer ( ) ) { lb_status. Text= "已连接" ; lb_status. BackColor= Color. Green; } else { lb_status. Text= "连接失败" ; lb_status. BackColor= Color. Red; } } 2.CommunicateService using System ; using System. Collections. Generic ; using System. Linq ; using System. Net. Http ; using System. Text ; using System. Threading. Tasks ; namespace 上位机程序{ public class CommunicateService { //单例模式: //1. 私有静态变量(在第四步供外界使用),创建类的实例 //2. 私有构造函数,确保外部无法直接实例化(确保是单个实例) //3. 确定供外界调用的代码资源 //4. 公开静态属性,供外界使用(把第一步类的实例,开放出去) //5. 外界使用 //1. 私有静态变量(在第四步供外界使用),创建类的实例 private static CommunicateService instance= new CommunicateService ( ) ; //2. 私有构造函数,确保外部无法直接实例化(确保是单个实例) private CommunicateService ( ) { } //4. 公开静态属性,供外界使用(把第一步类的实例,开放出去) public static CommunicateService Instance{ get { return instance; } } //3. 确定供外界调用的代码资源 HttpClient httpClient= new HttpClient ( "192.168.1.109" , 5001 ) ; HttpClient httpClient2= new HttpClient ( "127.0.0.1" , 8080 ) ; HttpClient httpClient3= new HttpClient ( "127.0.0.1" , 7930 ) ; /// <summary> /// 连接服务端 /// </summary> /// <returns></returns> public bool ConnectServer ( ) { if ( httpClient. Connect ( ) ) { return true ; } return false ; } public bool ConnectServer2 ( ) { if ( httpClient2. Connect ( ) ) { return true ; } return false ; } public bool ConnectServer3 ( ) { if ( httpClient3. Connect ( ) ) { return true ; } return false ; } /// <summary> /// 发送消息 /// </summary> /// <param name="Content"></param> public void Send2 ( string Content) { httpClient2. SendMsg ( Content) ; } /// <summary> /// 发送消息 /// </summary> /// <param name="Content"></param> public void Send ( string Content) { httpClient. SendMsg ( Content) ; } /// <summary> /// 发送消息 /// </summary> /// <param name="Content"></param> public void Send3 ( string Content) { httpClient3. SendMsg ( Content) ; } } } 3.HttpClient using System ; using System. Collections. Generic ; using System. Linq ; using System. Net ; using System. Net. Sockets ; using System. Text ; using System. Threading. Tasks ; using System. Windows. Forms ; namespace 上位机程序{ public class HttpClient { // 1:创建通信套接字。 public Socket SocketClient; #region 属性 /// <summary> /// /// </summary> public string Ip{ get ; set ; } = "127.0.0.1" ; /// <summary> /// 端口 /// </summary> public int Port{ get ; set ; } = 8080 ; #endregion /// <summary> /// 构造器(对IP和端口进行赋值) /// </summary> public HttpClient ( string ip, int port) { Ip= ip; Port= port; } /// <summary> /// 连接服务端 /// </summary> public bool Connect ( ) { // 1:创建通信套接字。(确定为因特网,流式数据,tcp通讯) SocketClient= new Socket ( AddressFamily. InterNetwork, SocketType. Stream, ProtocolType. Tcp) ; // 2.设置服务器的IP和端口号。(this,表示当前窗口,可以不加,加了更安全) IPEndPoint ip_port= new IPEndPoint ( IPAddress. Parse ( Ip) , Port) ; // 3. 连接 try { SocketClient. Connect ( ip_port) ; } catch ( Exception ex) { MessageBox. Show ( "连接服务器失败:" + ex. Message) ; return false ; } // 5:接受数据。 //创建一个一直运行的监听的线程 Task. Run ( new Action ( ( ) => { ReceiveMsg ( ) ; } ) ) ; return true ; } /// <summary> /// 以utf-8接受数据 /// </summary> private void ReceiveMsg ( ) { while ( true ) { try { // 创建一个缓冲区 byte [ ] buffer= new byte [ 1024 * 1024 * 10 ] ; // 数据长度 int length= - 1 ; // 第四步:调用读写函数发送或者接收数据。 try { // 接受数据 length= SocketClient. Receive ( buffer) ; } catch ( Exception ) { break ; } // 如果数据大于0,则解析 if ( length> 0 ) { string msg= string . Empty; msg= Encoding. UTF8. GetString ( buffer, 0 , length) ; // 如果收到机械手执行结束命令,就将结束标志位设为1 if ( msg. Contains ( "Finish" ) ) { GlobalParameters. FinishFlag= 1 ; } else if ( msg. Contains ( "OK" ) ) { GlobalParameters. SnapImageFlag= 1 ; GlobalParameters. SnapImageFlag2= 1 ; } else if ( msg. Contains ( "NG" ) ) { GlobalParameters. SnapImageFlag= 2 ; GlobalParameters. SnapImageFlag2= 2 ; } else if ( msg. Contains ( "Move" ) ) { string [ ] xy= msg. Split ( ',' ) ; GlobalParameters. X= Convert. ToDouble ( xy[ 1 ] ) ; GlobalParameters. Y= Convert. ToDouble ( xy[ 2 ] ) ; } // 报错,因为跨线程,跨UI // rtb_Receive.AppendText(msg); } } catch ( Exception ex) { } } } /// <summary> /// 发送数据utf8 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public void SendMsg ( string Content) { string content= Content. Replace ( "\\n" , "\n" ) ; byte [ ] sendMsg= Encoding. Default. GetBytes ( content) ; // 发送数据 SocketClient?. Send ( sendMsg) ; } } } 4.GlobalParameters using System ; using System. Collections. Generic ; using System. Linq ; using System. Text ; using System. Threading. Tasks ; namespace 上位机程序{ public class GlobalParameters { public static int FinishFlag= 0 ; public static int SnapImageFlag= 0 ; public static int SnapImageFlag2= 0 ; // 移动的X坐标 public static double X= 100 ; // 移动的Y坐标 public static double Y= 100 ; } }