motorTest/Model/RobotSocket.cs
2026-10-17 11:39:30 +08:00

195 lines
5.3 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Community;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
namespace Model
{
/// <summary>
/// 通讯类
/// </summary>
public class RobotSocket : NotificationObject
{
#region 通讯套接字
/// <summary>
/// 通讯套接字
/// </summary>
public Socket MySocket { get; set; }
#endregion
#region 通讯连接IP
/// <summary>
/// 通讯连接IP
/// </summary>
private string CommunicationIP { get; set; }
#endregion
#region 通讯连接端口号
/// <summary>
/// 通讯连接端口号
/// </summary>
private int CommunicationPort { get; set; }
#endregion
#region 连接状态
private ShowInfo connStatu;
/// <summary>
/// 连接状态
/// </summary>
public ShowInfo ConnStatu
{
get { return connStatu; }
set
{
connStatu = value;
this.RaisePropertyChanged("ConnStatu");
}
}
#endregion
#region 构造函数
/// <summary>
/// 构造函数
/// </summary>
/// <param name="ip">连接IP</param>
/// <param name="port">端口号</param>
public RobotSocket(string ip, int port)
{
CommunicationIP = ip;
CommunicationPort = port;
ConnStatu = new ShowInfo();
ConnStatu.StatuInfo = "等待连接";
}
#endregion
#region 开启连接
/// <summary>
/// 开启连接
/// </summary>
/// <param name="mySocker">套接字对象</param>
/// <param name="ip">IP地址</param>
/// <param name="port">端口</param>
public void StartConn()
{
try
{
//1.创建监听套接字 使用ip4协议 流式传输 tcp连接
MySocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
// 获取服务器端口
IPAddress address = IPAddress.Parse(CommunicationIP);
IPEndPoint endpoint = new IPEndPoint(address, CommunicationPort);
//开启连接服务器
MySocket.Connect(endpoint);
}
catch
{
}
}
#endregion
#region 关闭连接
/// <summary>
/// 关闭连接
/// </summary>
public void ShutDown()
{
try
{
MySocket.Close(); //关闭套接字
}
catch (Exception)
{
}
}
#endregion
#region 发送消息
byte[] sendbyte { get; set; }
private static object sendLock = new Object();
/// <summary>
/// 发送消息
/// </summary>
public void Send(string strMgs)
{
try
{
lock (sendLock)
{
if (!string.IsNullOrEmpty(strMgs))
{
//发送操作指令
sendbyte = System.Text.Encoding.ASCII.GetBytes(strMgs);
//sendbyte = System.Text.Encoding.Default.GetBytes(strMgs);
if (MySocket != null)
{
MySocket.Send(sendbyte);
}
}
}
}
catch (Exception)
{
}
}
#endregion
#region 接收消息
byte[] Message { get; set; }
int lenght { get; set; }
/// <summary>
/// 接收消息A
/// </summary>
public string ReceivesMessageA()
{
Message = new byte[1024 * 1024 * 1]; //1.1创建消息缓存容器
lenght = MySocket.Receive(Message); //1.2接收的消息并存入缓存区,注意Receive也会阻断当前线程
TheReceives = System.Text.Encoding.ASCII.GetString(Message, 0, lenght);
return TheReceives;
}
/// <summary>
/// 接收消息B
/// </summary>
/// <returns></returns>
public string ReceivesMessageB()
{
Message = new byte[1024 * 1024 * 1]; //1.1创建消息缓存容器
MySocket.Receive(Message); //1.2接收的消息并存入缓存区,注意Receive也会阻断当前线程
TheReceives = System.Text.Encoding.UTF8.GetString(Message);
//TheReceives = Encoding.Default.GetString(Message);
//TheReceives = System.Text.Encoding.ASCII.GetString(Message, 0, lenght);
return TheReceives;
}
#endregion
#region 接收到的信息
private string theReceives;
/// <summary>
/// 接收到的信息
/// </summary>
public string TheReceives
{
get { return theReceives; }
set
{
theReceives = value;
this.RaisePropertyChanged("TheReceives");
}
}
#endregion
}
}