Hello Jan,
I tried the two methods for can read as illustrated in the following two programs:
Firts Option:
#include "mbed.h"
#include "EthernetInterface.h"
#define ECHO_SERVER_PORT 11000
//Threads
Thread UDP_Client_Thread(osPriorityNormal, 2000);
Thread UDP_Server_Thread(osPriorityNormal, 2000);
Thread t;
Thread r;
CAN can2(PB_8, PB_9);
CAN can1(PB_5, PB_6);
EventQueue queue(32 * EVENTS_EVENT_SIZE);
EventQueue queue_1(32 * EVENTS_EVENT_SIZE);
//Mutex
Mutex Network_Mutex;
Mutex RecIP_Mutex;
// Network interface
EthernetInterface net;
Serial pc(USBTX, USBRX);
// LEDs
DigitalOut led1(LED1);
DigitalOut led2(LED2);
DigitalOut led3(LED3);
static const char* mbedIP = "192.168.220.45"; //IP
static const char* mbedMask = "255.255.252.0"; // Mask
static const char* mbedGateway = "192.168.220.1"; //Gateway
static const char* recvIP = "192.168.220.100";
char counter = 0;
void send() {
printf("send()\r\n");
if(can1.write(CANMessage(1337, &counter, 1))) {
counter++;
printf("Message sent: %d\r\n", counter);
led3 = !led3;
}
}
void receive()
{
CANMessage msg;
while(true)
{
if(can2.read(msg))
{
printf("Message received: %d\r\n", msg.data[0]);
led2 = !led2;
}
}
}
//////////////////////////////////////////////////////////////////////////////UDP_Server///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void UDP_Server()
{
UDPSocket server;
Network_Mutex.lock();
server.open(&net);
Network_Mutex.unlock();
server.bind(10110);
SocketAddress sockAddr;
while(true)
{
char buffer[256];
pc.printf("\nWaiting for UDP packet...\r\n");
int n = server.recvfrom(&sockAddr, buffer, sizeof(buffer));
buffer[n] = '\0';
// pc.printf("Received packet from: %s\n", sockAddr.get_ip_address());
pc.printf("Packet contents : '%s'\r\n",buffer);
//pc.printf("Sending Packet back to Client\n");
server.sendto(sockAddr, buffer, n);
}
// Bring down the ethernet interface
//net.disconnect();
//pc.printf("Done\n");
}
//////////////////////////////////////////////////////////////////////////UDP_Client//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void UDP_Client()
{
//Buffer for output messages
const char out_buffer[] = "very important data";
//address of destination ip address and port
RecIP_Mutex.lock();
SocketAddress td_addr(recvIP, 11000);
RecIP_Mutex.unlock();
Network_Mutex.lock();
UDPSocket td_sock(&net);
Network_Mutex.unlock();
// Loop to send data to Socket address "td_sock"
while(true)
{
int ret = td_sock.sendto(td_addr, &out_buffer, sizeof(out_buffer));
if(ret < 0)
{
Thread::wait(500);
}
}
}
// Socket demo
int main() {
// Bring up the ethernet interface
pc.baud(9600);
//pc.printf("Ethernet socket example... \n");
net.set_network(mbedIP, mbedMask, mbedGateway);// my device address
can1.frequency(1000000); // set CAN bit rate to 1Mbps
can2.frequency(1000000); // set CAN bit rate to 1Mbps
if (0 != net.connect())
{
printf("Error connecting\n\r");
// ThisThread::sleep_for(1000);
return -1;
}
else
{
printf("Connected Ethernet \n\r");
led1=!led1;
}
UDP_Server_Thread.start(UDP_Server);
UDP_Client_Thread.start(UDP_Client);
t.start(callback(&queue, &EventQueue::dispatch_forever));
queue.call_every(500, send);
r.start(receive);
while(1)
{
ThisThread::sleep_for(500);
}
}
Second Option:
#include "mbed.h"
#include "EthernetInterface.h"
#define ECHO_SERVER_PORT 11000
//Threads
Thread UDP_Client_Thread(osPriorityNormal, 2000);
Thread UDP_Server_Thread(osPriorityNormal, 2000);
Thread t;
Thread r;
CAN can2(PB_8, PB_9);
CAN can1(PB_5, PB_6);
EventQueue queue(32 * EVENTS_EVENT_SIZE);
EventQueue queue_1(32 * EVENTS_EVENT_SIZE);
//Mutex
Mutex Network_Mutex;
Mutex RecIP_Mutex;
// Network interface
EthernetInterface net;
Serial pc(USBTX, USBRX);
// LEDs
DigitalOut led1(LED1);
DigitalOut led2(LED2);
DigitalOut led3(LED3);
static const char* mbedIP = "192.168.220.45"; //IP
static const char* mbedMask = "255.255.252.0"; // Mask
static const char* mbedGateway = "192.168.220.1"; //Gateway
static const char* recvIP = "192.168.220.100";
char counter = 0;
void send() {
printf("send()\r\n");
if(can1.write(CANMessage(1337, &counter, 1))) {
counter++;
printf("Message sent: %d\r\n", counter);
led3 = !led3;
}
}
void receive()
{
CANMessage msg;
while(true)
{
if(can2.read(msg))
{
printf("Message received: %d\r\n", msg.data[0]);
led2 = !led2;
}
}
}
//////////////////////////////////////////////////////////////////////////////UDP_Server///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void UDP_Server()
{
UDPSocket server;
Network_Mutex.lock();
server.open(&net);
Network_Mutex.unlock();
server.bind(10110);
SocketAddress sockAddr;
while(true)
{
char buffer[256];
pc.printf("\nWaiting for UDP packet...\r\n");
int n = server.recvfrom(&sockAddr, buffer, sizeof(buffer));
buffer[n] = '\0';
// pc.printf("Received packet from: %s\n", sockAddr.get_ip_address());
pc.printf("Packet contents : '%s'\r\n",buffer);
//pc.printf("Sending Packet back to Client\n");
server.sendto(sockAddr, buffer, n);
}
// Bring down the ethernet interface
//net.disconnect();
//pc.printf("Done\n");
}
//////////////////////////////////////////////////////////////////////////UDP_Client//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void UDP_Client()
{
//Buffer for output messages
const char out_buffer[] = "very important data";
//address of destination ip address and port
RecIP_Mutex.lock();
SocketAddress td_addr(recvIP, 11000);
RecIP_Mutex.unlock();
Network_Mutex.lock();
UDPSocket td_sock(&net);
Network_Mutex.unlock();
// Loop to send data to Socket address "td_sock"
while(true)
{
int ret = td_sock.sendto(td_addr, &out_buffer, sizeof(out_buffer));
if(ret < 0)
{
Thread::wait(500);
}
}
}
// Socket demo
int main() {
// Bring up the ethernet interface
pc.baud(9600);
//pc.printf("Ethernet socket example... \n");
net.set_network(mbedIP, mbedMask, mbedGateway);// my device address
can1.frequency(1000000); // set CAN bit rate to 1Mbps
can2.frequency(1000000); // set CAN bit rate to 1Mbps
if (0 != net.connect())
{
printf("Error connecting\n\r");
// ThisThread::sleep_for(1000);
return -1;
}
else
{
printf("Connected Ethernet \n\r");
led1=!led1;
}
UDP_Server_Thread.start(UDP_Server);
UDP_Client_Thread.start(UDP_Client);
t.start(callback(&queue, &EventQueue::dispatch_forever));
queue.call_every(500, send);
r.start(callback(&queue, &EventQueue::dispatch_forever));
can2.attach(queue.event(receive));
while(1)
{
ThisThread::sleep_for(500);
}
}
Both option didn’t work with me as the sending will send only 3 messages and stop and the receiving part not working.
I tried to put both of them in defferent threads but still.
Please your advise.
Regards
Nada