[C#] Pourquoi un pointeur sur un tableau de byte provoque des codes d'erreur ?
Voila, je suis débutant en C#, et après avoir fait plusieurs tentatives je viens vous demander de l'aide.
J'ai une carte APPLICOM qui gére un réseau CAN. Avec cette carte, le fournisseur ma donner une DLL qui permet de dialoguer avec celle ci. Je l'ai tester avec LabWindows CVI et cela fonctionne. Quand j'essaye avec C# j'ai des codes d'erreur. A priori cela viendrait du tableau de byte passer par pointeur.
Voila le fichier H :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
#ifndef __APPLICOMIO_HEADER_FILE__
#define __APPLICOMIO_HEADER_FILE__
#include <cvidef.h>
typedef unsigned short WORD;
typedef unsigned char BYTE;
typedef unsigned long DWORD;
/* Translate a card number to a chan number */
#define CARD2CHAN(x) ((WORD)((x-1)*4))
/* Function prototype */
BOOL _stdcall AuInitBus_io (DWORD* pdwStatus);
BOOL _stdcall AuExitBus_io (DWORD* pdwStatus);
BOOL _stdcall AuWriteReadMsg_io (WORD wChan,
WORD wNes,
DWORD dwMsgParam,
WORD wNbTx,
BYTE* lpbyBufTx,
WORD* pwNbRx,
BYTE* lpbyBufRx,
DWORD* pdwStatus);
#endif |
Dans ce fichier, j'utilie la fonction AuWriteReadMsg_io.
Voila le code en C# ( Microsoft express )
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
|
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace CanCSharp
{
unsafe public partial class Form1 : Form
{
int StatusInit=0;
int StatusStop = 0;
[DllImport("applicomio.dll")]
public static extern bool AuInitBus_io(ref int StatusInit);
[DllImport("applicomio.dll")]
public static extern bool AuExitBus_io(ref int StatusStop);
[DllImport("applicomio.dll")]
public static extern bool AuWriteReadMsg_io(
UInt16 wChan,
uint wNes,
uint dwMesParam,
UInt16 wNbTx,
ref byte []BufTx,
ref UInt16 NbRx,
ref byte []BufRx,
ref uint Status);
public Form1()
{
InitializeComponent();
}
private void InitBtn_Click(object sender, EventArgs e)
{
bool ready = AuInitBus_io(ref StatusInit);
if (ready)
ErrorInit.Text = "OK";
else
ErrorInit.Text = "Fault" + FunctionErrorMes(StatusInit);
}
private void Form1_Load(object sender, EventArgs e)
{
ErrorInit.Text = "";
ErrorStop.Text = "";
}
private void StopBtn_Click(object sender, EventArgs e)
{
bool ready = AuExitBus_io(ref StatusStop);
if (ready)
ErrorStop.Text = "OK";
else
ErrorStop.Text = "Fault" + FunctionErrorMes(StatusStop);
}
private string FunctionErrorMes(int CodeErr)
{
if (CodeErr == 0)
return "";
if (CodeErr == 4)
return " = Code ID déja utilisé ou erreur émission";
if (CodeErr == 32)
return " = Paramêtre non OK (trop long ou trop court)";
if (CodeErr == 33)
return " = Erreur Time Out";
if (CodeErr == 36)
return " = Equipement non configuré";
if (CodeErr == 45)
return " = Faire un init carte applicom";
if (CodeErr == 47)
return " = Carte Applicom non valide";
if (CodeErr == 63)
return "Erreur de communication";
if (CodeErr == 66)
return "Mémoire de la carte Applicom insuffisante";
if (CodeErr == 93)
return " = Driver inaccéssible";
if (CodeErr == 99)
return " = DLL applicomIO en cours d'utilisation";
return " = Erreur inconnue";
}
public void SendBtn_Click(object sender, EventArgs e)
{
UInt16 wChan = 0;
uint wNes = 1;
uint dwMesParam = 0x180;
UInt16 wNbTx = 5;
byte [] BufTx;
UInt16 NbRx = 0;
byte [] BufRx;
uint Status = 0;
BufTx = new byte[5];
BufTx[0] = 0x08;
BufTx[1] =0x00;
BufTx[2] =0x00;
BufTx[3] =0x00;
BufTx[4] =0x00;
BufRx = new byte[1];
BufRx[0] = 0x00;
bool ready = AuWriteReadMsg_io
(wChan,wNes,dwMesParam,wNbTx,ref BufTx,ref NbRx,
ref BufRx,ref Status);
if (ready)
ErrorSend.Text = "OK";
else
ErrorSend.Text = "Fault" + FunctionErrorMes((int)Status);
}
}
} |
Et voici l'exemple en C qui fonctionne :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
|
void __declspec(dllexport) TX_TEST RunNCSCardsInNormalMode(tTestData
*testData, tTestError *testError)
{
int error = 0;
ErrMsg errMsg = {'\0'};
ERRORINFO errorInfo;
BOOL Result = FALSE;
BYTE BufRx = 0;
WORD NbRx = 0;
BYTE BufTx[5] = {0x08,0x00,0x00,0x00,0x00};
WORD NbTx = 5;
DWORD MsgParam = 0x180;
WORD Nes = 1;
WORD Chan = 0;
BufTx[3] = 0x00;
AuWriteReadMsg_io (Chan, Nes, MsgParam, NbTx, BufTx,&NbRx, &BufRx, &error);
Sleep(200);
BufTx[3] = 0x02;
AuWriteReadMsg_io (Chan, Nes, MsgParam, NbTx, BufTx,&NbRx, &BufRx, &error);
Error:
// If an error occurred, set the error flag to cause a run-time error in
TestStand.
if (error != 0)
{
testError->errorFlag = TRUE;
// OPTIONALLY SET THE ERROR CODE AND STRING
testError->errorCode = error;
testData->replaceStringFuncPtr(&testError->errorMessage, errMsg);
}
return;
} |
Lorsque je compile cette source cela ne génére pas d'érreur,mais ca marche pas Erreur 32 (Mauvais paramêtrage)
Lorsque je modifie le prototype de la fonction en mettant des pointeurs (comme en C), il n'arrive pas à lié le pointeur avec le tableau de byte. Donc si vous avez quelques pistes à exploiter, je suis prenneur ;)