Hola de nuevo... retomo este asunto porque aunque conseguí hacerlo funcionar con el programa original ligeramente modificado, no lo puedo copipastear asi como asi en mi proyecto y por mas que lo miro no logro encontrar ni la diferencia ni el fallo...
El tema sigue siendo el mismo: con el mismo programa en el PIC18F2550 y el mismo driver instalado en el PC, podemos enviar cuantas veces queramos los datos del micro al PC y del PC al micro que llegan bien. Osea la única diferencia es la aplicación, pero esta provoca un fallo en el micro haciendo que esta función uno=usb_put_packet(1, enviaUSB, 10, USB_DTS_TOGGLE); devuelva 0 y se observe que no le llegan los datos al PC. Es el que expongo a continuación: (Esta en C# igual que el original)
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Runtime.InteropServices; // Clase para importar DLL
using PVOID = System.IntPtr;
using DWORD = System.UInt32;
namespace FFTM.BusinessLayer.USB
{
unsafe public class USB
{
#region Variables
void* myOutPipe;
void* myInPipe;
#endregion
#region Constantes
private static string vid_pid_norm = "vid_04d8&pid_0011";
string out_pipe = "\\MCHP_EP1";
string in_pipe = "\\MCHP_EP1";
private const uint MESSAGE_OUT_LENGTH = 14;
private const uint MESSAGE_IN_LENGTH = 9;
#endregion
#region Properties
#endregion
#region Funciones importadas de la DLL: mpusbapi.dll
[DllImport("mpusbapi.dll")]
private static extern DWORD _MPUSBGetDLLVersion();
[DllImport("mpusbapi.dll")]
private static extern DWORD _MPUSBGetDeviceCount(string pVID_PID);
[DllImport("mpusbapi.dll")]
private static extern void* _MPUSBOpen(DWORD instance, string pVID_PID, string pEP, DWORD dwDir, DWORD dwReserved);
[DllImport("mpusbapi.dll")]
private static extern DWORD _MPUSBRead(void* handle, void* pData, DWORD dwLen, DWORD* pLength, DWORD dwMilliseconds);
[DllImport("mpusbapi.dll")]
private static extern DWORD _MPUSBWrite(void* handle, void* pData, DWORD dwLen, DWORD* pLength, DWORD dwMilliseconds);
[DllImport("mpusbapi.dll")]
private static extern DWORD _MPUSBReadInt(void* handle, DWORD* pData, DWORD dwLen, DWORD* pLength, DWORD dwMilliseconds);
[DllImport("mpusbapi.dll")]
private static extern bool _MPUSBClose(void* handle);
#endregion
#region Constructor
public USB ()
{
}
#endregion
#region Methods
#region Public
public void OpenPipes()
{
DWORD selection = 0;
myOutPipe = _MPUSBOpen(selection, vid_pid_norm, out_pipe, 0, 0);
myInPipe = _MPUSBOpen(selection, vid_pid_norm, in_pipe, 1, 0);
}
public void ClosePipes()
{
_MPUSBClose(myOutPipe);
_MPUSBClose(myInPipe);
}
/// <summary>
/// Esta es la funcion que se usa para enviar los datos por usb al
/// interface Zigbee
/// </summary>
/// <param name="SendData">Array to send</param>
/// <param name="SendLength">length of the packet to send</param>
public void SendPacket(USBOutMessage outMessage)
{
byte[] messageStream = getOutMessageArray(outMessage);
fixed (byte* SendData = messageStream)
{
DWORD SendLength = MESSAGE_OUT_LENGTH;
uint SendDelay = 1000;
DWORD SentDataLength;
OpenPipes();
_MPUSBWrite(myOutPipe, (void*)SendData, SendLength, &SentDataLength, SendDelay);
ClosePipes();
}
}
// esta es la funcion que se usa para recibir los datos por usb al interface Zigbee
public USBInMessage ReceivePacket()
{
USBInMessage inMessage = null;
fixed (byte* ReceiveData = new byte[MESSAGE_IN_LENGTH])
{
DWORD ReceiveLength = MESSAGE_IN_LENGTH;
uint ReceiveDelay = 1000;
DWORD ExpectedReceiveLength = ReceiveLength;
OpenPipes();
_MPUSBRead(myInPipe, (void*)ReceiveData, ExpectedReceiveLength, &ReceiveLength, ReceiveDelay);
ClosePipes();
inMessage = getInMessage(ReceiveData);
}
return inMessage;
}
Este es el código que si que funciona, pero yo no soy capaz de ver la diferencia, por favor si a alguien le ha pasado algo parecido, comentemelo por favor muchas gracias!
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Runtime.InteropServices; // Clase para importar DLL
using PVOID = System.IntPtr;
using DWORD = System.UInt32;
namespace PicUSB
{
unsafe public class PicUSBAPI
{
#region Definición de los Strings: EndPoint y VID_PID
string vid_pid_norm = "vid_04d8&pid_0011";
string out_pipe = "\\MCHP_EP1";
string in_pipe = "\\MCHP_EP1";
#endregion
#region Funciones importadas de la DLL: mpusbapi.dll
[DllImport("mpusbapi.dll")]
private static extern DWORD _MPUSBGetDLLVersion();
[DllImport("mpusbapi.dll")]
private static extern DWORD _MPUSBGetDeviceCount(string pVID_PID);
[DllImport("mpusbapi.dll")]
private static extern void* _MPUSBOpen(DWORD instance, string pVID_PID, string pEP, DWORD dwDir, DWORD dwReserved);
[DllImport("mpusbapi.dll")]
private static extern DWORD _MPUSBRead(void* handle, void* pData, DWORD dwLen, DWORD* pLength, DWORD dwMilliseconds);
[DllImport("mpusbapi.dll")]
private static extern DWORD _MPUSBWrite(void* handle, void* pData, DWORD dwLen, DWORD* pLength, DWORD dwMilliseconds);
[DllImport("mpusbapi.dll")]
private static extern DWORD _MPUSBReadInt(void* handle, DWORD* pData, DWORD dwLen, DWORD* pLength, DWORD dwMilliseconds);
[DllImport("mpusbapi.dll")]
private static extern bool _MPUSBClose(void* handle);
#endregion
void* myOutPipe;
void* myInPipe;
static void Main()
{
Application.EnableVisualStyles();
Application.Run(new PicUSB());
}
public void OpenPipes()
{
DWORD selection = 0;
myOutPipe = _MPUSBOpen(selection, vid_pid_norm, out_pipe, 0, 0);
myInPipe = _MPUSBOpen(selection, vid_pid_norm, in_pipe, 1, 0);
}
public void ClosePipes()
{
_MPUSBClose(myOutPipe);
_MPUSBClose(myInPipe);
}
private void SendPacket(byte* SendData, DWORD SendLength)
{
uint SendDelay = 1000;
DWORD SentDataLength;
OpenPipes();
_MPUSBWrite(myOutPipe, (void*)SendData, SendLength, &SentDataLength, SendDelay);
ClosePipes();
}
private void ReceivePacket(byte* ReceiveData, DWORD *ReceiveLength)
{
uint ReceiveDelay=1000;
DWORD ExpectedReceiveLength = *ReceiveLength;
OpenPipes();
_MPUSBRead(myInPipe, (void*)ReceiveData, ExpectedReceiveLength, ReceiveLength, ReceiveDelay);
ClosePipes();
}