A program that allows you to send CANMessages from your PC via ethernet.

Dependencies:   EthernetNetIf mbed

Committer:
OTBsolar
Date:
Tue Mar 22 13:51:59 2011 +0000
Revision:
0:4abab30c3917
V0,2
BETA

Who changed what in which revision?

UserRevisionLine numberNew contents of line
OTBsolar 0:4abab30c3917 1 #include "mbed.h"
OTBsolar 0:4abab30c3917 2 #include "CAN.h"
OTBsolar 0:4abab30c3917 3 #include "stdio.h"
OTBsolar 0:4abab30c3917 4 #include "stdlib.h"
OTBsolar 0:4abab30c3917 5 #include "string.h"
OTBsolar 0:4abab30c3917 6 #include "EthernetNetIf.h"
OTBsolar 0:4abab30c3917 7 #include "TCPSocket.h"
OTBsolar 0:4abab30c3917 8
OTBsolar 0:4abab30c3917 9 Ticker ticker;
OTBsolar 0:4abab30c3917 10 Serial pc(USBTX, USBRX);
OTBsolar 0:4abab30c3917 11 EthernetNetIf eth;
OTBsolar 0:4abab30c3917 12 DigitalOut write_activity(LED1); //CAN activity
OTBsolar 0:4abab30c3917 13 DigitalOut read_activity(LED2);
OTBsolar 0:4abab30c3917 14 DigitalOut ethernet_activity(LED3); //Ethernet activity
OTBsolar 0:4abab30c3917 15 DigitalOut live(LED4); //program running
OTBsolar 0:4abab30c3917 16 CAN can1(p9, p10); // rd, td Transmitter
OTBsolar 0:4abab30c3917 17 CAN can2(p30, p29); // rd, td Monitor
OTBsolar 0:4abab30c3917 18
OTBsolar 0:4abab30c3917 19 TCPSocket tcp; //The listening port where requests are queued
OTBsolar 0:4abab30c3917 20 TCPSocket* link; //The port where accepted requests can communicate
OTBsolar 0:4abab30c3917 21
OTBsolar 0:4abab30c3917 22 Host local(IpAddr(130,144,3,9), 24682); //mbed IP
OTBsolar 0:4abab30c3917 23 Host client;
OTBsolar 0:4abab30c3917 24
OTBsolar 0:4abab30c3917 25 TCPSocketErr accErr;
OTBsolar 0:4abab30c3917 26
OTBsolar 0:4abab30c3917 27 int counter = 0;
OTBsolar 0:4abab30c3917 28 int shift = 0;
OTBsolar 0:4abab30c3917 29 char candata[8];
OTBsolar 0:4abab30c3917 30 char pc_msg[128];
OTBsolar 0:4abab30c3917 31 char pc_canmsg[128];
OTBsolar 0:4abab30c3917 32 CANType pc_type;
OTBsolar 0:4abab30c3917 33 CANFormat pc_format;
OTBsolar 0:4abab30c3917 34 int pc_ID; //standard 11bit ID
OTBsolar 0:4abab30c3917 35 //int pc_IDe; //extended 29 (not used yet)
OTBsolar 0:4abab30c3917 36 int pc_length;
OTBsolar 0:4abab30c3917 37 int pcd0; int pcd1; int pcd2; int pcd3; int pcd4; int pcd5; int pcd6; int pcd7; //8 bytes data
OTBsolar 0:4abab30c3917 38 CANMessage msg;
OTBsolar 0:4abab30c3917 39 int size;
OTBsolar 0:4abab30c3917 40 char test;
OTBsolar 0:4abab30c3917 41 char *dump;
OTBsolar 0:4abab30c3917 42 char *buffer;
OTBsolar 0:4abab30c3917 43
OTBsolar 0:4abab30c3917 44
OTBsolar 0:4abab30c3917 45 /************************************************************************************
OTBsolar 0:4abab30c3917 46 * CAN STUFF *
OTBsolar 0:4abab30c3917 47 ************************************************************************************/
OTBsolar 0:4abab30c3917 48 void setdata(int d0, int d1, int d2, int d3, int d4, int d5, int d6, int d7) {
OTBsolar 0:4abab30c3917 49 candata[0] = (char) (d0); // LSB
OTBsolar 0:4abab30c3917 50 candata[1] = (char) (d1);
OTBsolar 0:4abab30c3917 51 candata[2] = (char) (d2);
OTBsolar 0:4abab30c3917 52 candata[3] = (char) (d3);
OTBsolar 0:4abab30c3917 53 candata[4] = (char) (d4);
OTBsolar 0:4abab30c3917 54 candata[5] = (char) (d5);
OTBsolar 0:4abab30c3917 55 candata[6] = (char) (d6);
OTBsolar 0:4abab30c3917 56 candata[7] = (char) (d7); // MSB
OTBsolar 0:4abab30c3917 57 }
OTBsolar 0:4abab30c3917 58
OTBsolar 0:4abab30c3917 59 void canread() {
OTBsolar 0:4abab30c3917 60 pc.printf("Read: [ ID: %d", msg.id);
OTBsolar 0:4abab30c3917 61 pc.printf(" Length: %d", msg.len);
OTBsolar 0:4abab30c3917 62 pc.printf(" Data: %x", msg.data[0]);
OTBsolar 0:4abab30c3917 63 pc.printf(" %x", msg.data[1]);
OTBsolar 0:4abab30c3917 64 pc.printf(" %x", msg.data[2]);
OTBsolar 0:4abab30c3917 65 pc.printf(" %x", msg.data[3]);
OTBsolar 0:4abab30c3917 66 pc.printf(" %x", msg.data[4]);
OTBsolar 0:4abab30c3917 67 pc.printf(" %x", msg.data[5]);
OTBsolar 0:4abab30c3917 68 pc.printf(" %x", msg.data[6]);
OTBsolar 0:4abab30c3917 69 pc.printf(" %x", msg.data[7]);
OTBsolar 0:4abab30c3917 70 pc.printf(" Type: %d", msg.type);
OTBsolar 0:4abab30c3917 71 pc.printf(" Format: %d ]\n", msg.format);
OTBsolar 0:4abab30c3917 72
OTBsolar 0:4abab30c3917 73 read_activity = !read_activity; //Blink!
OTBsolar 0:4abab30c3917 74
OTBsolar 0:4abab30c3917 75 sprintf(pc_canmsg,"!%d %d %d %d %x %x %x %x %x %x %x %x",
OTBsolar 0:4abab30c3917 76 msg.format, msg.type, msg.id, msg.len,
OTBsolar 0:4abab30c3917 77 msg.data[0], msg.data[1], msg.data[2], msg.data[3],
OTBsolar 0:4abab30c3917 78 msg.data[4], msg.data[5], msg.data[6], msg.data[7]);
OTBsolar 0:4abab30c3917 79 link->send(pc_canmsg,sizeof(pc_canmsg));
OTBsolar 0:4abab30c3917 80
OTBsolar 0:4abab30c3917 81 }
OTBsolar 0:4abab30c3917 82
OTBsolar 0:4abab30c3917 83 void pc_msg_read() {
OTBsolar 0:4abab30c3917 84 // Data to be sent as ("_" = space):
OTBsolar 0:4abab30c3917 85 //!<format>_<type>_<ID>_<length>_<d0>_<d1>_<d2>_<d3>_<d4>_<d5>_<d6>_<d7>
OTBsolar 0:4abab30c3917 86 for(shift=0;shift<128;shift++) {
OTBsolar 0:4abab30c3917 87 pc_msg[shift] = pc_msg[shift+1];
OTBsolar 0:4abab30c3917 88 }
OTBsolar 0:4abab30c3917 89 // Read pc_msg and extract all data
OTBsolar 0:4abab30c3917 90 sscanf(pc_msg,"%d %d %d %d %x %x %x %x %x %x %x %x",
OTBsolar 0:4abab30c3917 91 &pc_format, &pc_type, &pc_ID, &pc_length,
OTBsolar 0:4abab30c3917 92 &pcd0, &pcd1, &pcd2, &pcd3, &pcd4, &pcd5, &pcd6, &pcd7);
OTBsolar 0:4abab30c3917 93
OTBsolar 0:4abab30c3917 94 // Printing extracted data, mostly for testing
OTBsolar 0:4abab30c3917 95 pc.printf("Entered: [ ID: %d ",pc_ID);
OTBsolar 0:4abab30c3917 96 pc.printf("length: %d ",pc_length);
OTBsolar 0:4abab30c3917 97 pc.printf("data: %x %x %x %x %x %x %x %x ",
OTBsolar 0:4abab30c3917 98 pcd0, pcd1, pcd2, pcd3, pcd4, pcd5, pcd6, pcd7);
OTBsolar 0:4abab30c3917 99 pc.printf("type: %d ",pc_type);
OTBsolar 0:4abab30c3917 100 pc.printf("format: %d ]\n",pc_format);
OTBsolar 0:4abab30c3917 101
OTBsolar 0:4abab30c3917 102 // Setting the data to CANMessage.data format
OTBsolar 0:4abab30c3917 103 setdata(pcd0, pcd1, pcd2, pcd3, pcd4, pcd5, pcd6, pcd7);
OTBsolar 0:4abab30c3917 104
OTBsolar 0:4abab30c3917 105 // Transmitting CANMessage
OTBsolar 0:4abab30c3917 106 if(pc_type==0) {
OTBsolar 0:4abab30c3917 107 if(can1.write(CANMessage(pc_ID,candata,(char)pc_length,pc_type,pc_format))) {
OTBsolar 0:4abab30c3917 108 pc.printf("MBED: [ Message compiled and sent. ]\n");
OTBsolar 0:4abab30c3917 109 write_activity = !write_activity;
OTBsolar 0:4abab30c3917 110 }
OTBsolar 0:4abab30c3917 111 }
OTBsolar 0:4abab30c3917 112 else if(pc_type==1) {
OTBsolar 0:4abab30c3917 113 if(can1.write(CANMessage(pc_ID,pc_format))) {
OTBsolar 0:4abab30c3917 114 pc.printf("MBED: [ RTR Message compiled and sent. ]\n");
OTBsolar 0:4abab30c3917 115 write_activity = !write_activity;
OTBsolar 0:4abab30c3917 116 }
OTBsolar 0:4abab30c3917 117 }
OTBsolar 0:4abab30c3917 118 }
OTBsolar 0:4abab30c3917 119
OTBsolar 0:4abab30c3917 120
OTBsolar 0:4abab30c3917 121 /************************************************************************************
OTBsolar 0:4abab30c3917 122 * TCP STUFF *
OTBsolar 0:4abab30c3917 123 ************************************************************************************/
OTBsolar 0:4abab30c3917 124 void eth_act() {
OTBsolar 0:4abab30c3917 125 ethernet_activity = !ethernet_activity;
OTBsolar 0:4abab30c3917 126 }
OTBsolar 0:4abab30c3917 127
OTBsolar 0:4abab30c3917 128 void tcperrcheck(TCPSocketErr tcpsocketerr,char *phase) {
OTBsolar 0:4abab30c3917 129 switch(tcpsocketerr) {
OTBsolar 0:4abab30c3917 130 case TCPSOCKET_SETUP: printf("Err:Setup\n"); break; //TCPSocket not properly configured.
OTBsolar 0:4abab30c3917 131 case TCPSOCKET_TIMEOUT: printf("Err:Timeout\n"); break; //Connection timed out.
OTBsolar 0:4abab30c3917 132 case TCPSOCKET_IF: printf("Err:Interface\n"); break; //Interface has problems, does not exist or is not initialized.
OTBsolar 0:4abab30c3917 133 case TCPSOCKET_MEM: printf("Err:Memory\n"); break; //Not enough mem.
OTBsolar 0:4abab30c3917 134 case TCPSOCKET_INUSE: printf("Err:In use\n"); break; //Interface / Port is in use.
OTBsolar 0:4abab30c3917 135 case TCPSOCKET_EMPTY: printf("Err:Empty\n"); break; //Connections queue is empty.
OTBsolar 0:4abab30c3917 136 case TCPSOCKET_RST: printf("Err:Reset\n"); break; //Connection was reset by remote host.
OTBsolar 0:4abab30c3917 137 case TCPSOCKET_OK: if(phase == "bind") printf("Bound to port\n");
OTBsolar 0:4abab30c3917 138 if(phase == "listen") printf("Listening\n");
OTBsolar 0:4abab30c3917 139 if(phase == "accept") printf("Accepted: ");
OTBsolar 0:4abab30c3917 140 break; //Success.
OTBsolar 0:4abab30c3917 141 }
OTBsolar 0:4abab30c3917 142 }
OTBsolar 0:4abab30c3917 143
OTBsolar 0:4abab30c3917 144 void onLinkSocketEvent(TCPSocketEvent e) {
OTBsolar 0:4abab30c3917 145 switch(e) {
OTBsolar 0:4abab30c3917 146 case TCPSOCKET_CONNECTED: eth_act(); printf("TCP Socket Connected\r\n"); break;
OTBsolar 0:4abab30c3917 147 case TCPSOCKET_WRITEABLE: eth_act(); printf("TCP Socket Writable\r\n"); break;
OTBsolar 0:4abab30c3917 148 case TCPSOCKET_READABLE: {
OTBsolar 0:4abab30c3917 149 //Can now read some data...
OTBsolar 0:4abab30c3917 150 eth_act();
OTBsolar 0:4abab30c3917 151 printf("TCP Socket Readable\r\n");
OTBsolar 0:4abab30c3917 152 // Read in any available data into the buffer
OTBsolar 0:4abab30c3917 153 int len = link->recv(pc_msg, 128);
OTBsolar 0:4abab30c3917 154 test = pc_msg[0];
OTBsolar 0:4abab30c3917 155 if (test == '!') { // See if it's a valid message
OTBsolar 0:4abab30c3917 156 pc_msg_read(); // Valid => read the message and extract the data
OTBsolar 0:4abab30c3917 157 }
OTBsolar 0:4abab30c3917 158 else { // Invalid data or leftover characters
OTBsolar 0:4abab30c3917 159 pc.printf("Dumped:%s\n",pc_msg);
OTBsolar 0:4abab30c3917 160 memset(pc_msg, 0, sizeof(pc_msg));
OTBsolar 0:4abab30c3917 161 printf("%s",pc_msg);
OTBsolar 0:4abab30c3917 162 }
OTBsolar 0:4abab30c3917 163 }
OTBsolar 0:4abab30c3917 164 break;
OTBsolar 0:4abab30c3917 165 case TCPSOCKET_CONTIMEOUT: eth_act(); printf("TCP Socket Timeout\r\n"); break;
OTBsolar 0:4abab30c3917 166 case TCPSOCKET_CONRST: eth_act(); printf("TCP Socket CONRST\r\n"); break;
OTBsolar 0:4abab30c3917 167 case TCPSOCKET_CONABRT: eth_act(); printf("TCP Socket CONABRT\r\n"); break;
OTBsolar 0:4abab30c3917 168 case TCPSOCKET_ERROR: eth_act(); printf("TCP Socket Error\r\n"); break;
OTBsolar 0:4abab30c3917 169 case TCPSOCKET_DISCONNECTED: {
OTBsolar 0:4abab30c3917 170 //Close socket...
OTBsolar 0:4abab30c3917 171 printf("TCP Socket Disconnected\r\n");
OTBsolar 0:4abab30c3917 172 link->close();
OTBsolar 0:4abab30c3917 173 }
OTBsolar 0:4abab30c3917 174 break;
OTBsolar 0:4abab30c3917 175 default: printf("DEFAULT\r\n");
OTBsolar 0:4abab30c3917 176 }
OTBsolar 0:4abab30c3917 177 }
OTBsolar 0:4abab30c3917 178
OTBsolar 0:4abab30c3917 179 void onTCPSocketEvent(TCPSocketEvent e) {
OTBsolar 0:4abab30c3917 180 switch(e) {
OTBsolar 0:4abab30c3917 181 case TCPSOCKET_CONNECTED: eth_act(); printf("Connected\n"); break;
OTBsolar 0:4abab30c3917 182 case TCPSOCKET_ACCEPT: {
OTBsolar 0:4abab30c3917 183 eth_act();
OTBsolar 0:4abab30c3917 184 accErr = tcp.accept(&client,&link);
OTBsolar 0:4abab30c3917 185 tcperrcheck(accErr,"accept");
OTBsolar 0:4abab30c3917 186 link->setOnEvent(&onLinkSocketEvent);
OTBsolar 0:4abab30c3917 187 IpAddr clientIp = client.getIp();
OTBsolar 0:4abab30c3917 188 printf("Incoming TCP connection from %d.%d.%d.%d\r\n",
OTBsolar 0:4abab30c3917 189 clientIp[0], clientIp[1], clientIp[2], clientIp[3]);
OTBsolar 0:4abab30c3917 190 }
OTBsolar 0:4abab30c3917 191 break;
OTBsolar 0:4abab30c3917 192 case TCPSOCKET_READABLE: eth_act(); printf("Readable\n"); break;
OTBsolar 0:4abab30c3917 193 case TCPSOCKET_WRITEABLE: eth_act(); printf("Writeable\n"); break;
OTBsolar 0:4abab30c3917 194 case TCPSOCKET_CONTIMEOUT: eth_act(); printf("Timeout\n"); break;
OTBsolar 0:4abab30c3917 195 case TCPSOCKET_CONRST: eth_act(); printf("Reset\n"); break;
OTBsolar 0:4abab30c3917 196 case TCPSOCKET_CONABRT: eth_act(); printf("Aborted\n"); break;
OTBsolar 0:4abab30c3917 197 case TCPSOCKET_ERROR: eth_act(); printf("Error\n"); break;
OTBsolar 0:4abab30c3917 198 case TCPSOCKET_DISCONNECTED: eth_act(); printf("Disconnected\n"); tcp.close(); break;
OTBsolar 0:4abab30c3917 199 }
OTBsolar 0:4abab30c3917 200 }
OTBsolar 0:4abab30c3917 201
OTBsolar 0:4abab30c3917 202 int main() {
OTBsolar 0:4abab30c3917 203 //----------------Initialization-----------------------
OTBsolar 0:4abab30c3917 204 can2.frequency(1000000); //kbit/s
OTBsolar 0:4abab30c3917 205 can1.frequency(1000000);
OTBsolar 0:4abab30c3917 206 can2.monitor(1); //Works without this, in my case.
OTBsolar 0:4abab30c3917 207 pc.baud(115200); //Tested, works. Set terminal to the same. Also works with the c file.
OTBsolar 0:4abab30c3917 208
OTBsolar 0:4abab30c3917 209 //Ethernet setup
OTBsolar 0:4abab30c3917 210 printf("Setting up ethernet\n");
OTBsolar 0:4abab30c3917 211 EthernetErr ethErr = eth.setup();
OTBsolar 0:4abab30c3917 212 if (ethErr) {
OTBsolar 0:4abab30c3917 213 printf("Error %d in setup.\n", ethErr);
OTBsolar 0:4abab30c3917 214 return -1;
OTBsolar 0:4abab30c3917 215 }
OTBsolar 0:4abab30c3917 216 IpAddr ip = eth.getIp();
OTBsolar 0:4abab30c3917 217 printf("Setup OK. ");
OTBsolar 0:4abab30c3917 218 printf("MBED IP is %d.%d.%d.%d\n", ip[0], ip[1], ip[2], ip[3]);
OTBsolar 0:4abab30c3917 219
OTBsolar 0:4abab30c3917 220 //Generate method to deal with requests
OTBsolar 0:4abab30c3917 221 tcp.setOnEvent(&onTCPSocketEvent);
OTBsolar 0:4abab30c3917 222
OTBsolar 0:4abab30c3917 223 //Bind to local port
OTBsolar 0:4abab30c3917 224 printf("Init bind... ");
OTBsolar 0:4abab30c3917 225 TCPSocketErr bindErr = tcp.bind(local);
OTBsolar 0:4abab30c3917 226 tcperrcheck(bindErr, "bind");
OTBsolar 0:4abab30c3917 227
OTBsolar 0:4abab30c3917 228 //Listen to local port
OTBsolar 0:4abab30c3917 229 printf("Init listen... ");
OTBsolar 0:4abab30c3917 230 TCPSocketErr listenErr = tcp.listen();
OTBsolar 0:4abab30c3917 231 tcperrcheck(listenErr, "listen");
OTBsolar 0:4abab30c3917 232
OTBsolar 0:4abab30c3917 233 //Start timer
OTBsolar 0:4abab30c3917 234 Timer tmr;
OTBsolar 0:4abab30c3917 235 tmr.start();
OTBsolar 0:4abab30c3917 236
OTBsolar 0:4abab30c3917 237
OTBsolar 0:4abab30c3917 238 pc.printf("Please enter !<format>_<type>_<ID>_<length>_<d0>_<d1>_<d2>_<d3>_<d4>_<d5>_<d6>_<d7> ('_' = space)\n");
OTBsolar 0:4abab30c3917 239 while(1) {
OTBsolar 0:4abab30c3917 240 Net::poll();
OTBsolar 0:4abab30c3917 241 if(tmr.read() > 1) //Every second
OTBsolar 0:4abab30c3917 242 {
OTBsolar 0:4abab30c3917 243 tmr.reset();
OTBsolar 0:4abab30c3917 244 live=!live; //Show that we are alive
OTBsolar 0:4abab30c3917 245 }
OTBsolar 0:4abab30c3917 246 if (can2.read(msg)) {
OTBsolar 0:4abab30c3917 247 canread(); //Read when interupted.
OTBsolar 0:4abab30c3917 248 }
OTBsolar 0:4abab30c3917 249 }
OTBsolar 0:4abab30c3917 250 }