使用UDP协议与韩国OACIS压机通讯

时间:2022-09-02 16:34:48

最近一个项目需要发送SN给OACIS,

研究了一下OACIS文档,

使用UDP协议与韩国OACIS压机通讯

使用UDP协议与韩国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();
string str = string.Concat(new object[] { ">SN1;", val_strSN, ";", ch });
string str2 = this.Request(str);
return ((str2.Length > ) && (str2.Substring(, ) == "<SN1"));
} private string Request(string val_strRequest)
{
string str = this.RequestOneTime(val_strRequest);
if ((str.Substring(, ) != "#") || (str.Substring(str.Length - ) != "$"))
{
str = this.RequestOneTime(val_strRequest);
if ((str.Substring(, ) == "#") && (str.Substring(str.Length - ) == "$"))
{
return str;
}
str = this.RequestOneTime(val_strRequest);
if ((str.Substring(, ) == "#") && (str.Substring(str.Length - ) == "$"))
{
return str;
}
str = this.RequestOneTime(val_strRequest);
if ((str.Substring(, ) == "#") && (str.Substring(str.Length - ) == "$"))
{
return str;
}
str = this.RequestOneTime(val_strRequest);
if ((str.Substring(, ) == "#") && (str.Substring(str.Length - ) == "$"))
{
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, );
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 = ; 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[] == "#UG") && (strArray[strArray.Length - ] == "$")) && (strArray.Length == 0x86))
{
this.strCN = strArray[];
this.strFM = strArray[];
this.strSN = strArray[strArray.Length - ];
this.strDateTime = strArray[];
for (int i = ; i < ; i++)
{
this.strArrGV[i] = strArray[i + ];
}
return true;
}
this.strSN = str;
return false;
}
}