1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
| internal class RFID_SICK_RFU630 : xStringEx { private xString Connected = new xString(); private xString Enable = new xString(); private xString SendString = new xString(); private xString RecvString = new xString(); private static TcpClient tcpClient = new TcpClient();
private bool EnableLog = false; private string LogFile = "RFID_SICK_RFU630"; private string remoteIP = string.Empty; private int remotePort = 2112; private int recvTimeout = 500;
public RFID_SICK_RFU630() { Connected = TCreate(new xString(), "Connected"); Enable = TCreate(new xString(), "Enable"); SendString = TCreate(new xString(), "SendString"); RecvString = TCreate(new xString(), "RecvString"); }
public xState Init(string sPara) { try { Dictionary<string, object> PortCif = new Dictionary<string, object> { { "IPAddr", "127.0.0.1" }, { "PortNo", 2112 }, { "RecvTimeout", 1000 }, { "EnableLog", true }, { "LogFileName", LogFile } }; XmlFO.Configs.Get("TCPClient", sPara, PortCif);
remoteIP = Convert.ToString(PortCif["IPAddr"]); remotePort = Convert.ToInt32(PortCif["PortNo"]); recvTimeout = Convert.ToInt32(PortCif["RecvTimeout"]); EnableLog = Convert.ToBoolean(PortCif["EnableLog"]); LogFile = Convert.ToString(PortCif["LogFileName"]);
return xState.xTrue; } catch (Exception e) { xErrorOutException(e); return xState.xFalse; } }
public xState RFIDRecv(string sPara) { try { if (!IsOnline()) Connect(); string sendData = "\u0002" + XmlFO.StringAnalysis(sPara) + "\u0003"; string tcpRecvString = TCPSendRecv(sendData); if (tcpRecvString.Length < 40) return xState.xFalse; tcpRecvString = tcpRecvString.Substring(24, 40).Trim();
int length = tcpRecvString.Length; byte[] bytes = new byte[length / 2]; for (int i = 0; i < length; i += 2) { bytes[i / 2] = Convert.ToByte(tcpRecvString.Substring(i, 2), 16); } tcpRecvString = Encoding.ASCII.GetString(bytes).Trim('\0').Trim(); RecvString.xValue = tcpRecvString; Logout("Recv -> VIN: " + tcpRecvString);
return xState.xTrue; } catch (Exception ex) { Logout(ex.ToString()); return xState.xFalse; } }
public xState RFIDWriteVIN(string sPara) { try { if (!IsOnline()) Connect(); string vin = XmlFO.StringAnalysis(sPara.Trim()); Logout("写入VIN: " + vin); string vinHexString = string.Empty; Encoding.ASCII.GetBytes(vin).ToList().ForEach(b => vinHexString += b.ToString("X2"));
string sendString = "\u0002" + "sMN TAwriteTagData 0 3 0 9 32 +36 " + vinHexString.PadRight(36, '0') + "\u0003"; string tcpRecvString = TCPSendRecv(sendString); RecvString.xValue = tcpRecvString;
return xState.xTrue; } catch (Exception ex) { Logout(ex.ToString()); return xState.xFalse; } }
private string TCPSendRecv(string sendData) { Logout("Send -> " + sendData); tcpClient.Client.Send(Encoding.ASCII.GetBytes(sendData));
byte[] tcpRecvByts = new byte[1024]; int recvLength = tcpClient.Client.Receive(tcpRecvByts); if (recvLength < 2) return ""; byte[] tmpRecvData = new byte[recvLength]; Array.Copy(tcpRecvByts, tmpRecvData, recvLength);
string tcpRecvString = Encoding.ASCII.GetString(tmpRecvData).Trim('\0').Trim(); Logout("Recv -> " + tcpRecvString); return tcpRecvString; }
private void Connect() { if (tcpClient == null) tcpClient = new TcpClient(); if (tcpClient.Connected) return; tcpClient.Close(); tcpClient = new TcpClient(); tcpClient.Connect(IPAddress.Parse(remoteIP), remotePort); tcpClient.Client.ReceiveTimeout = recvTimeout; }
public bool IsOnline() { if (tcpClient == null) return false; bool onLine = !((tcpClient.Client.Poll(100, SelectMode.SelectRead) && (tcpClient.Client.Available == 0)) || !tcpClient.Client.Connected); if (!onLine) tcpClient = null; Connected.xValue = onLine.ToString(); return onLine; }
private void Logout(string msg) { if(EnableLog) OnlyOneStartUp.Log(LogFile, msg); } }
|