Using SPI_Transfer() Method In C#

All about Sub-20 Multi Interface USB Adapter USB to I2C, SPI, GPIO, RS232, RS485, Ir, LCD

Moderator: serg

Red
Posts: 6
Joined: Sat May 08, 2010 12:05 am

Using SPI_Transfer() Method In C#

Post by Red »

Hi All,

I have problem writing a function to read and write from SPI using SPI_Transfer(). I tried the sample code written for C#, but it was not helpful that much.
Using SUB20 tool software I can communicate with my device through SPI and I want to use the same configuration in my code(C#) but I can't define the SPI_Transfer() method correctly. The following is the sample code I wrote. Can someone help me to figure out how I am going to use this method.

Thank you very much.

Red

#region Namespace Inclusions
using System;
using System.Text;
using System.IO.Ports;
using System.Linq;
using System.Windows.Forms;
using System.Collections.Generic;
using Xdimax;
//using System.Data;
//using System.Drawing;
//using System.ComponentModel;
//using Tester_FormApp_Rev00.Properties;
#endregion


namespace Tester_FormApp_Rev00
{
public class SUB20_HANDLER
{

#region Local Variables

// SUB20 Class Instance
Sub20 dev = new Sub20();
public byte[] dataSend = new byte[] { 0x80, 0x44, 0x33, 0x31, 0x39, 0x39, 0x3B };
public Array[] dataReceive = new Array[32];


#endregion // Local Variables

#region Methods
// -----------------------------------------------------------------
// SUB-20 Device Initialization
// -----------------------------------------------------------------
public void DeviceInit()
{
SPIConfig();
}
// -----------------------------------------------------------------
// SPI Configuration
// -----------------------------------------------------------------
public void SPIConfig()
{
string s = "";

if (!dev.Open(0))
{
MessageBox.Show(
dev.GetStrError(dev.GetLastError()),
"Open failed");
return;
}


do
{
dev.LCD_Write("Hello Red");
/* configure SPI */
if (!dev.SPI_SetConfig(
Sub20.SpiEnable |
Sub20.SpiCpolFall |
Sub20.SpiSetupSmpl |
Sub20.SpiMsbFirst |
Sub20.SpiClk_125kHz))
{
MessageBox.Show(
dev.GetStrError(dev.GetLastError()),
"SPI_SetConfig failed");
break;
}



int doesWork = dev.SPI_Transfer(ref dataReceive, 0, Sub20.SpiSS_H);



/* transmit data, slave select 2 */

//if (dev.SPI_Transfer(ref dataReceive[0], 0, Sub20.SpiSS_H))
//{


//}
//else
//{
// MessageBox.Show( dev.GetStrError(dev.GetLastError()),
// "SPI_Read failed");
// break;
//}





} while (false);


dev.Close();

}



#endregion // Method



} // SUB20_HANDLER Class

} // Tester_FormApp_Rev00 namespace

serg
Posts: 143
Joined: Mon Aug 31, 2009 9:17 pm

Re: Using SPI_Transfer() Method In C#

Post by serg »

Hi,

What exactly doesn't work in our C# sample code? If you need to send and receive data just replace the SPI_Write call with the SPI_Transfer call, so the data from the data array will be sent with SS2 asserted and on return received data will be placed into the same array

Regards

Red
Posts: 6
Joined: Sat May 08, 2010 12:05 am

Re: Using SPI_Transfer() Method In C#

Post by Red »

Hi Serg,

According to the definition of the SPI_Transfer() function I should pass the following to it:
public bool SPI_Transfer(ref Array Buffer, int ss_pin, int ss_mode);
- ref to Array Buffer
- ss_pin (integer)
- ss_mode (integer)
I am defining an array of bytes like byte[] dataReceive = new byte[8];
and after opening the port and SPI_config() and SPI_Write()I am passing it to the function like:
do
{
if (!dev.SPI_Transfer(ref dataReceive, 0, Sub20.SpiSS_H))
{

MessageBox.Show(dev.GetStrError(dev.GetLastError()),
"SPI_Transfer failed");
break;
}
}
while(false);
dev.Close();

I don't know how I can pass the array of bytes to the function and not get rid of different kinds of compiler errors.

The goal is that I be able to write to SPI and then read the respond to the richbox in my form application, similar to the Sub-20 Software application.
It would be appreciated and most helpful if you could send me a code sample that SPI_Transfer() is used in it.
Please see the attached .txt file for your reference.
Thank you very much,

Red
Attachments
SUB-20 Sample Code.zip
(1.5 KiB) Downloaded 680 times

serg
Posts: 143
Joined: Mon Aug 31, 2009 9:17 pm

Re: Using SPI_Transfer() Method In C#

Post by serg »

Hi Red,

I am not C# expert, so I may be wrong here, but according to the MSDN http://msdn.microsoft.com/en-us/library ... S.71).aspx
The ref method parameter keyword on a method parameter causes a method to refer to the same variable that was passed into the method. Any changes made to the parameter in the method will be reflected in that variable when control passes back to the calling method., i.e the ref keyword is using in method's parameters declaration when you declare a method as part of your class and NOT when you call it. Therefore, you don't need to specify the "ref" keyword when you call the SPI_Transfer method. Let's get back to your goal, which is "to be able to write to SPI and then read the respond to ...". In the SPI, "sending" and "receiving" happen simultaniously, see http://en.wikipedia.org/wiki/Serial_Per ... erface_Bus for details. Depending on your application however, It is possible to divide a transaction to "send" and "receive" part by either sending dummy bytes(SPI_Read) or discarding received bytes(SPI_Write). Anyway, you should use the SPI_Transfer method if you need to exchange data with the slave device, i.e send and receive data at the same time. Therefore, you have to declare a data array, which will be used as a placeholder for both - output and input data, i.e

/* We assume that the SUB-20 was opened and SPI configured successfully */
...

/* declare data array ( 3 bytes ) */
byte [] data = new byte[] {0x33, 0x44, 0x55};

/* exchange data, with a device responding to the slave select 2 */
success=dev.SPI_Transfer(data, 2, Sub20.SpiSS_HL)

/* at this point the "data" array contains received data bytes */
Analyze(data)

...

See the C# sample code for more information

Regards

Red
Posts: 6
Joined: Sat May 08, 2010 12:05 am

Re: Using SPI_Transfer() Method In C#

Post by Red »

Hi Serg,

Thank you for the respond. The code you sent is not working. The Error message is :"Cannot convert from 'byte[]' to 'ref System.Array' ".
I have attached the project for your reference. If you don't have any C# compiler, there is a free one available here:
http://www.microsoft.com/express/downlo ... -Visual-CS

I am using the 2008 version of the same one.
I have a relatively high priority project which SUB-20 is one of the most important part of the system. I am using eight of them in it and this problem has stopped the whole project now.
Please get someone help me on that if you are not expert in this field.

Thank you.

Red
Attachments
c# Transfer test.zip
(78.39 KiB) Downloaded 767 times

serg
Posts: 143
Joined: Mon Aug 31, 2009 9:17 pm

Re: Using SPI_Transfer() Method In C#

Post by serg »

Hi Red,
You are right. This was our bug in .NET component v1.2.4. It has been fixed however in 1.2.5 or 1.2.6. Just download and install the latest version of the SUB-20 software. This package contains fixed .NET component v1.2.7.
Regards

Red
Posts: 6
Joined: Sat May 08, 2010 12:05 am

Re: Using SPI_Transfer() Method In C#

Post by Red »

Hi Serg,

The only availabe version for download in http://www.xdimax.com/sub20/sub20.html#DLD is SUB-20net-1.2.4.zip . Please let me know where I can find the latest version of the software.

Thanks.

Red

serg
Posts: 143
Joined: Mon Aug 31, 2009 9:17 pm

Re: Using SPI_Transfer() Method In C#

Post by serg »

Hi Red,

http://www.xdimax.com/sub20/download/SU ... 20-x32.exe or
http://www.xdimax.com/sub20/download/SU ... 20-x64.exe depending on your platform.

This package contains the .NET component

Regards

Hugoneus
Posts: 11
Joined: Tue May 11, 2010 9:01 pm

Re: Using SPI_Transfer() Method In C#

Post by Hugoneus »

What about the latest version of the FW? This one seems to still have the 0.2.3.

serg
Posts: 143
Joined: Mon Aug 31, 2009 9:17 pm

Re: Using SPI_Transfer() Method In C#

Post by serg »

Hi Hugoneus,

The FW v0.2.3 is the latest officially released one. Do you have any issues with this version of the firmware ?

Regards

Post Reply