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;
            }
        }
  • 相关阅读:
    HDU-3336-Count the string(扩展KMP)
    洛谷-P3805-Manacher模板
    洛谷-p5410-扩展KMP模板
    HDU-2594-Simpsons' Hidden Talents(kmp, 扩展kmp)
    POJ-3080-Blue jeans(KMP, 暴力)
    [办公应用]两个单独的列数据快速变为两列匹配关联数据
    [办公应用]如何将单词中的部分字母加下划线
    [办公应用]word 2007:全屏快捷键,让复制图片保持原样大小(office 全屏快捷键)
    [办公自动化]如何让excel图表标签中显示最新值数据
    [计算机故障处理]无法访问网络共享资源
  • 原文地址:https://www.cnblogs.com/fanxingthink/p/6187797.html
Copyright © 2011-2022 走看看