using System;
|
using System.Collections.Generic;
|
using System.IO.Ports;
|
using System.Linq;
|
using System.Net.Sockets;
|
using System.Text;
|
using System.Threading;
|
using System.Threading.Tasks;
|
using XImageSystem.Service.Exceptions;
|
using XImaging.Automation.Library.HxDriverLib;
|
|
namespace HxDriverService.Runtime.Peripheral
|
{
|
public class ConveyerControl
|
{
|
/// <summary>
|
/// 网络是否连接
|
/// </summary>
|
private bool m_bIsNetConnect = false;
|
/// <summary>
|
/// 接收线程
|
/// </summary>
|
private Thread tRevTcp;
|
/// <summary>
|
/// 读写输入输出的流
|
/// </summary>
|
private NetworkStream m_nsClient;
|
/// <summary>
|
/// 客户端
|
/// </summary>
|
private TcpClient m_tcpClient;
|
/// <summary>
|
/// ip地址
|
/// </summary>
|
private string m_strIPAddress = "192.168.0.7";
|
/// <summary>
|
/// 端口号
|
/// </summary>
|
private int m_nPortNumber = 2022;
|
/// <summary>
|
/// 当前命令
|
/// </summary>
|
public string m_strCommand = "";
|
/// <summary>
|
/// 是否做完(error或正常做完皆认为做完)
|
/// </summary>
|
private bool m_bDone;
|
|
public ConveyerControl(string strIP, int nPort)
|
{
|
m_strIPAddress = strIP;
|
m_nPortNumber = nPort;
|
}
|
|
public bool Start()
|
{
|
try
|
{
|
if (!m_bIsNetConnect)
|
{
|
m_tcpClient = new TcpClient();
|
m_tcpClient.Connect(m_strIPAddress, m_nPortNumber);
|
m_bIsNetConnect = true;
|
m_nsClient = m_tcpClient.GetStream();
|
|
LogConstant.logger.Print("Connect to conveyer");
|
|
tRevTcp = new Thread(RevTcp);
|
tRevTcp.IsBackground = true;
|
tRevTcp.Start();
|
}
|
}
|
catch (Exception ex)
|
{
|
throw new ConveyerException(ex.Message);
|
}
|
return m_bIsNetConnect && (m_tcpClient != null);
|
}
|
|
public void Close()
|
{
|
if (m_tcpClient != null)
|
{
|
m_tcpClient.Close();
|
m_tcpClient = null;
|
}
|
m_bIsNetConnect = false;
|
LogConstant.logger.Print("Disconnected to the conveyer");
|
}
|
|
public bool SendCommand(string strMsg)
|
{
|
if (m_nsClient == null)
|
{
|
return false;
|
}
|
try
|
{
|
LogConstant.logger.Print("SendCommand: " + strMsg);
|
byte[] dataSend = Encoding.ASCII.GetBytes(strMsg);
|
m_nsClient.Write(dataSend, 0, dataSend.Length);
|
return true;
|
}
|
catch (Exception ex)
|
{
|
Close();
|
LogConstant.logger.Print("Send:" + strMsg + " failed");
|
return false;
|
}
|
}
|
|
public void RevTcp()
|
{
|
while (true)
|
{
|
if (!m_bIsNetConnect) break;
|
if (m_tcpClient == null || m_nsClient == null) break;
|
|
byte[] byteRev = new byte[1024];
|
int byteRead = m_nsClient.Read(byteRev, 0, 1024);
|
byte[] byText = new byte[byteRead];
|
Array.Copy(byteRev, 0, byText, 0, byteRead);
|
|
string strMsg = Encoding.ASCII.GetString(byText);
|
LogConstant.logger.Print("Received resp:" + strMsg);
|
if (strMsg.Contains("ERROR"))
|
{
|
m_bDone = true;
|
throw new ConveyerException("Command: " + m_strCommand + " failed");
|
}
|
else if (strMsg == m_strCommand)
|
{
|
m_bDone = true;
|
}
|
}
|
|
//避免接收不全
|
Thread.Sleep(200);
|
}
|
|
public void STOP()
|
{
|
m_strCommand = "STOP\r\n";
|
try
|
{
|
SendCommand(m_strCommand);
|
//等待做完
|
while (!m_bDone)
|
{
|
Thread.Sleep(500);
|
}
|
//置回false
|
m_bDone = false;
|
}
|
catch (Exception ex)
|
{
|
throw new ConveyerException(ex.Message);
|
}
|
}
|
public void HOME()
|
{
|
m_strCommand = "HOME\r\n";
|
try
|
{
|
SendCommand(m_strCommand);
|
//等待做完
|
while (!m_bDone)
|
{
|
Thread.Sleep(500);
|
}
|
//置回false
|
m_bDone = false;
|
}
|
catch (Exception ex)
|
{
|
//置回false
|
m_bDone = false;
|
throw new ConveyerException(ex.Message);
|
}
|
}
|
|
public void STEPS(int x = 329500)
|
{
|
m_strCommand = "STEPS" + x.ToString() + "\r\n";
|
try
|
{
|
SendCommand(m_strCommand);
|
//等待做完
|
while (!m_bDone)
|
{
|
Thread.Sleep(500);
|
}
|
//置回false
|
m_bDone = false;
|
}
|
catch (Exception ex)
|
{
|
//置回false
|
m_bDone = false;
|
throw new ConveyerException(ex.Message);
|
}
|
}
|
}
|
}
|