inkfish321 发表于 2013-9-7 16:03:34

求助,谁同时懂C#和C++,想把一段程序用C实现,有些细节...

本帖最后由 inkfish321 于 2013-9-7 16:07 编辑

正在写一个BLE的上位机程序,但是NORDIC所给的例子只有C#,和DLL。提供C++的接口,但是没头文件。只有API的说明文档。

C#的部分代码如下:
using System;
using Nordicsemi;

namespace nRFUart
{
    internal class PipeSetup
    {
      /* Public properties for accessing discovered pipe IDs */
      public int UartRxPipe { get; private set; }
      public int UartTxPipe { get; private set; }

      MasterEmulator masterEmulator;

      public PipeSetup(MasterEmulator master)
      {
            masterEmulator = master;
      }

      /// <summary>
      /// Pipe setup is performed by sequentially adding services, characteristics and
      /// descriptors. Pipes can be added to the characteristics and descriptors one wants
      /// to have access to from the application during runtime. A pipe assignment must
      /// be stated directly after the characteristic or descriptor it shall apply for.
      /// The pipe type chosen will affect what operations can be performed on the pipe
      /// at runtime. <see cref="Nordicsemi.PipeType"/>.
      /// </summary>
      ///
      public void PerformPipeSetup()
      {
            /* GAP service */
            BtUuid uartOverBtleUuid = new BtUuid("6e400001b5a3f393e0a9e50e24dcca9e");
            masterEmulator.SetupAddService(uartOverBtleUuid, PipeStore.Remote);

            /* UART RX characteristic (RX from peripheral's viewpoint) */
            BtUuid uartRxUuid = new BtUuid("6e400002b5a3f393e0a9e50e24dcca9e");
            int uartRxMaxLength = 20;
            byte[] uartRxData = null;
            masterEmulator.SetupAddCharacteristicDefinition(uartRxUuid, uartRxMaxLength,
                uartRxData);
            /* Using pipe type Transmit to enable write operations */
            UartRxPipe = masterEmulator.SetupAssignPipe(PipeType.Transmit);

            /* UART TX characteristic (TX from peripheral's viewpoint) */
            BtUuid UartTxUuid = new BtUuid("6e400003b5a3f393e0a9e50e24dcca9e");
            int uartTxMaxLength = 20;
            byte[] uartTxData = null;
            masterEmulator.SetupAddCharacteristicDefinition(UartTxUuid, uartTxMaxLength,
                uartTxData);
            /* Using pipe type Receive to enable notify operations */
            UartTxPipe = masterEmulator.SetupAssignPipe(PipeType.Receive);
      }
    }
}

我想用C来实现,这是我改的代码:
#define NRF_UART_RX_BUF_LENGTH 20
#define NRF_UART_TX_BUF_LENGTH 20

typedef struct _PedoNrfUartIo{
//public:
    PedoIoTypeDef supper;
    void (*StopScanning)(void *me);
//private:
    uint8_t mPackHeadFlag1;
    uint8_t mPackHeadFlag2;
    uint32_t mReceiveCount;
    uint32_t mSendCount;
    uint32_t mBusy;
    uint8_t mUartRxData;
    uint8_t mUartTxData;
    int32_t UartRxPipe;
    int32_t UartTxPipe;
    HINSTANCE hMasterEmulatorDll;
    MasterEmulator *pMasterEmulator;
    BtUuid *pUartOverBtleUuid;
    BtUuid *pUartRxUuid;
    BtUuid *pUartTxUuid;
}PedoNrfUartIoTypeDef;



PedoIoTypeDef* CreateNrfUartIo(void);



static void ClosePedoNrfUart(void *me)
{
    PedoNrfUartIoTypeDef *this = (PedoNrfUartIoTypeDef *)me;
   
    if(this == NULL)
      return;

    if(this->hMasterEmulatorDll != INVALID_HANDLE_VALUE)
    {
      AfxFreeLibrary(this->hMasterEmulatorDll);
    }
        if(this->pMasterEmulator != NULL)
        {
                delete(this->pMasterEmulator);
        }
    free(this);
}


PedoIoTypeDef* CreateNrfUartIo(void)
{
    PedoNrfUartIoTypeDef *NrfUartIo = (PedoNrfUartIoTypeDef *)malloc(sizeof(PedoNrfUartIoTypeDef));
    PedoIoTypeDef *supper = (PedoIoTypeDef *)NrfUartIo;
   
    if(supper != NULL)
    {
      memset(NrfUartIo, 0, sizeof(PedoNrfUartIoTypeDef));
      supper->GetIoType = &NrfUartGetIoType;
      supper->GetPackHeadFlag = &NrfUartGetPackHeadFlag;
      supper->UpdateIoState = &NrfUartUpdateIoState;
      supper->CloseIo = &ClosePedoNrfUart;
      supper->ReadPack = &NrfUartReadPack;
      supper->SendPack = &NrfUartSendPack;
      NrfUartIo->hMasterEmulatorDll = AfxLoadLibrary(_T("MasterEmulator.dll"));
      //打开USB设备
    }
        else
        {
                return NULL;
        }
        if(this->hMasterEmulatorDll != INVALID_HANDLE_VALUE)
        {
                NrfUartIo->pMasterEmulator = new MasterEmulator();
                NrfUartIo->pUartOverBtleUuid = new BtUuid("6e400001b5a3f393e0a9e50e24dcca9e");
                NrfUartIo->pUartRxUuid = new BtUuid("6e400002b5a3f393e0a9e50e24dcca9e");
                NrfUartIo->pUartTxUuid = new BtUuid("6e400003b5a3f393e0a9e50e24dcca9e");
        }
        else
        {
                supper->CloseIo();
                return NULL;
        }
       
        if((NrfUartIo->pMasterEmulator != NULL) &&
                (NrfUartIo->pUartOverBtleUuid != NULL)&&
                (NrfUartIo->pUartRxUuid != NULL)&&
                (NrfUartIo->pUartTxUuid != NULL))
        {
          MasterEmulator *pMasterEmulator = NrfUartIo->pMasterEmulator;
          /* GAP service */
                pMasterEmulator->SetupAddService(*NrfUartIo->pUartOverBtleUuid, PipeStore.Remote);

                /* UART RX characteristic (RX from peripheral's viewpoint) */
                pMasterEmulator->SetupAddCharacteristicDefinition(NrfUartIo->pUartRxUuid ,
                                                                  NRF_UART_RX_BUF_LENGTH,
                                                                  NrfUartIo->mUartRxData);
                /* Using pipe type Transmit to enable write operations */
                NrfUartIo->UartRxPipe = pMasterEmulator->SetupAssignPipe(PipeType.Transmit);

                /* UART TX characteristic (TX from peripheral's viewpoint) */
                pMasterEmulator->SetupAddCharacteristicDefinition(*NrfUartIo->pUartTxUuid,
                                                                  NRF_UART_TX_BUF_LENGTH,
                                                                  NrfUartIo->mUartTxData);
               
                /* Using pipe type Receive to enable notify operations */
                NrfUartIo->UartTxPipe = pMasterEmulator->SetupAssignPipe(PipeType.Receive);
        }
        else
        {
                supper->CloseIo();
                return NULL;
        }

    return supper;
}

但是有很多问题不懂。例如用C++ NEW出来的类是不是该用类指针来代替。
另外看一下文档里提供的函数对照如下:
C#
public void SetupAddCharacteristicDefinition(
        BtUuid characteristicUuid,
        int maxDataLength,
        byte[] data
)

Visual C++
public:
void SetupAddCharacteristicDefinition(
        BtUuid^ characteristicUuid,
        int maxDataLength,
        array<unsigned char>^ data
)

C#
public void SetupAddService(
        BtUuid serviceUuid,
        PipeStore pipeStore
)

Visual C++
public:
void SetupAddService(
        BtUuid^ serviceUuid,
        PipeStore pipeStore
)

C#
public Collection<string> EnumerateUsb(
        UsbDeviceType deviceType
)

Visual C++
public:
Collection<String^>^ EnumerateUsb(
        UsbDeviceType deviceType
)


BtUuid^这个是什么意思?
Collection<String^>^这个又是什么意思?

wye11083 发表于 2013-9-7 16:31:17

还用C++ CLR编程?太费事了吧。除非它DLL是CLR的。
页: [1]
查看完整版本: 求助,谁同时懂C#和C++,想把一段程序用C实现,有些细节...