using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Text;
|
using System.Threading;
|
using System.Windows.Forms;
|
using System.Net;
|
using System.Net.Sockets;
|
using DevComponents.DotNetBar;
|
|
namespace WCS
|
{
|
/// <summary>
|
/// 条码线程
|
/// </summary>
|
class BarcodeThread
|
{
|
#region 画面上同步显示控件
|
public ListBox barcodedisplay;
|
public LabelX lb_barcode;
|
delegate void displayresponse(string text);
|
delegate void SetTextCallback(string barcode);
|
#endregion
|
|
Thread trd = null;
|
string barcodeip = "";
|
int barcodeno = 0;
|
int portno = 0;
|
|
//int i = 1;
|
//int scaleno = 0;
|
main form1;
|
|
private IPEndPoint ServerInfo;
|
private Socket socket;
|
private Byte[] MsgBuffer = new Byte[65535];
|
private Byte[] MsgSend = new Byte[65535];
|
|
public BarcodeThread(int barcode_no, string barcode_ip, int port, main f)
|
{
|
try
|
{
|
barcodeno = barcode_no;
|
barcodeip = barcode_ip;
|
this.portno = port;
|
form1 = f;
|
//MsgBuffer = new Byte[65535];
|
//MsgSend = new Byte[65535];
|
|
trd = new Thread(new ThreadStart(this.ThreadTask));
|
trd.IsBackground = true;
|
trd.Start();
|
}
|
catch (Exception em)
|
{
|
Common.WriteLogFile("WcsError", "BarcodeThread/BarcodeThread--条码线程启动出错:" + em.Message);
|
}
|
}
|
|
#region socket连接
|
/// <summary>
|
/// socket连接WMS服务器
|
/// </summary>
|
private void SocketConnect()
|
{
|
ServerInfo = new IPEndPoint(IPAddress.Parse(barcodeip), portno);
|
try
|
{
|
socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
socket.Connect(ServerInfo);
|
//Int32 bytes = socket.Receive(MsgBuffer, MsgBuffer.Length, SocketFlags.None);
|
//StringBuilder buff = new StringBuilder();
|
//String receiveData = Encoding.UTF8.GetString(MsgBuffer, 0, bytes);
|
//socket.Send(Encoding.Unicode.GetBytes("socket connect!"));
|
//RecevieData = "s";
|
//socket.BeginReceive(MsgBuffer, 0, MsgBuffer.Length, 0, new AsyncCallback(ReceiveCallBack), null);
|
|
DisplayInfo("【" + DateTime.Now.ToString() + "】读取条码数据...");
|
|
}
|
catch (Exception em)
|
{
|
DisplayInfo("【" + DateTime.Now.ToString() + "】条码连接失败!" + em.Message);
|
Common.WriteLogFile("WcsError", "BarcodeThread/SocketConnect--条码连接失败:" + em.Message);
|
}
|
}
|
|
/// <summary>
|
/// socket重新连接WMS服务器
|
/// </summary>
|
private void SocketReConnect()
|
{
|
if (socket.Connected)
|
{
|
socket.Close();
|
}
|
Thread.Sleep(100);
|
SocketConnect();
|
}
|
|
/// <summary>
|
/// 判断socket是否已连接
|
/// </summary>
|
/// <returns></returns>
|
private bool checkSocket()
|
{
|
bool ret = true;
|
bool isConnected = false;
|
bool isRead = false;
|
bool isWrite = false;
|
bool isError = false;
|
int avalSize = 0;
|
try
|
{
|
isConnected = socket.Connected;
|
isRead = socket.Poll(50, SelectMode.SelectRead);
|
avalSize = socket.Available;
|
isWrite = socket.Poll(50, SelectMode.SelectWrite);
|
isError = socket.Poll(50, SelectMode.SelectError);
|
if (isConnected == false || isError == true || (isRead == true && avalSize == 0))
|
{
|
ret = false;
|
}
|
}
|
catch (Exception em)
|
{
|
Common.WriteLogFile("WcsError", "BarcodeThread/checkSocket--判断条码是否连接失败:" + em.Message);
|
ret = false;
|
}
|
return ret;
|
}
|
#endregion
|
|
/// <summary>
|
/// 获取条码扫描器数据并解析
|
/// </summary>
|
private void GetBarcodeData()
|
{
|
string dataValue = "P", s_RecStr = "";
|
double d_RecStr = 0;
|
try
|
{
|
if (checkSocket() == false)
|
{
|
//调用重连
|
//Common.gs_barcode_data[barcodeno - 1] = "";
|
//DisplayInfo("【" + DateTime.Now.ToString() + "】条码连接已断开,重连...");
|
SocketReConnect();
|
return;
|
}
|
|
MsgSend = Encoding.UTF8.GetBytes(dataValue);
|
if (socket.Connected)
|
{
|
// 向主机发送数据.
|
socket.Send(MsgSend, MsgSend.Length, SocketFlags.None);
|
//socket.Send(MsgSend);
|
//DisplayInfo("发送信息:" + dataValue);
|
//RecevieData = "";
|
// 接收主机应答
|
socket.ReceiveTimeout = 60000;
|
Int32 bytes = socket.Receive(MsgBuffer, MsgBuffer.Length, SocketFlags.None);
|
StringBuilder buff = new StringBuilder();
|
if (bytes > 0)
|
{
|
// 将缓冲的字节数组,装换为字符串.
|
// String receiveData = Encoding.ASCII.GetString(MsgBuffer, 0, bytes);
|
s_RecStr = Encoding.ASCII.GetString(MsgBuffer, 0, bytes);
|
s_RecStr = s_RecStr.Trim('');
|
if (s_RecStr.IndexOf("NOREAD") >= 0)// || (s_RecStr.Length < 7 && s_RecStr.Length > 0))
|
{
|
//Common.gs_barcode_data[barcodeno - 1] = "";
|
DisplayInfo("【" + DateTime.Now.ToString() + "】条码返回信息:" + s_RecStr);
|
return;
|
}
|
if (s_RecStr.Length < 7)
|
{
|
return;
|
}
|
s_RecStr = s_RecStr.Substring(0, 7);
|
Common.gs_barcode_data[barcodeno - 1] = s_RecStr;
|
//DisplayBarcode(Common.gs_barcode_data[barcodeno - 1]);
|
DisplayInfo("【" + DateTime.Now.ToString() + "】" + Common.gs_barcode_data[barcodeno - 1]);
|
//DisplayInfo("收到消息:" + receiveData);
|
// AnalysisReceive(receiveData); //解析收到的数据
|
// 加入字符串缓存
|
//buff.Append(str);
|
// 再次接受,看看后面还有没有数据.
|
}
|
}
|
else
|
{
|
//Common.gs_barcode_data[barcodeno - 1] = "";
|
DisplayInfo("【" + DateTime.Now.ToString() + "】条码连接已断开,无法读取数据!");
|
}
|
return;
|
}
|
catch(Exception em)
|
{
|
Common.WriteLogFile("WcsError", "BarcodeThread/GetBarcodeData--读取条码数据失败:" + em.Message);
|
//Common.gs_barcode_data[barcodeno - 1] = "";
|
//DisplayInfo("【" + DateTime.Now.ToString() + "】扫描超时:" + t.Message);
|
//Thread.Sleep(400);
|
return;
|
}
|
}
|
|
#region 通讯反馈同步显示
|
/// <summary>
|
/// 显示条码通讯信息
|
/// </summary>
|
/// <param name="text"></param>
|
private void DisplayInfo(string text)
|
{
|
if (barcodedisplay == null)
|
{
|
return;
|
}
|
if (barcodedisplay.InvokeRequired)
|
{
|
try
|
{
|
displayresponse d = new displayresponse(DisplayInfo);
|
barcodedisplay.Invoke(d, new object[] { text });
|
}
|
catch (Exception em)
|
{
|
Common.WriteLogFile("WcsError", "BarcodeThread/DisplayInfo--同步显示条码通讯信息失败:" + em.Message);
|
}
|
}
|
else
|
{
|
if (barcodedisplay.Items.Count > 12)
|
{
|
barcodedisplay.Items.Clear();
|
}
|
barcodedisplay.Items.Add(text);
|
}
|
|
}
|
|
/// <summary>
|
/// 显示条码号
|
/// </summary>
|
/// <param name="text"></param>
|
private void DisplayBarcode(string barcode)
|
{
|
if (lb_barcode == null)
|
{
|
return;
|
}
|
if (lb_barcode.InvokeRequired)
|
{
|
try
|
{
|
SetTextCallback d = new SetTextCallback(DisplayBarcode);
|
lb_barcode.Invoke(d, new object[] { barcode });
|
}
|
catch (Exception em)
|
{
|
Common.WriteLogFile("WcsError", "BarcodeThread/DisplayBarcode--同步显示条码号失败:" + em.Message);
|
}
|
}
|
else
|
{
|
lb_barcode.Text = barcode;
|
}
|
|
}
|
#endregion
|
|
/// <summary>
|
/// 条码主线程
|
/// </summary>
|
private void ThreadTask()
|
{
|
SocketConnect();
|
while (true)
|
{
|
GetBarcodeData();
|
Thread.Sleep(Common.ci_BarcodetimeInterval);
|
}
|
}
|
|
}
|
}
|