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 - 测试代码