Content originally posted in LPCWare by TKoe on Wed Jul 18 07:32:41 MST 2012 Hello!
The nxpusblib seemed to have trouble in its current version with the length of the received bytes in the VirtualSerial mode. A patch by Vu Nguyen (http://lpcware.com/content/forum/nxpusblib-dualvirtualserial-example#comment-1419) fixed that. However, now I have the problem that it doesn't really receive any data anymore after I send data that's bigger than the maximum packet length minus one to the device. I set that to 64, so sending 63 bytes works fine, but one more and the data doesn't arrive in the end program. For testing I made it so that my program will always send 63 Bytes, but any more and it stops working...
Here's the code that reads the received data. The FIFO-functions that I have were tested on other projects and work flawlessly. the FIFO is initialized to a size of 1000 bytes. So whenever I get any data I push it into that FIFO which is read by a very simple state machine which basically just waits until enough data is present in the FIFO to continue operation. <code> void Get_USB_Data(void) { uint16_t bytes_rec = CDC_Device_BytesReceived(&VirtualSerial_CDC_Interface); if (bytes_rec > 0) { FIFO_push_byte(&RxFIFO, CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface)); } }
if (state == 1) { if (FIFO_pop(&RxFIFO, data, 1)) { /* See what data[0] contains and switch state */ } } /* ..... */ else if (state == 5) { if (FIFO_pop(&RxFIFO, data, expected_length)) { /* got all data, return to wait */ state = 1; } } }
int main() { Setup_Everything();
for (;;) { Get_USB_Data(); simple_state_machine(); USB_USBTask(); } } </code>
And here's the C# code of the program that sends the data: <code> SerialPort p = new SerialPort("COM14", 115200, Parity.None, 8, StopBits.One); int DATA_LEN = 63; byte[] data = new byte[DATA_LEN]; byte[] get_data = new byte[DATA_LEN];
byte[] data_len = new byte[4]; data_len[0] = (byte)((DATA_LEN >> 24) & 0xFF); data_len[1] = (byte)((DATA_LEN >> 16) & 0xFF); data_len[2] = (byte)((DATA_LEN >> 8) & 0xFF); data_len[3] = (byte)(DATA_LEN & 0xFF); Random rand = new Random(); // Fill Tx-array with random data for (int i = 0; i < DATA_LEN; i++) { data = (byte)rand.Next(0xFF); }
p.Open(); // Get ready to receive data p.Write(new byte[] {0xAA}, 0, 1);
// Transmit data length that the device will receive p.Write(data_len, 0, 4);
// Transmit data p.Write(data, 0, DATA_LEN);
// Tell the device to transmit saved data back to the PC p.Write(new byte[] {0xBB}, 0, 1);
// Receive data for (int g = 0; g < DATA_LEN; g++) { get_data[g] = (byte)(p.ReadByte() & 0xFF); }
/* Checking if data and get_data are equal here */ p.Close(); </code> Now if DATA_LEN is equal to or smaller than 63 everything works as expected. But as soon as I set it to 64 or higher the device doesn't seem to get the data as it is stuck in the "receive data" state.