下面 //// 内的代码没看懂,求详细解释!
byte[] btMsg, btBuf;
byte[] btSend;
char[] chBuf;
int iBt;
DateTime dtNow = DateTime.Now;
btMsg = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0 }; //////////////////没看懂的内容///////////////////////
btBuf = Encoding.ASCII.GetBytes(dtNow.Second.ToString("X2"));
chBuf = Encoding.ASCII.GetChars(btBuf);
iBt = Convert.ToInt16(chBuf[0]);
iBt <<= 4; // 为什么要这么做?
iBt += Convert.ToInt16(chBuf[1]);
btMsg[0] = (byte)iBt; // 还有这个,大于255的数转成 byte 有什么作用?
/////////////////////////////////////////
byte[] btMsg, btBuf;
byte[] btSend;
char[] chBuf;
int iBt;
DateTime dtNow = DateTime.Now;
btMsg = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0 }; //////////////////没看懂的内容///////////////////////
btBuf = Encoding.ASCII.GetBytes(dtNow.Second.ToString("X2"));
chBuf = Encoding.ASCII.GetChars(btBuf);
iBt = Convert.ToInt16(chBuf[0]);
iBt <<= 4; // 为什么要这么做?
iBt += Convert.ToInt16(chBuf[1]);
btMsg[0] = (byte)iBt; // 还有这个,大于255的数转成 byte 有什么作用?
/////////////////////////////////////////
(int)60 --- 0x3C --- > btBuf 33 43 --- chBuf '3' 'C' '3' ==33
'C' ==43;iBt == (0x33<<4)+0x43 0x373
取最后 0x73/...
验效?
public byte[] SetTime(Int16 desAddress) //设置时钟参数
{
byte[] btMsg, btBuf;
byte[] btSend;
char[] chBuf;
int iBt;
DateTime dtNow = DateTime.Now;
btMsg = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0 }; //计算秒
btBuf = Encoding.ASCII.GetBytes(dtNow.Second.ToString("X2"));
chBuf = Encoding.ASCII.GetChars(btBuf);
iBt = Convert.ToInt16(chBuf[0]);
iBt <<= 4;
iBt += Convert.ToInt16(chBuf[1]);
btMsg[0] = (byte)iBt;
btBuf = Encoding.ASCII.GetBytes(dtNow.Minute.ToString("X2"));
chBuf = Encoding.ASCII.GetChars(btBuf);
iBt = Convert.ToInt16(chBuf[0]);
iBt <<= 4;
iBt += Convert.ToInt16(chBuf[1]);
btMsg[1] = (byte)iBt; btBuf = Encoding.ASCII.GetBytes(dtNow.Hour.ToString("X2"));
chBuf = Encoding.ASCII.GetChars(btBuf);
iBt = Convert.ToInt16(chBuf[0]);
iBt <<= 4;
iBt += Convert.ToInt16(chBuf[1]);
btMsg[2] = (byte)iBt;
btBuf = Encoding.ASCII.GetBytes(dtNow.Day.ToString("X2"));
chBuf = Encoding.ASCII.GetChars(btBuf);
iBt = Convert.ToInt16(chBuf[0]);
iBt <<= 4;
iBt += Convert.ToInt16(chBuf[1]);
btMsg[3] = (byte)iBt; btBuf = Encoding.ASCII.GetBytes(dtNow.Month.ToString("X2"));
chBuf = Encoding.ASCII.GetChars(btBuf);
iBt = Convert.ToInt16(chBuf[0]);
iBt <<= 4;
iBt += Convert.ToInt16(chBuf[1]);
btMsg[4] = (byte)iBt; iBt = (int)dtNow.DayOfWeek;
btMsg[5] = (byte)iBt; iBt = Convert.ToInt16(dtNow.Year.ToString().Substring(2));
btMsg[6] = (byte)iBt;
btSend = new byte[btMsg.Length + 9];
btSend[0] = 0x7E;
btSend[1] = 0x9B;
btSend[2] = 0xFF;
btSend[3] = 0xFF;
btSend[4] = (byte)(desAddress >>= 8);
btSend[5] = (byte)(desAddress & 0xFF);
btSend[6] = 0x03; btSend[7] = (byte)btMsg.Length;
btMsg.CopyTo(btSend, 8); btSend[btSend.Length - 1] = 0xAA;
return (btSend);
}