I see the client/server example where we have a client and a server and we communicate text messages back and forth.
I am having a hard time understanding how this works with actual devices that are using the CAN Bus.
Do I need a client? A server? A router? Connectionless?
I would like to form the payload of CAN Packets to conform to the instrument I'm trying to communicate with. What does my topology need to be?
I have a program something like this.
#define SERVER_ADDR 1
#define SERVER_PORT 10
#define CLIENT_ADDR 2
#define DEVICE_NAME "can0"
int main(int argc, char * argv[])
{
csp_iface_t * iface;
csp_conn_t * conn;
csp_packet_t * packet;
int snode, cnode, port;
int ret;
if (argc < 4)
{
printf ("arguments: server_node client_node port\n");
return -1;
}
else
{
snode = atoi(argv[1]);
cnode = atoi(argv[2]);
port = atoi(argv[2]);
}
printf ("snode %d\n", snode);
printf ("cnode %d\n", cnode);
printf ("port %d\n", port);
printf ("\n");
/* init */
csp_init();
/* open */
ret = csp_can_socketcan_open_and_add_interface(DEVICE_NAME, CSP_IF_CAN_DEFAULT_NAME, cnode, 1000000, true, &iface);
if (ret != CSP_ERR_NONE) {
csp_print("failed to open: %d\n", ret);
return 1;
}
iface->is_default = 1;
/* connect */
conn = csp_connect(CSP_PRIO_NORM, snode, port, 1000, CSP_O_NONE);
if (conn == NULL) {
csp_print("Connection failed\n");
return 1;
}
uint8_t get_payload[8] = {0x05, 0x00, 0xcd, 0x8f, 0x40, 0x00, 0x04};
/* prepare data */
packet = csp_buffer_get_always();
memcpy(packet->data, get_payload, 7);
packet->length = 7;
/* send */
csp_send(conn, packet);
while (1) {
/* Read incoming frame */
packet = csp_read(conn, 1000);
if (packet == NULL) {
break;
}
/* We have a reply, ensure data is 0 (zero) termianted */
const unsigned int length = (packet->length < sizeof(packet->data)) ? packet->length : (sizeof(packet->data) - 1);
packet->data[length] = 0;
csp_print("%s", packet->data);
/* Each packet from csp_read must to be freed by user */
csp_buffer_free(packet);
}
/* close */
csp_buffer_free(packet);
csp_close(conn);
return 0;
}