1

我将带有 RJ45 电缆的 BlueBox RFID 阅读器插入我的 PC。

我想在 node.js 中与他交流,因为我构建了一个 Web 应用程序来读取 RFID 标签。

使用 RFID 阅读器,我有一个文档、一个库(带有 .h、.dll)和一个用于连接到 RFID 阅读器和读取标签的软件。我也有软件的源代码。(在 C# 中)。我对做我的网络应用程序很有启发。

由于该软件使用 C#,我决定在我的 node.js 应用程序中实现 Edge.js,以便在 Node.js 代码中运行 C# 代码。它完美地工作。

因此,我在 node.js 中运行 c# 代码,在此,我在我拥有并使用它的 DLL 库中有调用函数。与示例软件相同。

但是,有一刻,我有一个我不明白的错误。

这是我的 node.js 代码(我已将我的错误放在代码中,在 "SetChannel" 之后的评论中:

var edge = require('edge'); 
var helloWorld = edge.func(function () {/*

using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using System.Net;

class Startup
{
  [DllImport("BLUEBOXLib.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
  static extern int BLUEBOX_Init(out int Handle);

  [DllImport("BLUEBOXLib.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
  static extern int BLUEBOX_SetAddress(ref int Handle, byte Address);

  [DllImport("BLUEBOXLib.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
  static extern int BLUEBOX_Close(ref int Handle);

  [DllImport("BLUEBOXLib.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
  static extern int BLUEBOX_End(ref int Handle);

  [DllImport("BLUEBOXLib.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
  static extern int BLUEBOX_SetChannel(ref int Handle, String Channel, String Settings);

  public async Task<object> Invoke(dynamic input)
  {
    return await Task<object>.Run(() => {
        byte Address = Convert.ToByte(255);
        IPAddress Ip = IPAddress.Parse("192.168.4.200");
        UInt16 Port = Convert.ToUInt16(3000);
        int Handle;
        int Err = 0;

        Err = Startup.BLUEBOX_Init(out Handle);

        // check library initialization
        if (Err != 0)
        {
            Console.WriteLine("BLUEBOXLib.dll: Library load error!");

            Startup.BLUEBOX_Close(ref Handle);
            Startup.BLUEBOX_End(ref Handle);

            return "false";
        } else {

            Err = Startup.BLUEBOX_SetAddress(ref Handle, Address);

            if (Err == 0)
            {
                // create the settings string to pass to library
                System.String strSettings = "192.168.4.200:3000,60000";

                Err = Startup.BLUEBOX_SetChannel(ref Handle, "TCP", strSettings);

                //ERROR : Err = -2 : Error -2 = InvalidHandle
                // With Console.WriteLine, I Wrote Handle at different moment        
                //of the code and it's always the same int...
            }

            if (Err != 0)
            {

                Startup.BLUEBOX_Close(ref Handle);
                Startup.BLUEBOX_End(ref Handle);

                return "false";

            } else {

            // SOME CODE
            }
        }
return "true";
    });
  }
}

*/});

helloWorld({test:1},function(error,result){
    console.log("ERROR : "+error);
    console.log("RESULT : "+result);
});

为了让您理解,这里是我用作示例的软件代码:

这是接口写:

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace BLUEBOX_Polling
{
class BLUEBOXLibClass_x64:BLUEBOXLibInterface
{
    [DllImport("C:/Program Files (x86)/Soltec Soluzioni Tecnologiche/BLUEBOX SDK/BLUEBOX Polling/Source/x64/BLUEBOXLib.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
    static extern int BLUEBOX_GetSwRelease(System.Text.StringBuilder SwRel);

    [DllImport("C:/Program Files (x86)/Soltec Soluzioni Tecnologiche/BLUEBOX SDK/BLUEBOX Polling/Source/x64/BLUEBOXLib.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
    static extern int BLUEBOX_Init(out int Handle);

    [DllImport("C:/Program Files (x86)/Soltec Soluzioni Tecnologiche/BLUEBOX SDK/BLUEBOX Polling/Source/x64/BLUEBOXLib.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
    static extern int BLUEBOX_End(ref int Handle);

    [DllImport("C:/Program Files (x86)/Soltec Soluzioni Tecnologiche/BLUEBOX SDK/BLUEBOX Polling/Source/x64/BLUEBOXLib.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
    static extern int BLUEBOX_SetAddress(ref int Handle, byte Address);

    [DllImport("C:/Program Files (x86)/Soltec Soluzioni Tecnologiche/BLUEBOX SDK/BLUEBOX Polling/Source/x64/BLUEBOXLib.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
    static extern int BLUEBOX_SetChannel(ref int Handle, String Channel, String Settings);

    [DllImport("C:/Program Files (x86)/Soltec Soluzioni Tecnologiche/BLUEBOX SDK/BLUEBOX Polling/Source/x64/BLUEBOXLib.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
    static extern int BLUEBOX_Open(ref int Handle);

    [DllImport("C:/Program Files (x86)/Soltec Soluzioni Tecnologiche/BLUEBOX SDK/BLUEBOX Polling/Source/x64/BLUEBOXLib.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
    static extern int BLUEBOX_Close(ref int Handle);

    [DllImport("C:/Program Files (x86)/Soltec Soluzioni Tecnologiche/BLUEBOX SDK/BLUEBOX Polling/Source/x64/BLUEBOXLib.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
    static extern int BLUEBOX_GetFwRelease(ref int Handle, int Reader, System.Text.StringBuilder FwRel);

    [DllImport("C:/Program Files (x86)/Soltec Soluzioni Tecnologiche/BLUEBOX SDK/BLUEBOX Polling/Source/x64/BLUEBOXLib.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
    static extern int BLUEBOX_ReadParameters(ref int Handle, byte[] Parameters);

    [StructLayout(LayoutKind.Sequential)]
    public struct BLUEBOX_Tag
    {
        internal int TagType;
        internal IntPtr Id;
        internal int Length;
        internal int Antenna;
        internal int Input;
    };

    [DllImport("C:/Program Files (x86)/Soltec Soluzioni Tecnologiche/BLUEBOX SDK/BLUEBOX Polling/Source/x64/BLUEBOXLib.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
    static extern int BLUEBOX_DataRequest(ref int Handle, out IntPtr Tags, out int TagsNo);

    [DllImport("C:/Program Files (x86)/Soltec Soluzioni Tecnologiche/BLUEBOX SDK/BLUEBOX Polling/Source/x64/BLUEBOXLib.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
    static extern int BLUEBOX_FreeTagsMemory(ref int Handle, ref IntPtr Tags, int TagsNo);

    public int GetSwRelease(System.Text.StringBuilder SwRel)
    {
        return BLUEBOX_GetSwRelease(SwRel);
    }

    public int Init(out int Handle)
    {

        return BLUEBOX_Init(out Handle);
    }

    public int End(ref int Handle)
    {
        return BLUEBOX_End(ref Handle);
    }

    public int SetAddress(ref int Handle, byte Address)
    {
        Console.WriteLine("Before return address :" + Handle);
        return BLUEBOX_SetAddress(ref Handle, Address);
    }

    public int SetChannel(ref int Handle, String Channel, String Settings)
    {
        Console.WriteLine("Before return channel:" + Handle);
        return BLUEBOX_SetChannel(ref Handle, Channel, Settings);
    }

    public int Open(ref int Handle)
    {

        return BLUEBOX_Open(ref Handle);
    }

    public int Close(ref int Handle)
    {
        return BLUEBOX_Close(ref Handle);
    }

    public int GetFwRelease(ref int Handle, int Reader, System.Text.StringBuilder FwRel)
    {
        return BLUEBOX_GetFwRelease(ref Handle, Reader, FwRel);
    }

    public int ReadParameters(ref int Handle, byte[] Parameters)
    {
        return BLUEBOX_ReadParameters(ref Handle, Parameters);
    }

    public int DataRequest(ref int Handle, out IntPtr Tags, out int TagsNo)
    {
        return BLUEBOX_DataRequest(ref Handle, out Tags, out TagsNo);
    }

    public int FreeTagsMemory(ref int Handle, ref IntPtr Tags, int TagsNo)
    {
        return BLUEBOX_FreeTagsMemory(ref Handle, ref Tags, TagsNo);
    }
}
}

以及与 RFID 阅读器打开连接的代码(这是这样的视图:)在此处输入图像描述

private void StartButton_Click(object sender, EventArgs e)
    {
        if (this.DeviceDataGridView.RowCount > 1)
        {
            int Err = 0, RowIndex;
            byte Address;

            for (RowIndex = 0; RowIndex < this.DeviceDataGridView.RowCount - 1; RowIndex++)
            {
                // ok, there are devices in table
                try
                {
                    Address = Convert.ToByte(this.DeviceDataGridView[this.AddressColumn.Index, RowIndex].Value);
                    Console.WriteLine("Address : " + Address);
                }
                catch
                {
                    MessageBox.Show("Type a valid device address (0 - 255)!", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Stop);

                    // end all the library handles created
                    for (int Index = 0; Index < RowIndex; Index++)
                    {
                        BLUEBOXLib.Close(ref HandleArray[Index]);
                        BLUEBOXLib.End(ref HandleArray[Index]);
                    }
                    return;
                }

                // check the ip address in the data grid view
                try
                {
                    IPAddress Ip = IPAddress.Parse(this.DeviceDataGridView[this.IpColumn.Index, RowIndex].Value.ToString());
                }
                catch
                {
                    MessageBox.Show("Type a valid IP address!", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Stop);

                    // end all the library handles created
                    for (int Index = 0; Index < RowIndex; Index++)
                    {
                        BLUEBOXLib.Close(ref HandleArray[Index]);
                        BLUEBOXLib.End(ref HandleArray[Index]);
                    }
                    return;
                }

                // check the port
                try
                {
                    UInt16 Port = Convert.ToUInt16(this.DeviceDataGridView[this.PortColumn.Index, RowIndex].Value);
                }
                catch
                {
                    MessageBox.Show("Type a valid port number (1 - 65535)!", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Stop);

                    // end all the library handles created
                    for (int Index = 0; Index < RowIndex; Index++)
                    {
                        BLUEBOXLib.Close(ref HandleArray[Index]);
                        BLUEBOXLib.End(ref HandleArray[Index]);
                    }
                    return;
                }

                try
                {
                    // resize the handle array
                    Array.Resize(ref HandleArray, HandleArray.Length + 1);
                    // try to init. the library
                    Err = BLUEBOXLib.Init(out HandleArray[RowIndex]);
                    Console.WriteLine("Result Init : " + HandleArray[RowIndex]);

                    // check library initialization
                    if (Err != 0)
                    {
                        MessageBox.Show("BLUEBOXLib.dll: Library load error!", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);

                        // end all the library handles created
                        for (int Index = 0; Index < RowIndex; Index++)
                        {
                            BLUEBOXLib.Close(ref HandleArray[Index]);
                            BLUEBOXLib.End(ref HandleArray[Index]);
                        }
                        return;
                    }
                }
                catch (Exception Ex)
                {
                    MessageBox.Show(Ex.Message + "\n\n" + Ex.ToString(), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);

                    // end all the library handles created
                    for (int Index = 0; Index < RowIndex; Index++)
                    {
                        BLUEBOXLib.Close(ref HandleArray[Index]);
                        BLUEBOXLib.End(ref HandleArray[Index]);
                    }
                    return;
                }

                try
                {
                    // try to set address
                    Err = BLUEBOXLib.SetAddress(ref HandleArray[RowIndex], Address);

                    if (Err == 0)
                    {
                        // create the settings string to pass to library
                        System.String strSettings = this.DeviceDataGridView[this.IpColumn.Index,RowIndex].Value.ToString() + ":" + this.DeviceDataGridView[this.PortColumn.Index,RowIndex].Value.ToString() + ",60000";
                        Console.WriteLine(strSettings);
                        Err = BLUEBOXLib.SetChannel(ref HandleArray[RowIndex], "TCP", strSettings);

                        Console.WriteLine(Err + "Set Channel");
                    }

                    if (Err != 0)
                    {
                        MessageBox.Show("BLUEBOXLib.dll: Library initialization error!", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);

                        // end all the library handles created
                        for (int Index = 0; Index < RowIndex; Index++)
                        {
                            BLUEBOXLib.Close(ref HandleArray[Index]);
                            BLUEBOXLib.End(ref HandleArray[Index]);
                        }

                        // also release this library handle
                        BLUEBOXLib.End(ref HandleArray[RowIndex]);
                        return;
                    }
                }
                catch (Exception Ex)
                {
                    MessageBox.Show(Ex.Message + "\n\n" + Ex.ToString(), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);

                    // end all the library handles created
                    for (int Index = 0; Index < RowIndex; Index++)
                    {
                        BLUEBOXLib.Close(ref HandleArray[Index]);
                        BLUEBOXLib.End(ref HandleArray[Index]);
                    }

                    // also release this library handle
                    BLUEBOXLib.End(ref HandleArray[RowIndex]);
                    return;
                }

                try
                {
                    // open the connection
                    Err = BLUEBOXLib.Open(ref HandleArray[RowIndex]);

                    if (Err != 0)
                    {
                        MessageBox.Show("BLUEBOXLib.dll: Connection error!", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);

                        // end all the library handles created
                        for (int Index = 0; Index < RowIndex; Index++)
                        {
                            BLUEBOXLib.Close(ref HandleArray[Index]);
                            BLUEBOXLib.End(ref HandleArray[Index]);
                        }

                        // also release this library handle
                        BLUEBOXLib.End(ref HandleArray[RowIndex]);
                        return;
                    }
                }
                catch (Exception Ex)
                {
                    MessageBox.Show(Ex.Message + "\n\n" + Ex.ToString(), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);

                    // end all the library handles created
                    for (int Index = 0; Index < RowIndex; Index++)
                    {
                        BLUEBOXLib.Close(ref HandleArray[Index]);
                        BLUEBOXLib.End(ref HandleArray[Index]);
                    }

                    // also release this library handle
                    BLUEBOXLib.End(ref HandleArray[RowIndex]);
                    return;
                }

                try
                {
                    Thread.Sleep(100);
                    // Read the firmware version.
                    System.Text.StringBuilder FwRel = new StringBuilder(64);

                    Err = BLUEBOXLib.GetFwRelease(ref HandleArray[RowIndex], 0, FwRel);

                    if (Err == 0)
                    {
                        this.DeviceDataGridView[this.FwRelColumn.Index, RowIndex].Value = FwRel.ToString(0, 16);
                    }
                    else
                    {                            
                        MessageBox.Show("BLUEBOXLib.dll: Communication error!", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);

                        // End all the library handles created.
                        for (int Index = 0; Index < RowIndex; Index++)
                        {
                            BLUEBOXLib.Close(ref HandleArray[Index]);
                            BLUEBOXLib.End(ref HandleArray[Index]);
                        }

                        // Also release this library handle.
                        BLUEBOXLib.Close(ref HandleArray[RowIndex]);
                        BLUEBOXLib.End(ref HandleArray[RowIndex]);
                        return;
                    }

                    Thread.Sleep(100);
                    // Read the general parameters.
                    byte[] Params = new byte[7];

                    Err = BLUEBOXLib.ReadParameters(ref HandleArray[RowIndex], Params);

                    if (Err == 0)
                    {
                    }
                    else
                    {
                        MessageBox.Show("BLUEBOXLib.dll: Communication error!", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);

                        // End all the library handles created.
                        for (int Index = 0; Index < RowIndex; Index++)
                        {
                            BLUEBOXLib.Close(ref HandleArray[Index]);
                            BLUEBOXLib.End(ref HandleArray[Index]);
                        }

                        // Also release this library handle.
                        BLUEBOXLib.Close(ref HandleArray[RowIndex]);
                        BLUEBOXLib.End(ref HandleArray[RowIndex]);
                        return;
                    }
                }
                catch (Exception Ex)
                {
                    MessageBox.Show(Ex.Message + "\n\n" + Ex.ToString(), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);

                    // End all the library handles created.
                    for (int Index = 0; Index < RowIndex; Index++)
                    {
                        BLUEBOXLib.Close(ref HandleArray[Index]);
                        BLUEBOXLib.End(ref HandleArray[Index]);
                    }

                    // Also release this library handle.
                    BLUEBOXLib.Close(ref HandleArray[RowIndex]);
                    BLUEBOXLib.End(ref HandleArray[RowIndex]);
                    return;
                }
            }

            try
            {
                // Resize arrays.
                Array.Resize(ref t, this.DeviceDataGridView.RowCount - 1);
                Array.Resize(ref ThreadRun, this.DeviceDataGridView.RowCount - 1);

                // Create the acquisition threads.
                for (RowIndex = 0; RowIndex < this.DeviceDataGridView.RowCount - 1; RowIndex++)
                {
                    t[RowIndex] = new Thread(new ThreadStart(this.ThreadAcquisition));
                    // Set the thread name.
                    t[RowIndex].Name = RowIndex.ToString();
                    // Flag thread running.
                    ThreadRun[RowIndex] = true;
                    // Set the english culture as current culture.
                    t[RowIndex].CurrentCulture = new System.Globalization.CultureInfo("en-GB");
                    t[RowIndex].CurrentUICulture = new System.Globalization.CultureInfo("en-GB");
                    // Start the acquisition thread.
                    t[RowIndex].Start();
                }      

                // Manage controls.
                this.OpeningConnection();
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message + "\n\n" + Ex.ToString(), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);

                // End all the library handles created.
                for (int Index = 0; Index < RowIndex; Index++)
                {
                    BLUEBOXLib.Close(ref HandleArray[Index]);
                    BLUEBOXLib.End(ref HandleArray[Index]);
                }

                // Also release this library handle.
                BLUEBOXLib.Close(ref HandleArray[RowIndex]);
                BLUEBOXLib.End(ref HandleArray[RowIndex]);
                return;
            }
        }
        else
        {
            MessageBox.Show("Must be at least one device in device table!", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Stop);
        }
    }

您可以在此处下载带有文档、软件和库的 SDK:http: //idtronic-rfid.com/zips/BLUEBOX/BLUEBOX%20SDK_JAN15.zip

如果您想了解更多信息,我在这里!

非常感谢你的帮助!

托马斯

4

0 回答 0