这两天出了点问题,由于不会操作加密机,顺道学习了下加密机的硬件操作,不过已经实现了讲过网络加密机对新国网正式ESAM进行认证。
先把图片放出来吧
整个认证过程我没有对端口进行监听,也就略过了socket这部分内容,也许后续会完善进去,话不多说了,直接贴代码吧。有点乱,容我有时间整里下。
1.动态库函数调用
[DllImport("SJJ1009forformalchip.dll")]//连接设备 public static extern int ConnectDevice(string putIp, string putPortk, string putCTime); [DllImport("SJJ1009forformalchip.dll")]//关闭设备 public static extern int CloseDevice(); [DllImport("SJJ1009forformalchip.dll")]//身份认证 public static extern int Meter_Formal_IdentityAuthentication(int Flag, string PutDiv, System.Text.StringBuilder OutRand, System.Text.StringBuilder OutEndata);
2.串口操作自行补脑,这里就不多解释了
class="code_img_closed" src="/Upload/Images/2014053017/0015B68B3C38AA5B.gif" alt="" />logs_code_hide('9504f4f6-647e-46f2-bfec-a57eca69f086',event)" src="/Upload/Images/2014053017/2B1B950FA3DF188F.gif" alt="" />1 namespace RemoteReceiveComm 2 { 3 using System; 4 using System.Runtime.InteropServices; 5 using System.Text; 6 7 internal class MyCom 8 { 9 private byte bParity; 10 private int bSize; 11 private byte bStopBits; 12 private const uint GENERIC_READ = 0x80000000; 13 private const uint GENERIC_WRITE = 0x40000000; 14 private int hComm; 15 private const int INVALID_HANDLE_VALUE = -1; 16 private int iPort; 17 private int iRate; 18 private int iTimeout; 19 private const int OPEN_EXISTING = 3; 20 public bool Opened; 21 22 public MyCom() 23 { 24 this.iTimeout = 0xfa0; 25 this.hComm = -1; 26 this.Opened = false; 27 this.iPort = 0; 28 this.iRate = 0x2580; 29 this.bSize = 8; 30 this.bParity = 0; 31 this.bStopBits = 0; 32 } 33 34 public MyCom(int com, int rate, int size, byte parity, byte stopbits) 35 { 36 this.iTimeout = 0xfa0; 37 this.hComm = -1; 38 this.Opened = false; 39 this.iPort = com; 40 this.iRate = rate; 41 this.bSize = size; 42 this.bParity = parity; 43 this.bStopBits = stopbits; 44 } 45 46 [DllImport("kernel32.dll")] 47 private static extern bool BuildCommDCB(string lpDef, ref DCB lpDCB); 48 public void Close() 49 { 50 if (this.hComm != -1) 51 { 52 CloseHandle(this.hComm); 53 this.Opened = false; 54 } 55 } 56 57 [DllImport("kernel32.dll")] 58 private static extern bool CloseHandle(int hObject); 59 [DllImport("kernel32.dll")] 60 private static extern int CreateFile(string lpFileName, uint dwDesiredAccess, int dwShareMode, int lpSecurityAttributes, int dwCreationDisposition, int dwFlagsAndAttributes, int hTemplateFile); 61 [DllImport("kernel32.dll")] 62 private static extern bool GetCommState(int hFile, ref DCB lpDCB); 63 [DllImport("kernel32.dll")] 64 private static extern bool GetCommTimeouts(int hFile, ref COMMTIMEOUTS lpCommTimeouts); 65 public void Open() 66 { 67 DCB structure = new DCB(); 68 COMMTIMEOUTS lpCommTimeouts = new COMMTIMEOUTS(); 69 this.hComm = CreateFile("COM" + this.iPort, 0xc0000000, 0, 0, 3, 0, 0); 70 if (this.hComm == -1) 71 { 72 this.Opened = false; 73 throw new ApplicationException("打开所选串口失败!!"); 74 } 75 GetCommTimeouts(this.hComm, ref lpCommTimeouts); 76 lpCommTimeouts.ReadTotalTimeoutConstant = this.iTimeout; 77 lpCommTimeouts.ReadTotalTimeoutMultiplier = 0; 78 lpCommTimeouts.WriteTotalTimeoutMultiplier = 0; 79 lpCommTimeouts.WriteTotalTimeoutConstant = 0; 80 SetCommTimeouts(this.hComm, ref lpCommTimeouts); 81 structure.DCBlength = Marshal.SizeOf(structure); 82 GetCommState(this.hComm, ref structure); 83 structure.BaudRate = this.iRate; 84 structure.Parity = this.bParity; 85 structure.ByteSize = (byte) this.bSize; 86 structure.StopBits = this.bStopBits; 87 SetCommState(this.hComm, ref structure); 88 this.Opened = true; 89 } 90 91 public byte[] Read(int NumBytes) 92 { 93 byte[] lpBuffer = new byte[NumBytes]; 94 if (this.hComm == -1) 95 { 96 throw new ApplicationException("串口没有打开"); 97 } 98 OVERLAPPED lpOverlapped = new OVERLAPPED(); 99 int lpNumberOfBytesRead = 0; 100 ReadFile(this.hComm, lpBuffer, NumBytes, ref lpNumberOfBytesRead, ref lpOverlapped); 101 byte[] destinationArray = new byte[lpNumberOfBytesRead]; 102 Array.Copy(lpBuffer, destinationArray, lpNumberOfBytesRead); 103 return destinationArray; 104 } 105 106 [DllImport("kernel32.dll")] 107 private static extern bool ReadFile(int hFile, byte[] lpBuffer, int nNumberOfBytesToRead, ref int lpNumberOfBytesRead, ref OVERLAPPED lpOverlapped); 108 public string ReadS(int NumBytes) 109 { 110 byte[] lpBuffer = new byte[NumBytes]; 111 if (this.hComm == -1) 112 { 113 throw new ApplicationException("串口没有打开"); 114 } 115 OVERLAPPED lpOverlapped = new OVERLAPPED(); 116 int lpNumberOfBytesRead = 0; 117 ReadFile(this.hComm, lpBuffer, NumBytes, ref lpNumberOfBytesRead, ref lpOverlapped); 118 byte[] destinationArray = new byte[lpNumberOfBytesRead]; 119 Array.Copy(lpBuffer, destinationArray, lpNumberOfBytesRead); 120 return Encoding.Default.GetString(destinationArray); 121 } 122 123 [DllImport("kernel32.dll")] 124 private static extern bool SetCommState(int hFile, ref DCB lpDCB); 125 [DllImport("kernel32.dll")] 126 private static extern bool SetCommTimeouts(int hFile, ref COMMTIMEOUTS lpCommTimeouts); 127 public int Write(byte[] WriteBytes) 128 { 129 int lpNumberOfBytesWritten = 0; 130 if (this.hComm == -1) 131 { 132 throw new ApplicationException("串口没有打开"); 133 } 134 OVERLAPPED lpOverlapped = new OVERLAPPED(); 135 WriteFile(this.hComm, WriteBytes, WriteBytes.Length, ref lpNumberOfBytesWritten, ref lpOverlapped); 136 return lpNumberOfBytesWritten; 137 } 138 139 public int Write(string s) 140 { 141 byte[] bytes = Encoding.Default.GetBytes(s); 142 int lpNumberOfBytesWritten = 0; 143 if (this.hComm == -1) 144 { 145 throw new ApplicationException("串口没有打开"); 146 } 147 OVERLAPPED lpOverlapped = new OVERLAPPED(); 148 WriteFile(this.hComm, bytes, bytes.Length, ref lpNumberOfBytesWritten, ref lpOverlapped); 149 return lpNumberOfBytesWritten; 150 } 151 152 [DllImport("kernel32.dll")] 153 private static extern bool WriteFile(int hFile, byte[] lpBuffer, int nNumberOfBytesToWrite, ref int lpNumberOfBytesWritten, ref OVERLAPPED lpOverlapped); 154 155 public byte BParity 156 { 157 get 158 { 159 return this.bParity; 160 } 161 set 162 { 163 this.bParity = value; 164 } 165 } 166 167 public int BSize 168 { 169 get 170 { 171 return this.bSize; 172 } 173 set 174 { 175 this.bSize = value; 176 } 177 } 178 179 public byte BStopBits 180 { 181 get 182 { 183 return this.bStopBits; 184 } 185 set 186 { 187 this.bStopBits = value; 188 } 189 } 190 191 public int IPort 192 { 193 get 194 { 195 return this.iPort; 196 } 197 set 198 { 199 this.iPort = value; 200 } 201 } 202 203 public int IRate 204 { 205 get 206 { 207 return this.iRate; 208 } 209 set 210 { 211 this.iRate = value; 212 } 213 } 214 215 [StructLayout(LayoutKind.Sequential)] 216 private struct COMMTIMEOUTS 217 { 218 public int ReadIntervalTimeout; 219 public int ReadTotalTimeoutMultiplier; 220 public int ReadTotalTimeoutConstant; 221 public int WriteTotalTimeoutMultiplier; 222 public int WriteTotalTimeoutConstant; 223 } 224 225 [StructLayout(LayoutKind.Sequential)] 226 private struct DCB 227 { 228 public int DCBlength; 229 public int BaudRate; 230 public int fBinary; 231 public int fParity; 232 public int fOutxCtsFlow; 233 public int fOutxDsrFlow; 234 public int fDtrControl; 235 public int fDsrSensitivity; 236 public int fTXContinueOnXoff; 237 public int fOutX; 238 public int fInX; 239 public int fErrorChar; 240 public int fNull; 241 public int fRtsControl; 242 public int fAbortOnError; 243 public int fDummy2; 244 public ushort wReserved; 245 public ushort XonLim; 246 public ushort XoffLim; 247 public byte ByteSize; 248 public byte Parity; 249 public byte StopBits; 250 public char XonChar; 251 public char XoffChar; 252 public char ErrorChar; 253 public char EofChar; 254 public char EvtChar; 255 public ushort wReserved1; 256 } 257 258 [StructLayout(LayoutKind.Sequential)] 259 private struct OVERLAPPED 260 { 261 public int Internal; 262 public int InternalHigh; 263 public int Offset; 264 public int OffsetHigh; 265 public int hEvent; 266 } 267 } 268 }View Code
3.组帧函数
1 namespace RemoteReceiveComm.BaseClass 2 { 3 using System; 4 using System.Windows.Forms; 5 6 public class SetFrames 7 { 8 private string dataLength = "04"; 9 10 private string AddThirtyThree(string str) 11 { 12 str = this.SetTransFrame(str); 13 string[] strArray = str.Split(new char[] { ' ' }); 14 str = ""; 15 for (int i = strArray.Length - 1; i >= 0; i--) 16 { 17 int num2 = Convert.ToByte(strArray[i], 0x10) + 0x33; 18 if (num2 > 0xff) 19 { 20 num2 = Convert.ToByte((int) (num2 - 0x100)); 21 } 22 str = str + Convert.ToByte(num2).ToString("x2"); 23 } 24 return str; 25 } 26 27 public string FramesConfig(string strDataCode, int selectedIndex, string dataLen, string meterNum,string ctrl) 28 { 29 byte[] buffer = null; 30 byte num = 0; 31 int num2 = 0; 32 string str2 = ""; 33 string str3 = meterNum.Substring(10, 2) + meterNum.Substring(8, 2) + meterNum.Substring(6, 2) + meterNum.Substring(4, 2) + meterNum.Substring(2, 2) + meterNum.Substring(0, 2); 34 this.dataLength = dataLen.Trim(); 35 if (this.dataLength.Length == 1) 36 { 37 this.dataLength = "0" + this.dataLength; 38 } 39 else if (this.dataLength.Length > 2) 40 { 41 MessageBox.Show("应输入两位十六进制数!", "银河龙芯提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); 42 return ""; 43 } 44 string str4 = ""; 45 string[] strArray = strDataCode.Split(new char[] { '|' }); 46 if (selectedIndex == 4) 47 { 48 if (strArray.Length == 4) 49 { 50 str4 = (this.AddThirtyThree(strArray[0]) + this.AddThirtyThree(strArray[1])) + this.AddThirtyThree(strArray[2]) + this.AddThirtyThree(strArray[3]); 51 } 52 } 53 else if (((selectedIndex == 3) || (selectedIndex == 8)) || (selectedIndex == 11)) 54 { 55 if (strArray.Length == 3) 56 { 57 str4 = this.AddThirtyThree(strArray[0]) + this.AddThirtyThree(strArray[1]) + this.AddThirtyThree(strArray[2]); 58 } 59 } 60 else if ((selectedIndex == 9) || (selectedIndex == 10)) 61 { 62 if (strArray.Length == 2) 63 { 64 str4 = this.AddThirtyThree(strArray[0]) + this.AddThirtyThree(strArray[1]); 65 } 66 } 67 else if (selectedIndex == 0) 68 { 69 str3 = "111111111111"; 70 meterNum = "111111111111"; 71 this.dataLength = "06"; 72 str4 = this.AddThirtyThree(strDataCode); 73 } 74 else 75 { 76 str4 = this.AddThirtyThree(strDataCode); 77 } 78 string str5 = "68" + str3 + "68" + ctrl + this.dataLength + str4; 79 num2 = str5.Length / 2; 80 buffer = new byte[num2]; 81 for (int i = 0; i < (str5.Length / 2); i++) 82 { 83 buffer[i] = Convert.ToByte(str5.Substring(i * 2, 2), 0x10); 84 num = (byte) (num + buffer[i]); 85 } 86 str2 = num.ToString("x2"); 87 return (str5 + str2 + "16"); 88 } 89 90 public static string GetCS(string strData) 91 { 92 byte[] buffer = null; 93 byte num = 0; 94 buffer = new byte[strData.Length / 2]; 95 for (int i = 0; i < (strData.Length / 2); i++) 96 { 97 buffer[i] = Convert.ToByte(strData.Substring(i * 2, 2), 0x10); 98 num = (byte) (num + buffer[i]); 99 } 100 return num.ToString("x2"); 101 } 102 103 private string SetTransFrame(string str) 104 { 105 int length = str.Length; 106 for (int i = 1; i < (length / 2); i++) 107 { 108 str = str.Insert(((2 * i) + i) - 1, " "); 109 } 110 return str; 111 } 112 113 public string TelecontFramesConfig(string strDataCode, string dataLen, string meterNum, string controlNum) 114 { 115 byte[] buffer = null; 116 int num3; 117 byte num = 0; 118 int num2 = 0; 119 string str2 = ""; 120 string str3 = meterNum.Substring(10, 2) + meterNum.Substring(8, 2) + meterNum.Substring(6, 2) + meterNum.Substring(4, 2) + meterNum.Substring(2, 2) + meterNum.Substring(0, 2); 121 this.dataLength = dataLen.Trim(); 122 if (this.dataLength.Length == 1) 123 { 124 this.dataLength = "0" + this.dataLength; 125 } 126 else if (this.dataLength.Length > 2) 127 { 128 MessageBox.Show("应输入两位十六进制数!", "银河龙芯提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); 129 return ""; 130 } 131 string str4 = ""; 132 string[] strArray = strDataCode.Split(new char[] { '|' }); 133 for (num3 = 0; num3 < strArray.Length; num3++) 134 { 135 str4 = str4 + this.AddThirtyThree(strArray[num3]); 136 } 137 string str5 = "68" + str3 + "68" + controlNum + this.dataLength + str4; 138 num2 = str5.Length / 2; 139 buffer = new byte[num2]; 140 for (num3 = 0; num3 < (str5.Length / 2); num3++) 141 { 142 buffer[num3] = Convert.ToByte(str5.Substring(num3 * 2, 2), 0x10); 143 num = (byte) (num + buffer[num3]); 144 } 145 str2 = num.ToString("x2"); 146 return (str5 + str2 + "16"); 147 } 148 } 149 }View Code
4.连接加密机
1 private void button4_Click(object sender, EventArgs e) 2 { 3 int i=ConnectDevice(textBox1.Text.Trim(), textBox2.Text.Trim(), "5000"); 4 if ( i!= 0) 5 { 6 MessageBox.Show("打开设备失败,请重试!" + ConnectDevice(textBox1.Text.Trim(), textBox2.Text.Trim(), "5000"), "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); 7 } 8 else 9 { 10 button2.Enabled = false; 11 txtState.Text += "打开设备成功"; 12 } 13 }
5.认证
1 private void button3_Click(object sender, EventArgs e) 2 { 3 if (txt_Addr.Text.ToString() == "") 4 { 5 MessageBox.Show("通讯地址不能为空", "提示"); 6 txt_Addr.Focus(); 7 } 8 else if (txt_Addr.Text.Length < 12) 9 { 10 MessageBox.Show("输入地址小于12位!", "提示"); 11 txt_Addr.Focus(); 12 } 13 else if (txtMeter.Text.ToString() == "") 14 { 15 MessageBox.Show("表号不能为空", "提示"); 16 txtMeter.Focus(); 17 } 18 else if (txtMeter.Text.Length < 12) 19 { 20 MessageBox.Show("输入的表号小于12位!", "提示"); 21 txtMeter.Focus(); 22 } 23 else 24 { 25 string seed = "0000" + txtMeter.Text.Trim(); 26 string Address = txt_Addr.Text.Trim(); 27 string dataLen = "20"; 28 string str3 = ""; 29 if (txtEffectTime.Text == "") 30 { 31 MessageBox.Show("请输入有效时长", "提示"); 32 txtEffectTime.Focus(); 33 } 34 else if (txtOperator.Text == "") 35 { 36 MessageBox.Show("请输入操作者代码", "提示"); 37 txtOperator.Focus(); 38 } 39 else if (txtOperator.Text.Length < 8) 40 { 41 MessageBox.Show("操作者代码小于8位", "提示"); 42 txtOperator.Focus(); 43 } 44 else 45 { 46 try 47 { 48 System.Text.StringBuilder OutRand = new System.Text.StringBuilder(2048); 49 System.Text.StringBuilder OutEndata = new System.Text.StringBuilder(2048); 50 int i = 9999; 51 i=Meter_Formal_IdentityAuthentication(0, seed, OutRand, OutEndata); 52 if (i == 0) 53 { 54 txtReceFrame.AppendText("获取加密机认证数据成功! " + Environment.NewLine + "随机数:" + OutRand + Environment.NewLine + "密文:" + OutEndata + Environment.NewLine); 55 //string data_area = reverse("070000FF")+ reverse (txtOperator.Text.Trim()) +reverse(OutRand.ToString()) +reverse(OutEndata.ToString()) +reverse(seed); 56 string data_area = seed + OutRand.ToString() + OutEndata.ToString() + txtOperator.Text.Trim() + "070000FF"; 57 SetFrames frame = new SetFrames(); 58 int length = data_area.Length/2; 59 string str5 =frame.FramesConfig(data_area, 1, length.ToString("X2"), txt_Addr.Text,"03"); 60 SendData(str5); 61 ReceiveData(); 62 } 63 64 } 65 catch (Exception exception) 66 { 67 MessageBox.Show(exception.Message,"提示"); 68 } 69 } 70 } 71 }View Code
6.其中用到的发送及接收函数
(1)发送数据函数
1 private void SendData(string str) 2 { 3 if (!mycom1.Opened) 4 { 5 mycom1.Open(); 6 } 7 8 int num = 0; 9 try 10 { 11 if (!str.Contains("68aaaaaaaaaaaa68110452243337")) 12 { 13 SetText(txtReceFrame, "发送:" + SetTransFrame(str.ToUpper()) + "\r\n"); 14 } 15 byte[] writeBytes = mysendb(delspace(str.ToUpper())); 16 num = mycom1.Write(writeBytes); 17 } 18 catch (Exception exception) 19 { 20 SetText(txtReceFrame, exception.Message + "\r\n"); 21 MessageBox.Show("发送失败,请重试!", "提示!", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); 22 return; 23 } 24 DateTime time = DateTime.Now; 25 }View Code
(2)接收函数(PS:其中与认证无关的请自行略过吧,以后后续还有加抄读和远控功能,都在里边了,也懒得整理了)
private void ReceiveData() { try { byte[] reb = mycom1.Read(0x400); string str = dis_package(reb); if (!str.Contains("68 91 0F ")) { SetText(txtReceFrame, "接收:" + str + " " + DateTime.Now.ToString() + "\r\n"); } string[] strArray = new string[reb.Length]; string strings = ""; int index = 0; int num4 = 0; string character = ""; bool flag = false; foreach (byte num5 in reb) { strings = strings + num5.ToString("X2"); strArray[index] = num5.ToString("X2"); index++; } if (strings.Substring(0, 0x10).Contains("FEFEFEFE")) { num4 = 4; character = "FEFEFEFE"; } else if (strings.Substring(0, 0x10).Contains("FEFEFE")) { num4 = 3; character = "FEFEFE"; } else if (strings.Substring(0, 0x10).Contains("FEFE")) { num4 = 2; character = "FEFE"; } else if (strings.Substring(0, 0x10).Contains("FE")) { num4 = 1; character = "FE"; } else { num4 = 0; character = "68" + txt_Addr.Text.Substring(10, 2) + txt_Addr.Text.Substring(8, 2) + txt_Addr.Text.Substring(6, 2) + txt_Addr.Text.Substring(4, 2); } int[] numArray = SearchString.RegexTest(character, strings); for (int i = 0; i < numArray.Length; i++) { string str6; int num8; string str8; string str4 = strArray[numArray[i] + num4]; string str5 = strArray[(numArray[i] + 8) + num4]; int num7 = 0; if (((str4 == "68") && ((strArray.Length - numArray[i]) > 13)) && ((str5 == "83") || (str5 == "91"))) { num7 = Convert.ToInt32(strArray[(numArray[i] + 9) + num4], 0x10) - 4; if (num7 < 0) { num7 = 0; } byte[] buffer2 = new byte[num7]; str6 = ""; num8 = num7 - 1; while (num8 > -1) { if ((reb[((numArray[i] + 14) + num8) + num4] - 0x33) >= 0) { buffer2[num8] = Convert.ToByte((int)(reb[((numArray[i] + 14) + num8) + num4] - 0x33)); } else { buffer2[num8] = Convert.ToByte((int)((reb[((numArray[i] + 14) + num8) + num4] - 0x33) + 0x100)); } str6 = str6 + buffer2[num8].ToString("x2"); num8--; } string str7 = ""; for (int j = (numArray[i] + 5) + num4; j > (numArray[i] + num4); j--) { str7 = str7 + strArray[j]; } receive = str6; SetDataText("数据:" + str6 + "\r\n"); string randdata2 = str6.Substring(16,8); string ESAMNO = str6.Substring(0,16); txtReceFrame.AppendText("随机数2:"+randdata2+" "+"ESAM序列号:"+ESAMNO+"\r\n"); txtReceFrame.AppendText("认证成功!\r\n"); if (str5 != "91") { SetText(txtReceFrame, "操作成功!\r\n"); } if (receive == "") { receive = "83"; } flag = true; } if ((str4 == "68") && (str5 == "C3")) { MessageBox.Show("安全认证失败,请重试!", "提示!", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); str6 = ""; num7 = Convert.ToInt32(strArray[(numArray[i] + 9) + num4], 0x10); string[] strArray2 = new string[num7]; if (num7 != 2) { MessageBox.Show("返回的不是两个字节的状态字,请重试!" + num7, "提示!", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); return; } for (num8 = 0; num8 < num7; num8++) { if ((reb[(numArray[i] + 10) + num4] - 0x33) >= 0) { strArray2[num8] = TenToTwo(Convert.ToByte((int)(reb[((numArray[i] + 10) + num8) + num4] - 0x33))); } else { strArray2[num8] = TenToTwo(Convert.ToByte((int)((reb[((numArray[i] + 10) + num8) + num4] - 0x33) + 0x100))); } str6 = str6 + strArray2[num8] + " "; } SetDataText("数据:" + str6 + ErrState(str6) + "\r\n"); if (receive == "") { receive = "C3"; } } if ((str4 == "68") && (str5 == "94")) { MessageBox.Show("写入成功!", "提示!", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); } if ((str4 == "68") && (str5 == "D4")) { MessageBox.Show("写入失败!", "提示!", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); str6 = ""; if ((reb[(numArray[i] + 10) + num4] - 0x33) >= 0) { str6 = TenToTwo(Convert.ToByte((int)(reb[(numArray[i] + 10) + num4] - 0x33))); } else { str6 = TenToTwo(Convert.ToByte((int)((reb[(numArray[i] + 10) + num4] - 0x33) + 0x100))); } num7 = Convert.ToInt32(strArray[(numArray[i] + 9) + num4], 0x10); if (num7 != 1) { MessageBox.Show("返回的不是一个字节的状态字,请重试!" + num7, "提示!", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); return; } SetDataText("数据:" + str6 + ErrOneState(str6) + "\r\n"); } if ((str4 == "68") && (str5 == "9C")) { MessageBox.Show(" 跳合闸、报警、保电成功!", "提示!", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); } if ((str4 == "68") && (str5 == "DC")) { MessageBox.Show(" 跳合闸、报警、保电失败!", "提示!", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); num7 = Convert.ToInt32(strArray[(numArray[i] + 9) + num4], 0x10); if (num7 != 1) { MessageBox.Show("返回的不是一个字节的状态字,请重试!" + num7, "提示!", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); return; } str6 = ""; if ((reb[(numArray[i] + 10) + num4] - 0x33) >= 0) { str6 = TenToTwo(Convert.ToByte((int)(reb[(numArray[i] + 10) + num4] - 0x33))); } else { str6 = TenToTwo(Convert.ToByte((int)((reb[(numArray[i] + 10) + num4] - 0x33) + 0x100))); } SetDataText("数据:" + str6 + ErrOneState(str6) + "\r\n"); } if ((str4 == "68") && ((str5 == "9A") || (str5 == "9B"))) { str8 = "清零成功"; if (str5 == "9B") { str8 = "事件" + str8; } MessageBox.Show(str8, "提示!", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); } if ((str4 == "68") && ((str5 == "DA") || (str5 == "DB"))) { str8 = "清零失败"; if (str5 == "9B") { str8 = "事件" + str8; } MessageBox.Show(str8, "提示!", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); num7 = Convert.ToInt32(strArray[(numArray[i] + 9) + num4], 0x10); if (num7 != 1) { MessageBox.Show("返回的不是一个字节的状态字,请重试!" + num7, "提示!", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); return; } str6 = ""; if ((reb[(numArray[i] + 10) + num4] - 0x33) >= 0) { str6 = TenToTwo(Convert.ToByte((int)(reb[(numArray[i] + 10) + num4] - 0x33))); } else { str6 = TenToTwo(Convert.ToByte((int)((reb[(numArray[i] + 10) + num4] - 0x33) + 0x100))); } SetDataText("数据:" + str6 + ErrOneState(str6) + "\r\n"); } } if (!(flag || (reb.Length >= 1))) { MessageBox.Show("无返回数据,请检查户号、表号!", "提示!", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); } } catch (Exception) { MessageBox.Show("读抄失败无返回数据,请重试!", "提示!", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); } if (mycom1.Opened) { mycom1.Close(); } }View Code
————————————————————————————————————
后边就没什么重要的函数了,这次认证的工作终于在节前结束了,可以安心过端午了,哈哈
————————————————————————————————————
回来会继续做抄读和远控的功能,我也把部分代码分享出来,需要的可以参考下。不足之处还请大家指正!