C# - 记录上报PLC数据,需要转换各种格式的代码

1.上报PLC - 年月日转换成BCD码格式 - 测试代码

            ///测试转换成BCD码 格式///start///
            short[] tempData = new short[100];
            DateTime processStartTime = new DateTime(2014, 9, 16, 15, 18, 32);
            int nIndex = 0;
            //年月日转成BCD - 一个数字对应四位 四个数字=1 word ps:0914 = 0000 1001 0001 0100
            int year = processStartTime.Year;
            int month = processStartTime.Month;
            int day = processStartTime.Day;
            int hour = processStartTime.Hour;
            int minute = processStartTime.Minute;
            int second = processStartTime.Second;

            short year1 = (short)(BitConverter.ToInt16(BitConverter.GetBytes((year.ToString("D4")).ElementAt(2)), 0) - 48);
            short year2 = (short)(BitConverter.ToInt16(BitConverter.GetBytes((year.ToString("D4")).ElementAt(3)), 0) - 48);
            short month1 = (short)(BitConverter.ToInt16(BitConverter.GetBytes((month.ToString("D2")).ElementAt(0)), 0) - 48);
            short month2 = (short)(BitConverter.ToInt16(BitConverter.GetBytes((month.ToString("D2")).ElementAt(1)), 0) - 48);
            short day1 = (short)(BitConverter.ToInt16(BitConverter.GetBytes((day.ToString("D2")).ElementAt(0)), 0) - 48);
            short day2 = (short)(BitConverter.ToInt16(BitConverter.GetBytes((day.ToString("D2")).ElementAt(1)), 0) - 48);
            short hour1 = (short)(BitConverter.ToInt16(BitConverter.GetBytes((hour.ToString("D2")).ElementAt(0)), 0) - 48);
            short hour2 = (short)(BitConverter.ToInt16(BitConverter.GetBytes((hour.ToString("D2")).ElementAt(1)), 0) - 48);
            short minute1 = (short)(BitConverter.ToInt16(BitConverter.GetBytes((minute.ToString("D2")).ElementAt(0)), 0) - 48);
            short minute2 = (short)(BitConverter.ToInt16(BitConverter.GetBytes((minute.ToString("D2")).ElementAt(1)), 0) - 48);
            short second1 = (short)(BitConverter.ToInt16(BitConverter.GetBytes((second.ToString("D2")).ElementAt(0)), 0) - 48);
            short second2 = (short)(BitConverter.ToInt16(BitConverter.GetBytes((second.ToString("D2")).ElementAt(1)), 0) - 48);

            //  tempData[nIndex++] = (short)plcStatus.PLCSendData_MarkZR.recipeData_LaserA_A.nTotalProccessTime;
            tempData[nIndex++] = (short)((short)(month1 << 12) + (short)(month2 << 8) + (short)(year1 << 4) + year2);
            tempData[nIndex++] = (short)((short)(hour1 << 12) + (short)(hour2 << 8) + (short)(day1 << 4) + day2);
            tempData[nIndex++] = (short)((short)(second1 << 12) + (short)(second2 << 8) + (short)(minute1 << 4) + minute2);

            ///测试转换成BCD码 格式///end//

2.读出PLC - short转换成string字符  - 测试代码

            /测试转换stringstart///一个D/W两个字符///
            short[] nID = new short[10];//读出PLC的值
            nID[0] = 49;//0x30=1
            nID[1] = 50;//0x31=2
            nID[2] = 65;//0x41=A
            nID[3] = 66;//0x42=B
            string sWorkID_PLC = "";

            for (int i = 0; i < 10; i++)
            {
                sWorkID_PLC += Convert.ToString((char)(nID[i] & 0x00ff)) + Convert.ToString((char)(nID[i] >> 8 & 0x00ff));
            }
            sWorkID_PLC = sWorkID_PLC.Replace("\0", "");
            sWorkID_PLC = sWorkID_PLC.Replace("\r", "");
            sWorkID_PLC = sWorkID_PLC.Replace("\n", "");
            /测试转换stringend//

3.上报PLC - string转换成short  - 测试代码

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值