Using SPI_Transfer() Method In C#
Posted: Sun May 09, 2010 12:35 am
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
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