这是整个文件。我试图了解如何显示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)
{
}
}
}
答案 0 :(得分: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”。