Recommended Posts

Hi,

On c# I am using DLL like below;

       [DllImport("commdll.dll", SetLastError = true)]
        static extern uint InitDll(int Portno, int Baudrate);
                          
        [DllImport("commdll.dll", SetLastError = true)]
        static extern uint BusyStatus();

        [DllImport("commdll.dll", SetLastError = true)]
        static extern uint CommStatus();

        [DllImport("commdll.dll", SetLastError = true)]
        static extern int ReadMW(uint PLCAdres, uint MWNO, uint Adet);

        [DllImport("commdll.dll", SetLastError = true)]
        static extern int ReadMB(uint PLCAdres, uint MBNO, uint Adet);

        [DllImport("commdll.dll", SetLastError = true)]
        static extern short GetShort(int Index);

        [DllImport("commdll.dll", SetLastError = true)]
        static extern Byte GetByte(int Index);
               

        [DllImport("commdll.dll", SetLastError = true)]
        static extern int WriteMWExt(uint PLCAdres, uint MWNO, uint Adet);

        [DllImport("commdll.dll", SetLastError = true)]
        static extern short SetShort(int Index, int Value);

        [DllImport("commdll.dll", SetLastError = true)]
        static extern short SetByte(int Index, Byte Value);


        private void button3_Click(object sender, EventArgs e)
        {
            label2.Text = Convert.ToString(InitDll(3, 38400));

            label5.Text = Convert.ToString(ReadMW(1, 0, 5));
            label6.Text = Convert.ToString(GetShort(0));
            label7.Text = Convert.ToString(GetShort(1));
            label8.Text = Convert.ToString(GetShort(2));
        }

and it is working fine on c#.

But On daqfactory when I tried with short,ushort, word, uword combinations I am getting error message from dll like incorrect word addess.

 

global Portno=3
global Baudrate=38400
global InitStatus=-99

global PlcAdres=1
global StartWord=0
global WordCount=5
global ReadStatus=-99
Extern("commdll.dll","ulong InitDll(long, long)","InitDll","stdcall")
delay(1)
//Extern("commdll.dll","uword BusyStatus","BusyStatus","stdcall")
delay(1)
//Extern("commdll.dll","uword CommStatus","CommStatus","stdcall")
delay(1)
Extern("commdll.dll","long ReadMW(ulong,ulong,ulong)","ReadMW","stdcall")
delay(1)      
//Extern("commdll.dll","word ReadMB(uword,uword,uword)","ReadMB","stdcall")
delay(1)
//Extern("commdll.dll","short  GetShort(word)","GetShort","stdcall")
delay(1)
//Extern("commdll.dll","byte GetByte(word)","GetByteDaq","stdcall")
delay(1)      
//Extern("commdll.dll","word WriteMWExt(uword,uword,uword)","WriteMWExt","stdcall")
delay(1)      
//Extern("commdll.dll","short SetShort(word, word)","SetShort","stdcall")
delay(1)
//Extern("commdll.dll","short SetByte(word, byte)","SetByte","stdcall")
delay(1)

            
 InitStatus = InitDll(@Portno,@Baudrate)
 ReadStatus = ReadMW(@PlcAdres, @StartWord,@WordCount)

Is there any table for variables on daqfactory with c# variable?

 

Share this post


Link to post
Share on other sites

Well, the first problem I see is that these functions all appear to take scalar values, but you are passing pointers.  If you get no errors from your extern() function calls, but get an error when you actually call the imported function, then that is likely the issue.  So, for example, it should be:

InitStatus = InitDll(PortNo, BaudRate)

the @ sign returns the pointer to that variable, not the variable contents.  DAQFactory checks to make sure you don't pass a pointer to an imported function that expects a value, and vice-versa.  

Share this post


Link to post
Share on other sites

Hi,

I will test.

what about variables type? is it important? or what is the uint and int variables on daqfactory?

Share this post


Link to post
Share on other sites

DAQFactory only has two variable types, numbers and strings.  It uses the declaration you provide in extern() to convert to the appropriate C data type before calling the external function.  This is done internally and you do not need to worry about it.  DAQFactory will complain, however, when you try and pass a regular variable to a parameter that is expecting a pointer, or vice-versa.  

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now