CB34EX problem
Posted: Wed Nov 30, 2011 9:25 am
Hi everybody,
I'm trying to make an Ethernet to CAN bridge using CB34EX. But i'm facing a very strange problem
On one side I have an UDP java "server" that delivers each #3 milliseconds 64bits of random data to send over UDP. On the other side I have the netburner that is waiting for UDP packets. If it receives some data, it sends them on CAN bus.
Here are the two little programs :
Java "server" (run method of the thread) :
Netburner C code :
Most of the time, everything is working fine for several seconds (#100secs) and then it stops unexpectedly with error CAN_CHANNEL_USED (led1 is red) and less than 16 free channels (led2 is red).
If I replace DONT_WAIT with any other value, than the netburner hangs, stops, (after several seconds) never returning from SendMessage function and the CAN port sends always the same values.
I've not been able to find my error.
Any help would be very appreciated.
Thanks a lot,
frank
I'm trying to make an Ethernet to CAN bridge using CB34EX. But i'm facing a very strange problem

Here are the two little programs :
Java "server" (run method of the thread) :
Code: Select all
public void run() {
while (true) {
int size = 8;//(int)(random.nextFloat()*8) + 1;
bytesSent += size;
System.out.println(bytesSent / 8);
byte[] byteBuffer = new byte[size];
random.nextBytes(byteBuffer);
DatagramPacket dgPacket = new DatagramPacket(byteBuffer, byteBuffer.length, destinationAddress, destinationPort);
try {
dgSocket.send(dgPacket);
} catch (IOException e) {
e.printStackTrace();
}
long t = System.nanoTime();
while((System.nanoTime() - t) < 3*1000000);
}
}
Code: Select all
#include "predef.h"
#include <stdio.h>
#include <ctype.h>
#include <startnet.h>
#include <autoupdate.h>
#include <canif.h>
#include <udp.h>
#define LED1_GREEN (0x80)
#define LED1_RED (0x40)
#define LED1_OFF (0xC0)
#define LED2_GREEN (0x20)
#define LED2_RED (0x10)
#define LED2_OFF (0x30)
extern "C" {
void UserMain(void * pd);
}
const char * AppName="CanEthernet";
void UserMain(void * pd) {
InitializeStack();
OSChangePrio(MAIN_PRIO);
EnableAutoUpdate();
StartHTTP();
/*
* CAN initialisation : must be done before any CAN function (except CanShutDown)
* 1Mhz, null global mask (each message is accepteds) and default IRQ level = 4
*/
int canResponse = CanInit(1000000, 0x0000);
/*
* Prepare FIFO to recieve messages from UDP
*/
OS_FIFO udpFifo;
OSFifoInit(&udpFifo);
/*
* Register UDP fifo using listen port parameter
*/
RegisterUDPFifo(15001, &udpFifo);
int freeCanChannel = FreeCanChannels();
if(freeCanChannel == 16) putleds( LED2_GREEN );
putleds( LED1_GREEN );
while (canResponse == CAN_OK) {
{//This is UDP receive scope stuff input
/*
* create UDP packet waiting until something is comming from port
*/
UDPPacket pkt(&udpFifo, 0);
if(pkt.Validate()) {
WORD nbBytesRead = pkt.GetDataSize();
PBYTE udpByteBuffer = pkt.GetDataBuffer();
//If we are here, it's because something is comming from UDP
//Send it via CAN with message ID 1
int currentPosition = 0;
int bytesToSend = 0;
while(currentPosition < nbBytesRead) {
//Send message length of maximum 8 bytes
bytesToSend = nbBytesRead - currentPosition;
if(bytesToSend > 8) bytesToSend = 8;
canResponse = SendMessage(NormToNbId(1), &udpByteBuffer[currentPosition], bytesToSend, DONT_WAIT, 0);
currentPosition += bytesToSend;
//USER_ENTER_CRITICAL();
//for(int i = 0; i<1000; i++) asm("nop");
//USER_EXIT_CRITICAL();
}
}
}
}
freeCanChannel = FreeCanChannels();
putleds( LED2_OFF );
putleds( LED1_OFF );
if(freeCanChannel < 16) putleds( LED2_RED );
if(canResponse == CAN_CHANNEL_USED) putleds( LED1_RED );
CanShutDown();
UnregisterUDPFifo(15001);
}
If I replace DONT_WAIT with any other value, than the netburner hangs, stops, (after several seconds) never returning from SendMessage function and the CAN port sends always the same values.
I've not been able to find my error.
Any help would be very appreciated.
Thanks a lot,
frank