首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >需要帮助显示DLL文件中的值

需要帮助显示DLL文件中的值
EN

Stack Overflow用户
提问于 2013-10-09 02:19:41
回答 1查看 757关注 0票数 0

这是整个文件。我正在尝试了解如何显示来自ODBAXIS的信息。此代码在第130行上有一个错误代码。

代码语言:javascript
复制
/*
 * 
 * */
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);

代码的这一部分在我可以正确地从rd宏接收信息的地方工作。

代码语言:javascript
复制
                if (retCode == 0)
                    txtMac500.Text = InsertDecimal(Convert.ToInt32(odb.mcr_val), Convert.ToInt16(odb.dec_val));

这部分代码会产生一个错误。(hndl,1,1,ref odba)格式不正确。不知道什么才是。

代码语言:javascript
复制
                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)
    {

    }


}

}
EN

回答 1

Stack Overflow用户

发布于 2017-03-07 17:08:23

我知道这有点老了,但似乎没人回答。也许这会在未来对某些人有所帮助:

我在VB中是这样做的:

代码语言:javascript
复制
    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“。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19255336

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档