Menu

LibNoDave with vb.net

Help
2009-06-25
2013-05-09
  • Nobody/Anonymous

    Has anyone used LibNoDave with VB.net.
    I have added LibNoDave.net.dll in my references but it does not find function references for opensocket function.

     
    • GerardoPG

      GerardoPG - 2009-06-25

      Hi,
      We've been using a while this great piece of code; it's amazing. I don't know exactly you error, we have never had it. But let me try to explain you how we work with it.

      1) we create a new solution, we create a new solution's project called "libnodave2", in this we only have the wraper code inside. We command that the final assembly (libnodave2.dll) will be generated in a new and specific folder that will be called "BIN"; so, all my new assemblys will be put it there. Also we have copied the libnodave.dll... at this point we only have libnodave.dll and libnodave2.dll in the "BIN" folder.

      2) we create a second project inside the solution. In this I command again the final assembly must be generated in the "BIN" folder. I add a reference to the "libnodave2.dll", now I can use the libnodave.dll power.

      3) we program the things to configure the initial configuration (IP address, rack, slot and port)
                  this._PLC_IP = PLCIP; this._PLCPort = PLCPort;
                  this._PLCRack = PLCRack; this._PLCSlot = PLCSlot;

      4) we starts the libnodave connection configuration.
                  if (!_PLCConnected) {
                      try {
                          fds = new libnodave.daveOSserialType();
                          fds.rfd = libnodave.openSocket(this._PLCPort, this._PLC_IP);
                          fds.wfd = fds.rfd;
                          int res = 0;
                          if (fds.rfd > 0) {
                              di = new libnodave.daveInterface(fds, "PLCS7VIA", 0, libnodave.daveProtoISOTCP, libnodave.daveSpeed187k);
                              di.setTimeout(10000);
                              dc = new libnodave.daveConnection(di, 0, this._PLCRack, this._PLCSlot);
                              res = dc.connectPLC();
                              if (res.Equals(0)) {
                                  this._PLCConnected = true;
                                  Trace.WriteLine(string.Format("[{0}] Connected to PLC {1}", System.Reflection.MethodBase.GetCurrentMethod().Name, this._PLC_IP));
                              }
                              else {
                                  dc.disconnectPLC();
                                  libnodave.closePort(fds.rfd);
                                  Trace.WriteLine(string.Format("[{0}] Error trying to Connect with PLC {1}; daveError: {2}", System.Reflection.MethodBase.GetCurrentMethod().Name, this._PLC_IP, libnodave.daveStrerror(res)));
                              }
                          }
                          else {
                              Trace.WriteLine(string.Format("[{0}] [ERROR] Couln't open TCP connection to {1}", System.Reflection.MethodBase.GetCurrentMethod().Name, this._PLC_IP));
                          }
                      }
                      catch (Exception ex) {
                          Trace.WriteLine(string.Format("[{0}] [Exception] PLC_IP: [{1}] [ERRORDesc]: {2}", System.Reflection.MethodBase.GetCurrentMethod().Name, this._PLC_IP, ex.Message));
                      }
                  }

      5) I have made some structures to encapsulate and reuse the instructions for read simple and bit data, another struc for receive data, and another to write. This mean: I just pass the tag value (DB100.DBD200 or DB200.DBX100.2) and the structures discovers in it's the constructor exactly the things about len, start byte write, DB, etc, etc. This really helped me a lot.

      --- For me this means "read a tag", a create an special structure to instruct how the read should be:
                      if (!ReadData._TagTyp_ID.Equals(2)) {
                          res = dc.readBytes(libnodave.daveDB, ReadInst._DBNumber, ReadInst._Start, ReadInst._Len, null);
                      }

                      if (res != 0) {
                          this._PLCConnected = false;
                          throw new Exception(string.Format("Error ejecuting the reading instruction for values: [{0}] libnodaveError: {1}", ReadInst.ToString(), libnodave.daveStrerror(res)));
                      }
                      else if (res == 0) {
                          // a var to store the retrieved value
                          string theValue = string.Empty;
                          // read values according to the ReadData_YourTag_ID
                          switch (ReadData._TagTyp_ID) {
                              case (int)S7DataTypes.Byte:                                          //1             Byte
                                  theValue = dc.getU8At(ReadData._RealTagAddress).ToString();
                                  break;
                              case (int)S7DataTypes.Bit:                                           //2             Bit
                                  int Temp = dc.getU8At(ReadData._RealTagAddress);
                                  byte byteValue = Convert.ToByte(Temp);
                                  byte bytePosition = Convert.ToByte(ReadData._RealTagBit);
                                  bool BitStatus = ((byteValue & bytePosition) != 0);
                                  if (BitStatus)
                                      theValue = "1";
                                  else
                                      theValue = "0";
                                  break;
                              case (int)S7DataTypes.Word:                                          //3             Word
                                  theValue = dc.getU16().ToString();
                                  //theValue = dc.getU16At(ReadData._RealTagAddress).ToString();
                                  break;
                              case (int)S7DataTypes.Long_Float:                                     //4             Long/Float
                                  //theValue = dc.getFloatAt(ReadData._RealTagAddress).ToString();
                                  theValue = dc.getFloat().ToString();
                                  break;
                              case (int)S7DataTypes.DWord:                                         //5             DWord

                                  theValue = dc.getU32().ToString();
                                  //theValue = dc.getS32().ToString();

                                  //theValue = dc.getU32At(ReadData._RealTagAddress).ToString();
                                  //theValue = dc.getS32At(ReadData._RealTagAddress).ToString();
                                  //theValue = dc.getU32().ToString();
                                  break;
                              case (int)S7DataTypes.Int:                                           //6             int
                                  theValue = dc.getS16At(ReadData._RealTagAddress).ToString();
                                  break;
                              case (int)S7DataTypes.DInt:                                          //7             Dint
                                  theValue = dc.getS32At(ReadData._RealTagAddress).ToString();
                                  break;
                              default:
                                  theValue = "ERROR";
                                  break;
                          }
                          //ReadData[i]._Value = theValue;
                          ReturnValues = new SReceivedData();
                          ReturnValues._YourTag_ID = ReadData._YourTag_ID;
                          ReturnValues._Value = theValue;
      ---

      --- Writing to PLC for me is:
                     byte[] bytes;
                      byte t1, t2, t3, t4;

                      if (!this.PLCConnected) this.ConnectToPLC();

                      switch (TheValues.TagTyp) {
                          case 3: // word
                              res = dc.writeBytes(libnodave.daveDB, TheValues.DBNumber, TheValues.Start, TheValues.Len, BitConverter.GetBytes(libnodave.daveSwapIed_16(Convert.ToInt16(TheValues.Data.Trim()))));
                              break;
                          case 4: // Float
                              float swapVal1 = float.Parse(TheValues.Data);
                              //float swapVal = libnodave.toPLCfloat(0.0f + swapVal1 + 0.0f);
                              bytes = BitConverter.GetBytes(libnodave.toPLCfloat(0.0f + swapVal1 + 0.0f));
                              t1 = bytes[0]; t2 = bytes[1]; t3 = bytes[2]; t4 = bytes[3];
                              bytes[0] = t4; bytes[1] = t3; bytes[2] = t2; bytes[3] = t1;
                              res = dc.writeBytes(libnodave.daveDB, TheValues.DBNumber, TheValues.Start, TheValues.Len, bytes);
                              break;
                          case 5: // DWord
                               res = dc.writeBytes(libnodave.daveDB, TheValues.DBNumber, TheValues.Start, TheValues.Len, BitConverter.GetBytes(libnodave.daveSwapIed_32(Convert.ToInt32(TheValues.Data.Trim()))));
                              break;
                          default:
                              res = -5;
                              break;
                      }

                      if (res != 0) {
                          this._PLCConnected = false;
                          throw new Exception(string.Format("Error al intentar escribir: [{0}] libnodaveError: {1}", TheValues.ToString(), libnodave.daveStrerror(res)));
                      }
      ---

      Hope this help you.
      gPg

       

Anonymous
Anonymous

Add attachments
Cancel