Can not start my own pcan project

This forum covers issues concerning multiple software products.
Post Reply
irma1
Posts: 4
Joined: Mon 15. Oct 2018, 23:29

Can not start my own pcan project

Post by irma1 » Tue 16. Oct 2018, 09:35

Hello ,
I am new in using PCANBASIC API in c# visual Studio .
The c# sample work just fine when I run it but while creating my own project I get this error.

System.BadImageFormatException: 'An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)'

in this row : return Initialize(Channel, Btr0Btr1, (TPCANType)0, 0, 0); in pcanbasic.cs
I can not do the initialization.
Can somebody help please I have tried everything I could.

User avatar
U.Wilhelm
Sales & Support
Sales & Support
Posts: 1104
Joined: Fri 10. Sep 2010, 19:34

Re: Can not start my own pcan project

Post by U.Wilhelm » Tue 16. Oct 2018, 09:41

You mixed up the API DLL Version (32Bit/64Bit) - please check if the DLL´s are ONLY installed by our Driver Setup - not manually (most people do not know how to do - thats why we add this feature in the Setup)
PeakOemDrv.exe -> run -> select Advanced >- select PCAN-Basic
Remove ALL other copy of the DLL on your PC. See .Net Specification - using Windows 32/64DLL.
See also here
--------------------------------
PEAK-System Technik
Technical Support Team
support@peak-system.com
-------------------------------

irma1
Posts: 4
Joined: Mon 15. Oct 2018, 23:29

Problem in writing function

Post by irma1 » Fri 26. Oct 2018, 09:30

I have written a function like that that is taking a string value from 0-1024 value convertin to hexadecimal and try to send the Can Message.
But when I send 141 wich in hexadecimal is =8D input string error I get
when I send 150 which in hexadecimal = 96 the messega is sent .
I think that I am doing something wrong while conveting and I am new at programing.
Can someone help ?

Code: Select all

 strMsg = new StringBuilder(256);
                msg = new TPCANMsg();
                msg.DATA = new Byte[8];
                msg.ID = Convert.ToUInt32(id, 16);
                msg.MSGTYPE = TPCANMessageType.PCAN_MESSAGE_STANDARD;
               // Console.WriteLine(msg.ID);
                string messageDataHex = Convert.ToInt32(value).ToString("X");
                string data = "";
                for (int i = 8 - messageDataHex.Length; i > 0; i--)
                {
                    data = "0" + messageDataHex;
                    messageDataHex = data;
                }
                msg.LEN = 8;
                for (int i = 0; i <= (msg.LEN - 2) / 2; i++)
                {
                    var subString = messageDataHex.Substring(i * 2, 2);
                    msg.DATA[i] = Convert.ToByte(messageDataHex.Substring(i * 2, 2));


                }


                result11 = PCANBasic.Write(PCANBasic.PCAN_USBBUS1, ref msg);

User avatar
U.Wilhelm
Sales & Support
Sales & Support
Posts: 1104
Joined: Fri 10. Sep 2010, 19:34

Re: Can not start my own pcan project

Post by U.Wilhelm » Fri 26. Oct 2018, 09:53

Waht exact do you want to do?

The CAN Protocoll could handle in the CAN2.0A/B Version max. 8 Data Bytes in one Frame.
So you could transfer 8 x 8 Bit (64 Bit). If you want to transfer a Value larger 1 Byte (range 0..255 or -128 to 127) you need to use a different Data Type. For Example use Byte 0 and Byte 1 of a CAN Frame and use this two Byte to transport a 16Bit Value (0 to 65.535 or -32.768 to 32.767). It is now depending on the Data Type you want to use, or what your Receiver require. This is called byte order or endianness. Please see here and here.
--------------------------------
PEAK-System Technik
Technical Support Team
support@peak-system.com
-------------------------------

irma1
Posts: 4
Joined: Mon 15. Oct 2018, 23:29

Re: Can not start my own pcan project

Post by irma1 » Fri 26. Oct 2018, 11:18

My function takes a parameter a string with a range of 0-1024 and the value sent will be the message to be written ex. Msg.DATA I did try to convert and the function in my previous post works just for decimals which converted to hexadecimal are number . Ex (not 8D ) and I couldnt solve this problem. This is what I tried to explain in the first post.

User avatar
U.Wilhelm
Sales & Support
Sales & Support
Posts: 1104
Joined: Fri 10. Sep 2010, 19:34

Re: Can not start my own pcan project

Post by U.Wilhelm » Fri 26. Oct 2018, 11:37

You need to convert the "String" to a Value of the needed Data Type. This is depending which language you use.
C#:

Code: Select all

int myNumber = someString.ParseInt(); // returns value or 0
int age = someString.ParseInt(45); // with default value 45
see: Microsoft

But this have nothing to do with our API - it is depending on the programming language how to convert a String to a signed or unsigned Data Type.

C++ or C:

Code: Select all

int number1 = atoi(charNumber1); 
see here
--------------------------------
PEAK-System Technik
Technical Support Team
support@peak-system.com
-------------------------------

Post Reply