Menu

Problems when writing values that are not bit

Help
GerardoPG
2008-06-30
2013-05-09
  • GerardoPG

    GerardoPG - 2008-06-30

    Hi,
    Now i'm able to read & write bits: no problem!.

    But, I can't write values to the PLC. I don't know what i'm doing wrong. I'm programming in .net 2.0 + C#.

    Ok, I'm tetsting writin to the following directions:
    1) DB4.DBW2 and the value is a simple 4.
    2) DB6.DBD4 and the value is a simple 7.

    Using my function it's like this:
    WriteToPLC(4,2,2,"4");
    WriteToPLC(6,4,4,"7");

    My functions is like this one:

    public void WriteToPLC(int DBNumber, int Start, int Len, string Data) {
      int res = 0;
      try {
        int a = Convert.ToInt32(Data);
        byte[] aa = BitConverter.GetBytes(libnodave.daveSwapIed_32(a));
        res = dc.writeBytes(libnodave.daveDB, DBNumber, Start, Len, ada);
        if (res != 0) {
          throw new Exception(string.Format("[{0}] [Exception] Error writing to PLC IP: {1} DB: {2} Start: {3} Len: {4} Data: {5} libnodaveError: {6}", System.Reflection.MethodBase.GetCurrentMethod().Name, this._PLC_IP, DBNumber, Start, Len, Data, libnodave.daveStrerror(res)));
        }
      }
      catch (Exception ex) {
         // code for catch
      }
    }

    Can anybody help me about what's wrong?? thanks in advance!!

    GerardoPG.

     
    • GerardoPG

      GerardoPG - 2008-06-30

      OK buddys,
      My problem now is shorter than before... I'm just having problems when reading & writing to DWord or floating numbers...

      the problem is that libnodave responses res = 0, but the values weren't written. Any trick-tip-help??

      thanks in advance!!

      switch (TheValues.TagTyp)  // tag type, is a flagtype
      {
          case 3: // word --->>> OK!!!
              res = dc.writeBytes(libnodave.daveDB, TheValues.DBNumber, TheValues.Start, TheValues.Len, BitConverter.GetBytes(libnodave.daveSwapIed_16(Convert.ToInt16(TheValues.Data.Trim()))));
              break;
          case 5: // DWord ---->>> N-OK!!
              float swapVal = libnodave.toPLCfloat(float.Parse(TheValues.Data));
              byte[] bytes = BitConverter.GetBytes(swapVal);
              res = dc.writeBytes(libnodave.daveDB, TheValues.DBNumber, TheValues.Start, TheValues.Len, bytes);
              break;
          default:
              res = -5;
              break;
      }

      -----
      GerardoPG

       
    • afk

      afk - 2008-07-01

      Do you want to write a DWord (Int32 or UInt32) or a Float to the PLC ?

      Axel

       
      • GerardoPG

        GerardoPG - 2008-07-02

        Hi Axel,
        First, thanks again for your help. You always helped me in the past and now.

        Well, I've resolved my problem, and yes: when trying to write Word, DWord and Float to the PLC. I don't why my solution works, I hope you can help me understand a bit better.

        I have a funtion named "WriteToPLC", this write data that it's not Bit type (I use another function for that purposes). This is the code:
        public bool WriteToPLC(SWriteS7Data TheValues) {
            int res = 0;
            try {
                /*
                    "TheValues.TagTyp" could be any of the next data column "Tp":
                    Tp  Desc        Length
                    1   Byte        1        
                    2   Bit         1        
                    3   Word        2        
                    4   Long/Float  4        
                    5   DWord       4        
                    6   int         2        
                    7   Dint        4
                 */

                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 5: // DWord
                        float swapVal1 = float.Parse(TheValues.Data);
                        //////float swapVal = libnodave.toPLCfloat(0.0f + swapVal1 + 0.0f);
                        byte[] bytes = BitConverter.GetBytes(libnodave.toPLCfloat(0.0f + swapVal1 + 0.0f));
                        byte t1, t2, t3, t4;
                        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;
                    default:
                        res = -5;
                        break;
                }

                if (res != 0) {
                    throw new Exception(string.Format("Error al intentar escribir: [{0}] libnodaveError: {1}", TheValues.ToString(), libnodave.daveStrerror(res)));
                }
            }
            catch (Exception ex) {
                Trace.WriteLine(string.Format("[{0}] [Exception] Error writing to PLC IP: {1} ExMsg: {2}", System.Reflection.MethodBase.GetCurrentMethod().Name, this._PLC_IP, ex.Message));
                return false;
            }
            return true;
        }

        You can see in my code that when I write a value of type 5 ("DWord") too I can write a "Float" value. Well, first I use the recommended method for write (get the value in a data type correctly, next use the libnodave.toPLCfloat function and get the bytes. But, finally I always have to, again, swap the values.

        If I don't do that no values are written to the PLC. So, this code is actually running for me. And as you can see, I can write too values for Int16 (Word) with no problems.

        Can you, Axel, help understand why this code works?

        And again, thanks for your support!

        Gerardo.

         
        • afk

          afk - 2008-07-02

          I'm not shure if I really understand what you want to do in your code. As I understand you want to write values of type Word (Tp=3), Float (Tp=4) and DWord (TP=5), but your code handles only with Tp=3 for Words and Tp=5 for Floats, and the second part is wrong in my opinion. I think for Tp=5 (DWords) you should use a similar code as for Words, but with "libnodave.daveSwapIed_32(Convert.ToInt32".

          I think your code for Floats is working because BitConverter.GetBytes seems to swap the bytes also, thus you have to swap the bytes afterwards again, but I think you should change your case-condition to Tp=4.

          Axel

           

Anonymous
Anonymous

Add attachments
Cancel