zoukankan      html  css  js  c++  java
  • 使用UDP协议与韩国OACIS压机通讯

    最近一个项目需要发送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;
            }
        }
  • 相关阅读:
    Linux官方源、镜像源汇总
    python3 pip报错 TypeError: 'module' object is not callable
    2019-11-27:kali 2019-4中文乱码解决方法
    2019-11-26:密码学基础知识,csrf防御
    2019-11-25:信息收集,笔记
    2019-11-24:postgresql数据库安装,最后报错failed to load SQLModule 问题的解决方案
    2019-11-22:xss绕过笔记
    2019-11-20:xss学习笔记
    2019-11-19:无返回的盲型xxe,使用带外读取数据
    2019-11-19:xxe漏洞利用,笔记
  • 原文地址:https://www.cnblogs.com/fanxingthink/p/6187797.html
Copyright © 2011-2022 走看看