这两天出了点问题,由于不会操作加密机,顺道学习了下加密机的硬件操作,不过已经实现了讲过网络加密机对新国网正式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.串口操作自行补脑,这里就不多解释了
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
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