最近一个项目需要发送SN给OACIS,
研究了一下OACIS文档,
从文档中可以看出,传输协议只能使用UDP,切传输命令为>SN1; + SN + ; + LF
LF的定义在Linux和Unix中换行符,但是在Windows中换行为CRLF,MAC中又为CR
所以这里只能用ASCII,LF的ASCII为10
所以程序如下:
public class OACISHelper { public string[] strArrGV; public string strCN; public string strDateTime; public string strFM; public string strOACISIPAdd; public int iOACISPort; public string strSN; public bool setSN(string val_strSN) { char ch = Convert.ToChar(10); string str = string.Concat(new object[] { ">SN1;", val_strSN, ";", ch }); string str2 = this.Request(str); return ((str2.Length > 4) && (str2.Substring(0, 4) == "<SN1")); } private string Request(string val_strRequest) { string str = this.RequestOneTime(val_strRequest); if ((str.Substring(0, 1) != "#") || (str.Substring(str.Length - 1) != "$")) { str = this.RequestOneTime(val_strRequest); if ((str.Substring(0, 1) == "#") && (str.Substring(str.Length - 1) == "$")) { return str; } str = this.RequestOneTime(val_strRequest); if ((str.Substring(0, 1) == "#") && (str.Substring(str.Length - 1) == "$")) { return str; } str = this.RequestOneTime(val_strRequest); if ((str.Substring(0, 1) == "#") && (str.Substring(str.Length - 1) == "$")) { return str; } str = this.RequestOneTime(val_strRequest); if ((str.Substring(0, 1) == "#") && (str.Substring(str.Length - 1) == "$")) { return str; } } return str; } private string RequestOneTime(string val_strRequest) { byte[] bytes = null; try { byte[] dgram = Encoding.Default.GetBytes(val_strRequest); UdpClient client = new UdpClient(); IPEndPoint remoteEP = new IPEndPoint(IPAddress.Any, 0); client.Client.ReceiveTimeout = 0x3e8; client.Send(dgram, dgram.Length, this.strOACISIPAdd, this.iOACISPort); bytes = client.Receive(ref remoteEP); return Encoding.Default.GetString(bytes); } catch (Exception exception) { string str2 = ""; if (bytes != null) { for (int i = 0; i < bytes.Length; i++) { str2 = str2 + " / " + i.ToString() + "-" + bytes[i].ToString(); } } return exception.Message; } } public bool readResults() { string str = this.Request("@UG;LN;004;$"); string[] strArray = str.Split(new char[] { ‘;‘ }); if (((strArray[0] == "#UG") && (strArray[strArray.Length - 1] == "$")) && (strArray.Length == 0x86)) { this.strCN = strArray[4]; this.strFM = strArray[10]; this.strSN = strArray[strArray.Length - 3]; this.strDateTime = strArray[8]; for (int i = 0; i < 120; i++) { this.strArrGV[i] = strArray[i + 11]; } return true; } this.strSN = str; return false; } }
时间: 2024-09-30 06:40:10