using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.IO.Ports;
using System.Diagnostics;
using System.Threading;
using System.Runtime.InteropServices;
using Rfid245ApiLib;
using System.Windows.Forms;
namespace _2._4G
{
public partial class Form2 : Form
{
hComSocket CS; //???????(connect handle struct)
int hCom = -1; //????(comm handle)
Socket sockClient = null; //????(TCP/IP handle)
int ConnectFlag = 0; //?????(connect falg)
int Language = 0;
int ResetFlag = 0;
RFID_StandardProtocol rfid_sp = new RFID_StandardProtocol();
int DisplayCnt = 0;
int Displayindex = 0;
/// <summary>
/// memcmp API
/// </summary>
/// <param name="b1">????1</param>
/// <param name="b2">????2</param>
/// <returns>???????????0?????1????2?????0???????1????2?????0???</returns>
[DllImport("msvcrt.dll")]
unsafe private static extern int memcmp(void* b1, void* b2, int count);
[DllImport("msvcrt.dll")]
unsafe private static extern void* memcpy(void* dest, void* src, int count);
[DllImport("msvcrt.dll")]
unsafe private static extern void* malloc(int size);
[DllImport("msvcrt.dll")]
unsafe private static extern void* calloc(int num, int size);
[DllImport("msvcrt.dll")]
unsafe private static extern void free(void* memblock);
public Form2()
{
InitializeComponent();
}
private void QUERYINTERNET_button_Click(object sender, EventArgs e)
{
QueryInternet(255);
}
private void ByteToDecimalstr(ref string ToStr, byte[] Buf)
{
for (int i = 0; i < 4; i++)
{
ToStr = ToStr + IntToInt_Str((int)Buf[i]);
if (3 != i)
ToStr = ToStr + ".";
}
}
private string IntToInt_Str(int value)
{
string str = string.Format("{0:D}", value);
return str;
}
private int QueryInternet(int ReaderNum)
{
string TempStr = "";
string TempStrEnglish = "";
string IPAddr = "";
string MaskCode = "";
string GateWay = "";
int InternetPort = 0;
byte[] IPAddrbuf = new byte[4];
byte[] MaskCodebuf = new byte[4];
byte[] GateWaybuf = new byte[4];
byte[] InternetPortbuf = new byte[2];
byte ReaderAddr = (byte)(ReaderNum & 0xFF);
if (0x00 == rfid_sp.Config_GetInternetAccess(CS, IPAddrbuf, MaskCodebuf, GateWaybuf, InternetPortbuf, ReaderAddr))
{
TempStr = "???????????!";
TempStrEnglish = "Reader ethernet query success!";
ByteToDecimalstr(ref IPAddr, IPAddrbuf);
ByteToDecimalstr(ref MaskCode, MaskCodebuf);
ByteToDecimalstr(ref GateWay, GateWaybuf);
InternetPort = (InternetPortbuf[1] << 8) + (int)InternetPortbuf[0];
}
else
{
TempStr = "???????????!";
TempStrEnglish = "Reader ethernet query fail!";
}
ADDRESS_textBox.Text = IPAddr;
MASK_textBox.Text = MaskCode;
GATEWAY_textBox.Text = GateWay;
PORT_textBox.Text = Convert.ToString(InternetPort);
//if (0 == Language)
// ListBoxAdd(TempStr);
//else
// ListBoxAdd(TempStrEnglish);
return 0;
}
private void SETINTERNET_button_Click(object sender, EventArgs e)
{
}
private void Form2_Load(object sender, EventArgs e)
{
int status = 0;
status = ConnectReader();
if (0 == status)
{
CS.hCom = -1;
CS.sockClient = sockClient;
//CONNECT_button.Enabled = false;
//DISCONNECT_button.Enabled = true;
ConnectFlag = 1; //?????(connect flag)
//MessageBox.Show("success");
QueryInternet(255);
//GetVersion(255);
//timer1.Enabled = true;
//GetSerialNum(255);
//timer1.Enabled = true;
//Thread GSNT = new Thread(new ThreadStart(GetSerialNumThread));
//Thread GVT = new Thread(new ThreadStart(GetVersionThread));
//GSNT.Start();
//GVT.Start();
}
}
private int ConnectReader()
{
int status = 0;
string TempStr = "";
string TempStrEnglish = "";
status = SocketConnect();
switch (status)
{
case 0:
TempStr = "????!";
TempStrEnglish = "Connect success!";
//CONNECT_button.Enabled = false;
//DISCONNECT_button.Enabled = true;
break;
case -1:
TempStr = "????!";
TempStrEnglish = "Connect fail!";
//CONNECT_button.Enabled = true;
//DISCONNECT_button.Enabled = false;
break;
case -2:
TempStr = "???????!";
TempStrEnglish = "Connect parameter incomplete!";
//CONNECT_button.Enabled = true;
//DISCONNECT_button.Enabled = false;
break;
case -3:
TempStr = "??IP????!";
TempStrEnglish = "Input IP Invalid!";
//CONNECT_button.Enabled = true;
//DISCONNECT_button.Enabled = false;
break;
}
//if (0 == Language)
// ListBoxAdd(TempStr);
//else
// ListBoxAdd(TempStrEnglish);
//if (TempStr != "????!" || TempStrEnglish != "Connect success!")
// return -1;
return 0;
}
private int SocketConnect()
{
IPAddress ipaddr;
//if (txt_IpAddress.Text == "" || txt_Port.Text == "")
//{
// return -2; //???????(input info incomplete)
//}
if (!IPAddress.TryParse("192.168.1.178", out ipaddr))
{
return -3;
}
else
{
if (rfid_sp.Socket_ConnectSocket(ref sockClient,
"192.168.1.178",
Convert.ToInt32("100"))
== RFID_StandardProtocol.SUCCESS)
return 0; //????(connect success)
else
return -1; //????(connect fail)
}
}
private void DisconnectHandler()
{
int status = 0;
status = DisConnectReader();
if (0 == status)
{
//CONNECT_button.Enabled = true;
//DISCONNECT_button.Enabled = false;
ConnectFlag = 0; //?????(connect falg)
}
}
private int DisConnectReader()
{
int status = 0;
string TempStr = "";
string TempStrEnglish = "";
status = SocketDisConnect();
switch (status)
{
case 0:
TempStr = "????!";
TempStrEnglish = "Close success!";
break;
case -1:
TempStr = "????!";
TempStrEnglish = "Close fail!";
break;
case -2:
TempStr = "??????!";
TempStrEnglish = "Handle unavailable!";
break;
}
//if (0 == Language)
// ListBoxAdd(TempStr);
//else
// ListBoxAdd(TempStrEnglish);
//if (TempStr != "????!" || TempStrEnglish != "Close success!")
// return -1;
return 0;
}
private int SocketDisConnect()
{
if (sockClient != null)
{
if (rfid_sp.Socket_CloseSocket(sockClient) == RFID_StandardProtocol.SUCCESS)
{
CS.sockClient = null;
return 0; //????(close success)
}
else
return -1; //????(close fail)
}
else
{
return -2; //????(handle unavailable)
}
}
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
DisconnectHandler();
}
}
}