private int readBitVal(string name, int startAddress, int size, ref short[] values) { byte[] sendDataByte; byte[] recvDataByte = new byte[4999]; try { if (string.IsNullOrEmpty(name)) { throw new Exception("Device name can not be empty!"); } int address = startAddress; int readAddress = address; string readString = ""; do { string command = McCommand.CreateCommand(CommandType.batchRead, SubCommandType.bit, name, readAddress.ToString(), size); readAddress += McCommand.MAX_BIT_RW_POINT; sendDataByte = Encoding.ASCII.GetBytes(command); socketTCP.Send(sendDataByte, sendDataByte.Length, SocketFlags.None); int byteRead = socketTCP.Receive(recvDataByte, recvDataByte.Length, SocketFlags.None); if (byteRead > 0) { string recvStr = Encoding.ASCII.GetString(recvDataByte, 0, byteRead); string completeCode = McCommand.GetCompleteCode(recvStr); if (completeCode != "0000") { return(Convert.ToInt32(completeCode, 16)); } readString += recvStr.Substring(22); } else { return(-1); } } while (readAddress < (address + size)); values = Enumerable.Range(0, size).Select(x => short.Parse(readString[x].ToString())).ToArray(); return(0); } catch (Exception ex) { throw ex; } }
private int writeBitVal(string name, int startAddress, int size, short[] writeValues) { byte[] writeByte; byte[] recvDataByte = new byte[4999]; try { if (string.IsNullOrEmpty(name)) { throw new Exception("Device name can not be empty!"); } int address = startAddress; var writeAry = split(writeValues, McCommand.MAX_BIT_RW_POINT); var qArray = writeAry.Select((val, idx) => new { Index = idx, Value = val }); //產生index foreach (var wAry in qArray) { string writeAddress = (address + (wAry.Index * McCommand.MAX_BIT_RW_POINT)).ToString(); string command = McCommand.CreateCommand(CommandType.batchWrite, SubCommandType.bit, name, writeAddress, wAry.Value.Count(), wAry.Value.ToArray()); writeByte = Encoding.ASCII.GetBytes(command); socketTCP.Send(writeByte, writeByte.Length, SocketFlags.None); int returnBytes = socketTCP.Receive(recvDataByte, recvDataByte.Length, SocketFlags.None); if (returnBytes > 0) { string recvStr = Encoding.ASCII.GetString(recvDataByte, 0, returnBytes); string completeCode = McCommand.GetCompleteCode(recvStr); if (completeCode != "0000") { return(Convert.ToInt32(completeCode, 16)); } } else { return(-1); } } return(0); } catch (Exception ex) { throw ex; } }
private int readWordVal(string name, int startAddress, int size, ref short[] values) { byte[] sendDataBytes; byte[] recvDataBytes = new byte[4999]; try { int address = startAddress; int readAddress = address; string readString = ""; do { string command = McCommand.CreateCommand(CommandType.batchRead, SubCommandType.word, name, readAddress.ToString(), size); readAddress += McCommand.MAX_WORD_RW_POINT; sendDataBytes = Encoding.ASCII.GetBytes(command); socketTCP.Send(sendDataBytes, sendDataBytes.Length, SocketFlags.None); int byteRead = socketTCP.Receive(recvDataBytes, recvDataBytes.Length, SocketFlags.None); if (byteRead > 0) { string recvStr = Encoding.ASCII.GetString(recvDataBytes, 0, byteRead); string completeCOde = McCommand.GetCompleteCode(recvStr); if (completeCOde != "0000") { return(Convert.ToInt32(completeCOde, 16)); } readString += recvStr.Substring(22); } else { return(-1); } } while (readAddress < (address + size)); values = McCommand.HEXStringToShortArray(readString); return(0); } catch (Exception ex) { Console.WriteLine(string.Format("Read word value fail. Socket disconnect. Exception: {0}", ex)); return(-1); } }
private int writeWordVal(string name, int startAddress, int size, short[] writeValues) { byte[] writeDataBytes; byte[] recvDataBytes = new byte[4999]; try { var writeArray = split(writeValues, McCommand.MAX_WORD_RW_POINT); var qArray = writeArray.Select((val, idx) => new { Index = idx, Value = val });//產生index foreach (var wAry in qArray) { string writeAddress = (startAddress + (wAry.Index * McCommand.MAX_WORD_RW_POINT)).ToString(); string command = McCommand.CreateCommand(CommandType.batchWrite, SubCommandType.word, name, writeAddress, wAry.Value.Count(), wAry.Value.ToArray()); writeDataBytes = Encoding.ASCII.GetBytes(command); socketTCP.Send(writeDataBytes, writeDataBytes.Length, SocketFlags.None); int returnBytes = socketTCP.Receive(recvDataBytes, recvDataBytes.Length, SocketFlags.None); if (returnBytes > 0) { string recvStr = Encoding.ASCII.GetString(recvDataBytes, 0, returnBytes); string completeCode = McCommand.GetCompleteCode(recvStr); if (completeCode != "0000") { return(Convert.ToInt32(completeCode, 16)); } } else { return(-1); } } return(0); } catch (Exception ex) { Console.WriteLine(string.Format("Write word value fail. Socket disconnect. Exception: {0}", ex)); return(-1); } }