PLC时间对接
定时器测试 s7通讯加辅助方法 4.21 测试
This commit is contained in:
1 parent
76cd069435
commit
e8001a81bb
4 files changed
+238
-148
No files matched your search
@@ -280,6 +280,10 @@ namespace WindowsFormsApp1.server
|
||||
.ToArray());
|
||||
break;
|
||||
|
||||
case "dtl":
|
||||
address[i].value = GetDTLTime(read.Content, index).ToString();
|
||||
break;
|
||||
|
||||
default:
|
||||
return BllResult.Error("找不到未知的数据类型");
|
||||
}
|
||||
@@ -359,6 +363,12 @@ namespace WindowsFormsApp1.server
|
||||
stringBytes.CopyTo(writebyte, index);
|
||||
break;
|
||||
|
||||
case "dtl":
|
||||
DateTime totime = Convert.ToDateTime(value);
|
||||
byte[] totime_byte = GetBytesFromDTLTime(totime);
|
||||
totime_byte.CopyTo(writebyte, index);
|
||||
break;
|
||||
|
||||
default:
|
||||
return BllResult.Error("找不到未知的数据类型");
|
||||
}
|
||||
@@ -803,5 +813,40 @@ namespace WindowsFormsApp1.server
|
||||
|
||||
|
||||
#endregion
|
||||
|
||||
#region
|
||||
public byte[] GetBytesFromDTLTime(DateTime dateTime)
|
||||
{
|
||||
byte[] array = new byte[12];
|
||||
ByteTransform.TransByte((short)dateTime.Year).CopyTo(array, 0);
|
||||
array[2] = (byte)dateTime.Month;
|
||||
array[3] = (byte)dateTime.Day;
|
||||
array[4] = 5;
|
||||
array[5] = (byte)dateTime.Hour;
|
||||
array[6] = (byte)dateTime.Minute;
|
||||
array[7] = (byte)dateTime.Second;
|
||||
ByteTransform.TransByte(dateTime.Millisecond * 1000 * 1000).CopyTo(array, 8);
|
||||
return array;
|
||||
}
|
||||
|
||||
public DateTime GetDTLTime(byte[] buffer, int index)
|
||||
{
|
||||
int year = (int)ByteTransform.TransInt16(buffer, index);
|
||||
int month = (int)buffer[index + 2];
|
||||
int day = (int)buffer[index + 3];
|
||||
int hour = (int)buffer[index + 5];
|
||||
int minute = (int)buffer[index + 6];
|
||||
int second = (int)buffer[index + 7];
|
||||
int millisecond = ByteTransform.TransInt32(buffer, index + 8) / 1000 / 1000;
|
||||
if (year == 0 && month == 0 && day == 0)
|
||||
{
|
||||
year = 1900;
|
||||
month = 1;
|
||||
day = 1;
|
||||
}
|
||||
return new DateTime(year, month, day, hour, minute, second, millisecond);
|
||||
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user