0

Wasn't aware of the 64bit OS issue, I am compiling as AnyCPU, but running on a 32bit OS. A re-compile as 32bit made no change.

private void button5_Click(object sender, EventArgs e)
{
  int Adr = 02;
  int Len = 8;
  TData dat = Receive(Adr,Len);
  if (dat.BytesRead == 0)
   {
     StatusLabel1.Text = "Read Failure";
   }
  string s = dat.TextData;
  txtBox1.Text = s;
 }

reports "Read Failure" and the debug shows HasData as false.

0

Yes, the Delphi code will both send data to the eeprom and read it back.

My C# conversion will also send data to the eeprom, but the read(receive) is not returning data.

0

Still bashing away at this project,, here is a bit more info regarding the DLL.

The user does not need to solve how to access the communication ports in his
application. The DLL should work in all Windows versions (95 to Seven, incl. 64-bit edition).

The DLL is originally created in Delphi 6 but should work with any programming language due to use of stdcall convention and simple format of data exchange. Please follow the documentation of your programming language for more details about how to use external functions and procedures from DLL. We have no information about how to use the DLL in other programming languages.

Note* in Delphi, Integer = signed 32-bit.

List of functions and procedures provided by the DLL
Type TData = array [0..255] of byte;
function Version: PChar; stdcall;
procedure ConnectionSetup(ConMode, PortNum: integer; Bidirectional, Slow:
boolean); stdcall;
function Connect: boolean; stdcall;
procedure Disconnect; stdcall;
function Send(Len: integer; Data: TData): boolean; stdcall;
function Receive(Adr, Len: Integer): TData; stdcall;

RECEIVE
Reads RDS data from the device.

Adr: Starting address
Len: Total length of the RDS data being read.

The function returns a TData array where the item with index 0 contains the starting
address and the item with index 1 contains the length of the data received. Following
items (index 2 and higher) contain the data received. If the Receive operation fails, the length value returned in the array is set to zero.

The entire Delphi 6 example source code:

unit example1;
interface
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, StdCtrls, ExtCtrls;
type
  TData = array [0..255] of byte;
type
  TForm1 = class(TForm)
     RadioGroup1: TRadioGroup;
     GroupBox1: TGroupBox;
     CheckBox1: TCheckBox;
     CheckBox2: TCheckBox;
     Button1: TButton;
     Button2: TButton;
     GroupBox2: TGroupBox;
     Edit1: TEdit;
     Label1: TLabel;
     Button3: TButton;
     StatusBar1: TStatusBar;
     Button4: TButton;
     RadioGroup2: TRadioGroup;
     Button5: TButton;
     procedure Button4Click(Sender: TObject);
     procedure Button1Click(Sender: TObject);
     procedure Button2Click(Sender: TObject);
     procedure FormClose(Sender: TObject; var Action: TCloseAction);
     procedure Button3Click(Sender: TObject);
     procedure Button5Click(Sender: TObject);
  private
     { Private declarations }
  public
     { Public declarations }
  end;
var
  Form1: TForm1;
implementation
{$R *.dfm}
function Version: PChar; stdcall; external 'mrdsio.dll';
function Connect: boolean; stdcall; external 'mrdsio.dll';
procedure Disconnect; stdcall; external 'mrdsio.dll';
function Send(Len: integer; Data: TData): boolean; stdcall; external
'mrdsio.dll';
function Receive(Adr, Len: integer): TData; stdcall; external 'mrdsio.dll';
procedure ConnectionSetup(ConMode, PortNum: integer; Bidirectional, Slow:
boolean); stdcall; external 'mrdsio.dll';
procedure TForm1.Button4Click(Sender: TObject);
begin
ShowMessage(Version);
end;
procedure TForm1.Button1Click(Sender: TObject);
var PortNum: integer;
begin
if (RadioGroup1.ItemIndex=0) then PortNum:=$378 else
PortNum:=RadioGroup1.ItemIndex;
ConnectionSetup(RadioGroup2.ItemIndex, PortNum, CheckBox1.Checked,
CheckBox2.Checked);
if (Connect) then StatusBar1.SimpleText:='OK' else
StatusBar1.SimpleText:='Failed';
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
Disconnect;
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Disconnect;
end;
procedure TForm1.Button3Click(Sender: TObject);
var Data: TData;                   //Data[0] = address, Data[1..] = rds data
     i, Len: integer;              //Len = length of rds data
begin
Data[0]:=$02;                      //PS buffer address
for i:=1 to 8 do Data[i]:=$20;     //fill by spaces at first
for i:=1 to length(Edit1.Text) do
  Data[i]:=Ord(Edit1.Text[i]);     //copy PS to Data
Len:=8;
if (Send(Len,Data)) then StatusBar1.SimpleText:='OK' else
StatusBar1.SimpleText:='Failed';
end;
procedure TForm1.Button5Click(Sender: TObject);
var Data: TData;
     i: integer;
     s: string;
begin
if not (CheckBox1.Checked) then
  begin StatusBar1.SimpleText:='Bidirectional operation not allowed'; exit; end;
for i:=0 to 255 do Data[i]:=0;
Data:=Receive($02,8);
if (Data[1]=0) then
  begin StatusBar1.SimpleText:='Failed'; exit; end;
s:='';
for i:=1 to 8 do
  s:=s+Chr(Data[i+1]);
Edit1.Text:=s;
end;
end.

MRDSIO.DLL source code listing:

library mrdsio;
uses
  SysUtils,
  Classes,
  Forms,
  Unit1 in 'Unit1.pas' {DM1: TDataModule};
{$R *.res}
function Version: PChar; stdcall;
begin
Result:='0.98';
end;
function Connect: boolean; stdcall;
begin
Result:=DM1.Connect;
end;
procedure Disconnect; stdcall;
begin
DM1.Disconnect;
end;
function Send(Len: integer; Data: Unit1.TData): boolean; stdcall;
begin
Result:=DM1.Send(Len, Data);
end;
function Receive(Adr, Len: Integer): Unit1.TData; stdcall;
begin
Result:=DM1.Receive(Adr, Len);
end;
procedure ConnectionSetup(ConMode, PortNum: integer; Bidirectional, Slow:
boolean); stdcall;
begin
DM1.ConnectionSetup(ConMode, PortNum, Bidirectional, Slow);
end;
Exports
  Version, Connect, Disconnect, ConnectionSetup, Send, Receive;
begin
DM1:=TDM1.Create(Application);
end

.

Note: Sources from the DM1 data module are not available for public use.


Now the C# conversion I have tried is this:

[DllImport("mrdsio.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
            public static extern byte[] Receive(int Adr, int Len);

        private void btnRX_Click(object sender, EventArgs e)
        {
            byte[] Data = new byte[256];
            int i;
            string s;
            int Adr = 0x02;		// buffer address
            int Len = 8;
            if (!(checkBox1.Checked))
            { toolStripStatusLabel1.Text = "Bidirectional operation not allowed"; return; }
            for (i = 0; i <= 255; i++) //Data[i] = 0;
                Data = Receive(Adr, Len);
            if ((Data[1] == 0))
            { toolStripStatusLabel1.Text = "Failed"; return; }
            s = " ";
            for (i = 1; i <= 8; i++)
                s = s + Data[i + 1].ToString();
            txtBox1.Text = s;
        }

I've tried the suggestions already posted on this thread without success, which is why I am going back to step one..

Does this appear to be the correct translation from Delphi to C#?

[DllImport("mrdsio.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
            public static extern byte[] Receive(int Adr, int Len);

Edited by SteveyD: n/a

0
[return: MarshalAs(UnmanagedType.Struct)]
    [DllImport("mrdsio.dll")]
    static extern TData Receive(int Adr, int Len);

    [StructLayout(LayoutKind.Explicit, Size=256)]
    unsafe struct TData
    {
      [FieldOffset(0)]
      public fixed byte m_buffer[256];
    }
    void simpleButton3_Click(object sender, EventArgs e)
    {
      TData buffer = Receive(0x02, 8);
      Debugger.Break();
    }

I think I really missed the boat back on page 3 of this thread.... I've run this a many different times with the same data stored in the eeprom and the m_buffer is reporting the same values repeatedly


How would I break down the m_buffer value currently held into its byte array knowing its length is 8?

Edited by SteveyD: n/a

0

I think I really missed the boat back on page 3 of this thread.... I've run this a many different times with the same data stored in the eeprom and the m_buffer is reporting the same values repeatedly


How would I break down the m_buffer value currently held into its byte array knowing its length is 8?

Nevermind, too many long nights in front of the screen is playing tricks on me.

Receive(Adr,Len); is still not returning the eeprom data.

0

A thousand Thanks you's to Sknake..

My error was not in the Function but in a bool declaration related to the connection.

Regards
Steve

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.