CANopen 块传输通信详解

这篇文章主要是记录CANopen 块传输协议的基本思路,具体代码各位可以根据思路跟进源码阅读,这里只贴出主要内容代码~

目录

1.块上传详解(读取)

1).主节点:发送读取数据初始化

2).从节点:服务器端响应”需要读取的总数据包字节数” 

3).主节点:收到响应后,发送命令,表示可以开始传输了

4).从节点:开始循环向主节点发送数据包

5).主节点:告诉从节点,我收到了多少条包!

6).从节点:告诉主节点,已经发送完毕

7).主节点:告诉从节点,我也接收完毕了,整个传输完成!

2.块下载详解(写入)

1).主节点:告诉从节点我要写入多少个字节数

2).从节点:我准备好了,可以写入数据

3).主节点:收到响应后,开始逐条发送数据,无需响应

4).从节点:告诉主节点我收到了多少条

5).主节点:告诉从节点,写入完毕!

6).从节点:告诉主节点,我知道写入完毕了


1.块上传详解(读取)

 

 CANopen 块传输通信详解_第1张图片

 

这里以图2为例子, 进行说明

假设主节点为01,待读取节点为0x1b

读取的数据是

UNS8 g_upgrade_buf_appbin[258]="bbbcccddd111222333444555666777888999AAA\0";

/* Mapped at index 0x2007, subindex 0x01 */

 

1).主节点:发送读取数据初始化

发送:

A0 07 20 01 10 00 00 00

data[0] = (5 << 5) | SDO_BCS_INITIATE_UPLOAD_REQUEST;

data[1] = index & 0xFF;        /* LSB */

data[2] = (index >> 8) & 0xFF; /* MSB */

data[3] = subIndex;

data[4] = SDO_BLOCK_SIZE;

for (i = 5 ; i < 8 ; i++)

data[i] = 0;

 

2).从节点:服务器端响应”需要读取的总数据包字节数” 

接收:

C2  07 20 01 27 00 00 00

data[0] = (6 << 5) | (1 << 1) | SDO_BSS_INITIATE_UPLOAD_RESPONSE;

data[1] = index & 0xFF;        /* LSB */

data[2] = (index >> 8) & 0xFF; /* MSB */

data[3] = subIndex;

data[4] = (UNS8) nbBytes;

data[5] = (UNS8) (nbBytes >> 8);

data[6] = (UNS8) (nbBytes >> 16);

data[7] = (UNS8) (nbBytes >> 24);

 

3).主节点:收到响应后,发送命令,表示可以开始传输了

发送:

A3 00 00 00  00 00 00 00

data[0] = (5 << 5) | SDO_BCS_START_UPLOAD;

for (i = 1 ; i < 8 ; i++)

data[i] = 0;

 

4).从节点:开始循环向主节点发送数据包

接收:

01 62 62 62 63 63 63 64

02 64 64 31 31 31 32 32

03 32 33 33 33 34 34 34

04 35 35 35 36 36 36 37

05 37 37 38 38 38 39 39

86 39 41 41 41 00 00 00

data[0] = SeqNo;

err = lineToSDO(d, line, 7, data + 1);

 

5).主节点:告诉从节点,我收到了多少条包!

发送:

A2 06 10 00  00 00 00 00 

data[0] = (5 << 5) | SDO_BCS_UPLOAD_RESPONSE;

data[1] = d->transfers[line].seqno;

data[2] = SDO_BLOCK_SIZE;

data[3] = data[4] = data[5] = data[6] = data[7] = 0;

 

6).从节点:告诉主节点,已经发送完毕

接收:

Cd 00 00 00 00 00 00 00

data[0] = (6 << 5) | ((d->transfers[line].endfield) << 2)|SDO_BCS_END_DOWNLOAD_REQUEST;

for (i = 1 ; i < 8 ; i++)

data[i] = 0;

 

7).主节点:告诉从节点,我也接收完毕了,整个传输完成!

发送:

A1 00 00 00  00 00 00 00

data[0] = (5 << 5) | SDO_BCS_END_UPLOAD_REQUEST;

for (i = 1 ; i < 8 ; i++)

data[i] = 0;

 

2.块下载详解(写入)

 CANopen 块传输通信详解_第2张图片

图3

这里以图3为例子, 进行说明

假设主节点为01,待写入节点为0x1b

假如待写入的数据是

UUNS8 write_net_block_buf[] =  "0123456789aaabbbcccddd\0";

/* Mapped at index 0x2007, subindex 0x01 */

 

1).主节点:告诉从节点我要写入多少个字节数

发送:

C2 07 20 01 18 00 00 00

buf[0] = (6 << 5) | (1 << 1 );   /* CCS = 6 , CC = 0 , S = 1 , CS = 0 */

buf[1] = index & 0xFF;        /* LSB */

buf[2] = (index >> 8) & 0xFF; /* MSB */

buf[3] = subIndex;

for (i = 0 ; i < 4 ; i++)

buf[i+4] = (UNS8)((count >> (i<<3))); /* i*8 */

 

2).从节点:我准备好了,可以写入数据

接收:

A0 07 20 01 10 00 00 00

data[0] = (5 << 5) | SDO_BSS_INITIATE_DOWNLOAD_RESPONSE;

data[1] = (UNS8) index;        /* LSB */

data[2] = (UNS8) (index >> 8); /* MSB */

data[3] = subIndex;

data[4] = SDO_BLOCK_SIZE;

data[5] = data[6] = data[7] = 0;

 

3).主节点:收到响应后,开始逐条发送数据,无需响应

发送:

01 30 31 32 33 34 35 36

02 37 38 39 61 61 61 62

03 62 62 63 63 63 64 65

84 64 00 00 00 00 00 00

当实际发送字节大于7个字节时:

data[0] = SeqNo;

err = lineToSDO(d, line, 7, data + 1);

剩余的小于7个字节的发送时:

data[0] = 0x80 | SeqNo;

err = lineToSDO(d, line, nbBytes, data + 1); //实际的剩余字节

for (i = nbBytes + 1 ; i < 8 ; i++) //多余的空间填0

 data[i] = 0;

 

4).从节点:告诉主节点我收到了多少条

接收:

A2 04 10 00 00 00 00 00

data[0] = (5 << 5) | SDO_BSS_DOWNLOAD_RESPONSE;

data[1] = d->transfers[line].seqno;

data[2] = SDO_BLOCK_SIZE;

data[3] = data[4] = data[5] = data[6] = data[7] = 0;

 

5).主节点:告诉从节点,写入完毕!

发送:

D1 00 00 00  00 00 00 00

data[0] = (6 << 5) | ((d->transfers[line].endfield) << 2) | SDO_BCS_END_DOWNLOAD_REQUEST;

for (i = 1 ; i < 8 ; i++)

data[i] = 0;

 

6).从节点:告诉主节点,我知道写入完毕了

将传输结构体里面的数据拷贝到数据字典里,并复位SDO传输!

接收:

A1 00 00 00 00 00 00 00

data[0] = (5 << 5) | SDO_BSS_END_DOWNLOAD_RESPONSE;

for (i = 1 ; i < 8 ; i++)

data[i] = 0;

 

本文原创,转载请注明出处!!!谢谢

 

你可能感兴趣的:(CANopen)