簡體   English   中英

需要幫助顯示DLL文件中的值

[英]Need help displaying values from a DLL file

這是整個文件。 我試圖了解如何顯示來自ODBAXIS的信息。 此代碼在第130行有一個錯誤代碼。

/*
 * 
 * */
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)
    {

    }


}

}

我看到這有點舊,但似乎沒有答案。 也許這會在將來對某人有所幫助:

我在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”。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM