public ErrState getCardType(byte[] snr, ref byte type) { byte[] buf = new byte[256]; int rxLen = 0; buf[0] = 0xFF; buf[1] = (byte)'N'; // read button type ErrState state = sendMsg(buf, 2, buf, ref rxLen); if ((state == ErrState.ERR_NO_ERROR) && (rxLen > 0)) { if ((buf[0] == 0xFF) && (buf[1] == 'N') && (buf[2] > 0)) { #if true snr[0] = buf[8]; snr[1] = buf[7]; snr[2] = buf[6]; snr[3] = buf[5]; snr[4] = buf[4]; snr[5] = buf[3]; #else snr[5] = buf[8]; snr[4] = buf[7]; snr[3] = buf[6]; snr[2] = buf[5]; snr[1] = buf[4]; snr[0] = buf[3]; #endif type = buf[2]; return(ErrState.ERR_NO_ERROR); } } type = 0; return(state); }
public int buzzer(int onOff) { byte[] buf = new byte[256]; int rxLen = 0; if (onOff > 0) { buf[0] = 0x81; // Buzzer on } else { buf[0] = 0x80; // Buzzer off } ErrState state = sendMsg(buf, 1, buf, ref rxLen); if ((state != ErrState.ERR_NO_ERROR) || (rxLen == 0)) { return(1); } return(0); }
public ErrState readCardHeader(TcpClient tcpclient) { byte[] str = new byte[256]; int rxLen = 0; Socket soc = tcpclient.Client; int fIndex, line = 0, readSize = 37, z, fSize = 200; byte[] rawBuf = new byte[80]; byte crc; byte[] txBuf = new byte[256]; ErrState state = readCardData(0, 200, str, ref rxLen); if (state != ErrState.ERR_NO_ERROR) { return(state); } if (rxLen != 200) { return(ErrState.ERR_NO_CARDREADER); } soc.Send(Encoding.ASCII.GetBytes("OK CARDDATA READHEADER 200"), SocketFlags.None); for (fIndex = 0; fIndex < fSize; line++) { string sline = string.Format("{0:X4}", line); Array.Copy(Encoding.ASCII.GetBytes(sline), 0, txBuf, 0, 4); if ((fIndex + readSize) > fSize) { readSize = fSize - fIndex; } Array.Copy(str, line * 37, rawBuf, 0, readSize); fIndex += readSize; for (z = 0; z < readSize; z++) { txBuf[4 + (z * 2) + 0] = (byte)Hex[rawBuf[z] / 16]; txBuf[4 + (z * 2) + 1] = (byte)Hex[rawBuf[z] % 16]; } // end for z // now calc the CRC crc = 0; for (z = 0; z < ((readSize * 2) + 4); z++) { crc ^= txBuf[z]; } txBuf[z++] = (byte)Hex[crc / 16]; txBuf[z++] = (byte)Hex[crc % 16]; txBuf[z++] = 0x0d; int tRet = 0; for (; tRet != z;) { tRet = soc.Send(txBuf, 0, z, SocketFlags.None); if (tRet == 0) { Thread.Sleep(10); } } } byte[] r = { (byte)'\r' }; soc.Send(r, SocketFlags.None); return(ErrState.ERR_NO_ERROR); }
public int setBaud(int speed) { byte[] str = new byte[8]; byte[] resp = new byte[256]; int ns; int rxLen = 0; // <0xc1><'A'><Speed> Speed 0 = 9600, 1=19200,38400,57600,115200 str[0] = 0xc1; str[1] = (byte)'A'; ns = speed; switch (speed) { case 9600: { str[2] = 0; break; } case 19200: { str[2] = 1; break; } case 38400: { str[2] = 2; break; } case 57600: { str[2] = 3; break; } case 115200: { str[2] = 4; break; } default: { str[2] = 0; ns = 9600; break; } } this._timeOut = 1000; ErrState state = sendMsg(str, 3, resp, ref rxLen); if (state != ErrState.ERR_NO_ERROR) { return(9600); } try { this._currentPort.Close(); Thread.Sleep(20); Baudrate = ns; this._currentPort.Open(); Array.Clear(resp, 0, resp.Length); state = sendMsg(str, 3, resp, ref rxLen); if (state != ErrState.ERR_NO_ERROR) { return(9600); } if ((rxLen > 0) && (resp[0] == 0xC1) && (resp[1] == 'A') && (resp[2] == str[2])) { return(ns); } else { Baudrate = 9600; return(9600); } } catch (Exception) { return(9600); } }