2013-10-08 56 views
0

這是整個文件。我試圖瞭解如何顯示來自ODBAXIS的信息。此代碼對線路130需要幫助顯示來自一個DLL文件的值

/* 
* 
* */ 
using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.Runtime.InteropServices; 


namespace SampleFOCAS2Projtest1 
{ 

public partial class frmMacroVars : Form 
{ 

    bool iConnected = false; 
    short hndl; 
    //ushort Flibhndl; 

    //Read a macro variable 
    [DllImport("fwlib32")] 
    private static extern short cnc_rdmacro(short hndl, short number, short length, ref ODBM c); 


    //[DllImport("FWLIB32")] 
    // private static extern short cnc_absolute(short hndl, short number, short length, ref ODBAXIS c); 


    /* read absolute axis position */ 
    [DllImport("FWLIB32.dll", EntryPoint = "cnc_absolute")] 
    public static extern short cnc_absolute(short hndl, 
    short a, short b, [Out, MarshalAs(UnmanagedType.LPStruct)] ODBAXIS c); 

    // In the FOCAS2 Functions, long value types are equivalent to Int32 types in .NET 
    // Write a macro variable 
    [DllImport("fwlib32")] 
    private static extern short cnc_wrmacro(short hndl, short number, short length, int mcr_val, short dec_val); 

    //Once again, specify layout of the structure for communication with unmanaged DLL 
    [StructLayout(LayoutKind.Sequential, Pack = 4)] 
    public struct ODBM 
    { 
     public short datano;  /* custom macro variable number */ 
     public short dummy;  /* (not used) */ 
     public int mcr_val;  /* value of custom macro variable */ 
     public short dec_val;  /* number of places of decimals */ 

    } 

    /* cnc_absolute:read absolute axis position */ 
    /* cnc_machine:read machine axis position */ 
    /* cnc_relative:read relative axis position */ 
    /* cnc_distance:read distance to go */ 
    /* cnc_skip:read skip position */ 
    /* cnc_srvdelay:read servo delay value */ 
    /* cnc_accdecdly:read acceleration/deceleration delay value */ 
    /* cnc_absolute2:read absolute axis position 2 */ 
    /* cnc_relative2:read relative axis position 2 */ 
    [StructLayout(LayoutKind.Sequential, Pack = 4)] 
    public class ODBAXIS 
    { 
     public short dummy; /* dummy */ 
     public short type; /* axis number */ 
     [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)] 
     public int[] data;  /* data value */ 




    } 



    //Constructor 
    public frmMacroVars(bool iConnect, short handle) 
    { 
     InitializeComponent(); 
     this.iConnected = iConnect; 
     this.hndl = handle; 

    } 
    //Form Load Event 
    private void frmMacroVars_Load(object sender, EventArgs e) 
    { 
     if (iConnected) 
      lblConnected.Text = "You ARE connected"; 
     else 
      lblConnected.Text = "You are NOT connected"; 
    } 

    //Back Button 
    private void cmdBack_Click(object sender, EventArgs e) 
    { 
     //Close this form and return to main form 
     this.Close(); 
    } 

    private void tmrGetMacros_Tick(object sender, EventArgs e) 
    { 
     ODBM odb = new ODBM(); 
     short odbmSize = 10;   //This value will always be 10. It is the size of the struct 
     short retCode = -1;    //The return value of our FOCAS 2 Calls 


     ODBAXIS odba = new ODBAXIS(); //Added for reading the X axis 
     short retCodee = -1; 
     // int outcome = 0;  // interger needed for converting short to string 
     // int test1 = 10; 
     //int test2 = 10; 


     //Only get the values if we are connected 
     if (iConnected) 
     { 
      try 
      { 
       //Get the contents of macro variable 500 and put in ODBM struct 
       //This requires our handle to the control 
       retCode = cnc_rdmacro(hndl, 500, odbmSize, ref odb); 

一個錯誤代碼,代碼的這部分工作在那裏我可以正確地接收來自rdmacro的信息。

   if (retCode == 0) 
        txtMac500.Text = InsertDecimal(Convert.ToInt32(odb.mcr_val), Convert.ToInt16(odb.dec_val)); 

這部分代碼會產生錯誤。 (hndl,1,1,ref odba)不是正確的格式。不知道是什麼。

   retCodee = cnc_absolute(hndl, 1,1, ref odba); 


        textBox1.Text = retCodee.ToString(); 

      } 
      catch (Exception) { } 
     } 
    } 


    //Inserts a decimal place in an integer and returns the string of it 
    //This is necessary because we do not get the actual macro variable value back, only the digits and a value that 
    //specifies where the decimal place is, so the number must be created manually 
    public string InsertDecimal(int value, short decSpot) 
    { 
     //This takes in an integer 32 and a sport for a decimal and returns a string of an offset value. The decimal 
     //point here is represented by a SHORT 

     string strValue = value.ToString(); 

     if (decSpot != -1) 
      strValue = (value/Math.Pow(10, decSpot)).ToString(); 
     else 
      strValue = value.ToString(); 

     return strValue; 
    } 

    //Update Macro Variable 110 with contents of Textbox 
    private void cmdUpdate_Click(object sender, EventArgs e) 
    { 
     string newMacVar = txtWrite110.Text; 
     short decIndex = 0; 
     int numVal = 0; 
     short structLen = 10;  //It is always 10 in length 
     short macNum = 110;   //We are writing to mac var 110 

     if (!iConnected) 
      MessageBox.Show("You are not connected to the control.", "Not Connected"); 
     else 
     { 
      try 
      { 
       if (txtWrite110.Text.Length > 0) 
       { 
        decIndex = (short)newMacVar.IndexOf('.'); 

        if (decIndex <= 0) 
         decIndex = 0; 
        else 
         newMacVar = newMacVar.Remove(decIndex, 1); 

        numVal = Convert.ToInt32(newMacVar); 

        //Write our textbox input to macro variable 110 
        cnc_wrmacro(hndl, macNum, structLen, numVal, decIndex); 
        txtWrite110.Text = string.Empty; 
       } 
       else 
       { 
        MessageBox.Show("Type a number into the Write 'Macro Variable textbox'", "No Input"); 
       } 
      } 
      catch (Exception ex) 
      { 
       MessageBox.Show("There was an error. Check your input. Error: " + ex.Message, "Error"); 
      } 
     } 
    } 

    private void txtMac110_TextChanged(object sender, EventArgs e) 
    { 

    } 


} 

} 
+0

關於如何請求axis1的信息以告訴您如何顯示它的信息不足。 –

+0

'軸1'在哪裏? – edtheprogrammerguy

+0

@edtheprogrammerguy我相信'ODBAXIS.type == 1' –

回答

0

我看到這有點舊,但似乎沒有答案。也許這將幫助別人的未來:

我這樣做是在VB中像這樣:

Dim AxData As New ODBAXIS 

    Dim DataLen As Short 
    DataLen = 4 + 4 * MAX_AXIS 


    LastError = cnc_absolute(Hndl, -1, DataLen, AxData) 

參數3是將被檢索的數據的長度。 「1」不是有效的長度。
此外,我正在使用「1」,在我使用「-1」的情況下,我一次讀取所有軸數據。如果您只想讀取第一軸的數據,那麼就像您所做的那樣使用「1」。