public static byte[] readPacket(ref ERROR_LIST err) { if (!isPortOpen()) { err = ERROR_LIST.ERROR_PORT_NOT_OPEN; return(null); } int readBytes = serialPort.BytesToRead; byte[] readBuffer = new byte[readBytes]; if (serialPort.BytesToRead > 0) { serialPort.Read(readBuffer, 0, readBytes); } else { err = ERROR_LIST.ERROR_RECIVE_DATA_NONE; return(null); } err = ERROR_LIST.ERROR_NONE; return(readBuffer); }
public static byte[] sendCommand(INNCOM_COMMAND_LIST CMD, byte[] payload, ref ERROR_LIST err, int sleepTime) { if (payload == null) { err = ERROR_LIST.ERROR_INPUT_DATA_NONE; return(null); } byte[] command = commandToByteArray(CMD); command = Utility.mergeByteArray(command, payload); return(sendCommand(command, ref err, sleepTime)); }
public static void writePacket(ref byte[] sendbuffer, ref ERROR_LIST err) { if (!isPortOpen()) { err = ERROR_LIST.ERROR_PORT_NOT_OPEN; return; } if (sendbuffer == null) { err = ERROR_LIST.ERROR_INPUT_DATA_NONE; return; } serialPort.Write(sendbuffer, 0, sendbuffer.Length); err = ERROR_LIST.ERROR_NONE; }
public static byte[] buildCMDPacket(byte src, byte dest, byte[] payload, ref ERROR_LIST ret) { if (payload == null) { ret = ERROR_LIST.ERROR_INPUT_DATA_NONE; return(null); } int totalPackageLength = (int)PACKET_CONF.COMM_LEN_OVERHEAD + payload.Length; if (totalPackageLength <= (int)PACKET_CONF.COMM_LEN_OVERHEAD) { ret = ERROR_LIST.ERROR_INPUT_DATA_WRONG; return(null); } byte[] CMD = new byte[totalPackageLength]; CMD[(int)PACKET_CONF.COMM_POS_SOF] = (byte)PACKET_CONF.COMM_SOF; CMD[(int)PACKET_CONF.COMM_POS_VER] = (byte)PACKET_CONF.COMM_VERSION; CMD[(int)PACKET_CONF.COMM_POS_SRC] = src; CMD[(int)PACKET_CONF.COMM_POS_DEST] = dest; CMD[(int)PACKET_CONF.COMM_POS_SEQID] = 0x01; CMD[(int)PACKET_CONF.COMM_POS_LEN] = (byte)(payload.Length >> 8 & 0x00FF); CMD[(int)PACKET_CONF.COMM_POS_LEN + 1] = (byte)(payload.Length); //payload is at least 2 bytes - command for (int i = 0; i < payload.Length; i++) { CMD[(int)PACKET_CONF.COMM_POS_PAYLOAD + i] = payload[i]; } //Calcurate CRC UInt16 calcurateCRC = UpdateCRC16(CMD, (uint)totalPackageLength - (int)PACKET_CONF.COMM_AFTER_PAYLOAD_OVERHEAD); CMD[totalPackageLength - 3] = (byte)((calcurateCRC >> 8) & 0x00FF); CMD[totalPackageLength - 2] = (byte)((calcurateCRC) & 0x00FF); CMD[totalPackageLength - 1] = (byte)PACKET_CONF.COMM_EOF; ret = ERROR_LIST.ERROR_NONE; return(CMD); }
public static byte[] sendCommand(INNCOM_COMMAND_LIST CMD, ref ERROR_LIST err) { return(sendCommand(commandToByteArray(CMD), ref err, (int)Constants.defaultSleep)); }
public static byte[] sendCommand(INNCOM_COMMAND_LIST CMD, ref ERROR_LIST err, int sleepTime) { return(sendCommand(commandToByteArray(CMD), ref err, sleepTime)); }
public static byte[] sendCommand(INNCOM_COMMAND_LIST CMD, byte[] payload, ref ERROR_LIST err) { return(sendCommand(CMD, payload, ref err, (int)Constants.defaultSleep)); }
//public static ResourceManager rm = new ResourceManager("IncomUtility.StringsEng", Assembly.GetExecutingAssembly()); //public static ResourceManager rm = new ResourceManager("IncomUtility.StringsChn", Assembly.GetExecutingAssembly()); public static byte[] sendCommand(byte[] payload, ref ERROR_LIST error, int sleepTime) { if (!SerialPortIO.isPortOpen()) { error = ERROR_LIST.ERROR_PORT_NOT_OPEN; if (isMessageboxWortk) { // MessageBox.Show("Incom is not connected"); MessageBox.Show(rm.GetString("NotConnected")); } return(null); } if (payload == null) { error = ERROR_LIST.ERROR_INPUT_DATA_NONE; return(null); } int retryCount = (int)Constants.retryCount; byte[] u8TXbuffer = null; byte[] u8RXbuffer = null; SerialPortIO.mutex.WaitOne(); while (retryCount-- > 0) { SerialPortIO.flushIOBuffer(); u8TXbuffer = buildCMDPacket((byte)PACKET_CONF.COMM_SYSTEM_MFG_PC, (byte)PACKET_CONF.COMM_SYSTEM_INCOM, payload, ref error); if (error != ERROR_LIST.ERROR_NONE) { continue; } SerialPortIO.writePacket(ref u8TXbuffer, ref error); if (error != ERROR_LIST.ERROR_NONE) { continue; } Thread.Sleep(sleepTime); u8RXbuffer = SerialPortIO.readPacket(ref error); if (error != ERROR_LIST.ERROR_NONE) { continue; } error = validateRXPacket(u8RXbuffer); if (error == ERROR_LIST.ERROR_NONE) { break; } } if (APP.APP_UI_CommLog.packetCheck == true) { MainWindow.winCommLog.setText(u8TXbuffer, u8RXbuffer); } SerialPortIO.flushIOBuffer(); SerialPortIO.mutex.ReleaseMutex(); return(u8RXbuffer); }