11
This commit is contained in:
commit
53c85e94af
393 files changed
+141844
No files matched your search
@@ -0,0 +1,146 @@
|
||||
#include <stdio.h>
|
||||
#include "loopback2.h"
|
||||
#include "socket.h"
|
||||
#include "wizchip_conf.h"
|
||||
#include "modbusSlave.h"
|
||||
#include "gpio.h"
|
||||
|
||||
#if LOOPBACK_MODE == LOOPBACK_MAIN_NOBLCOK
|
||||
|
||||
|
||||
static rtumST myRtumST[20];
|
||||
uint8_t myRtumSTIdx = 0;
|
||||
uint8_t omrfins_buf[1024];
|
||||
|
||||
uint8_t readrx[14] = { 0xc0, 0x00, 0x02, 0x00, 0x0A, 0x00, 0x00, 0x01, 0x00, 0x00, 0x01, 0x01, 0x00, 0x40 };
|
||||
_Bool handleReadRecv(uint16_t size, uint16_t local_node) {
|
||||
readrx[4] = local_node;
|
||||
for (int i = 0; i < 14; i++) {
|
||||
if (readrx[i] != omrfins_buf[i]) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
uint16_t lastMyRtumSTIdx;
|
||||
if (myRtumSTIdx == 0) {
|
||||
lastMyRtumSTIdx = 19;
|
||||
} else {
|
||||
lastMyRtumSTIdx = myRtumSTIdx - 1;
|
||||
}
|
||||
|
||||
uint16_t thisSize = get_register(myRtumST[lastMyRtumSTIdx].size);
|
||||
|
||||
if (thisSize * 2 + 14 != size) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
for (int i = 0; i < thisSize; i++) {
|
||||
uint8_t byte_h = omrfins_buf[i * 2 + 14];
|
||||
uint8_t byte_l = omrfins_buf[i * 2 + 15];
|
||||
uint16_t value = ((uint16_t) byte_h << 8) | byte_l;
|
||||
set_register(1400 + (20 * lastMyRtumSTIdx) + i, value);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
uint8_t send_read[18] = { 0x80, 0x00, 0x02, 0x00, 0x01, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x01, 0x01, 0x82, 0x00, 0x64, 0x00, 0x00, 0x0a };
|
||||
uint8_t write_send[58] = { 0x80, 0x00, 0x02, 0x00, 0x01, 0x00, 0x00, 0x0b, 0x00, 0x00, 0x01, 0x02, 0x82, 0x00, 0x64, 0x00, 0x00, 0x02 };
|
||||
uint16_t handleSend(uint16_t local_node) {
|
||||
uint16_t sendsize = 0;
|
||||
uint16_t slave_id = get_register(myRtumST[myRtumSTIdx].slave_id);
|
||||
uint16_t plc_address = get_register(myRtumST[myRtumSTIdx].plc_address);
|
||||
uint16_t size = get_register(myRtumST[myRtumSTIdx].size);
|
||||
|
||||
if (slave_id == 0 || slave_id >= 127 || size > 20 || size == 0) {
|
||||
sendsize = 0;
|
||||
} else {
|
||||
if (myRtumSTIdx % 2 == 1) {
|
||||
send_read[7] = local_node;
|
||||
send_read[13] = (plc_address >> 8) & 0xFF;
|
||||
send_read[14] = plc_address & 0xFF;
|
||||
send_read[17] = size;
|
||||
sendsize = 18;
|
||||
for (int i = 0; i < sendsize; i++) {
|
||||
omrfins_buf[i] = send_read[i];
|
||||
}
|
||||
|
||||
} else {
|
||||
write_send[7] = local_node;
|
||||
write_send[13] = (plc_address >> 8) & 0xFF;
|
||||
write_send[14] = plc_address & 0xFF;
|
||||
write_send[17] = size;
|
||||
for (int i = 0; i < size; i++) {
|
||||
uint16_t value = get_register(1400 + (20 * myRtumSTIdx) + i);
|
||||
write_send[i * 2 + 18] = (value >> 8) & 0xFF;
|
||||
write_send[i * 2 + 19] = value & 0xFF;
|
||||
}
|
||||
sendsize = (size * 2) + 18;
|
||||
for (int i = 0; i < sendsize; i++) {
|
||||
omrfins_buf[i] = write_send[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
myRtumSTIdx = (myRtumSTIdx + 1) % 20;
|
||||
return sendsize;
|
||||
}
|
||||
|
||||
void updateMyRtumST(rtumST myRtumSTs[20]) {
|
||||
for (int i = 0; i < 20; i++) {
|
||||
myRtumST[i] = myRtumSTs[i];
|
||||
}
|
||||
}
|
||||
|
||||
int32_t loopback_udpc_omrfins(uint8_t sn, uint8_t *destip, uint16_t destport, uint16_t local_node, uint16_t *comm_cnt, _Bool *comm2) {
|
||||
int32_t ret;
|
||||
uint16_t size = 0, sentsize = 0;
|
||||
static uint16_t any_port = 50000;
|
||||
switch (getSn_SR(sn)) {
|
||||
case SOCK_UDP:
|
||||
if ((size = getSn_RX_RSR(sn)) > 0) {
|
||||
*comm2 = 1;
|
||||
*comm_cnt += 1 ;
|
||||
if (size > DATA_BUF_SIZE) {
|
||||
size = DATA_BUF_SIZE;
|
||||
}
|
||||
ret = recvfrom(sn, omrfins_buf, size, destip, (uint16_t*) &destport);
|
||||
|
||||
if (ret <= 0) {
|
||||
return ret;
|
||||
} else {
|
||||
handleReadRecv(ret, local_node);
|
||||
slmpTimeout = 490;
|
||||
}
|
||||
}
|
||||
|
||||
size = 0;
|
||||
if (slmpTimeout > 500) {
|
||||
size = handleSend(local_node);
|
||||
if (size == 0) {
|
||||
slmpTimeout = 1000;
|
||||
}
|
||||
}
|
||||
|
||||
sentsize = 0;
|
||||
while (sentsize != size) {
|
||||
ret = sendto(sn, omrfins_buf + sentsize, size - sentsize, destip, destport);
|
||||
if (ret < 0) {
|
||||
return ret;
|
||||
}
|
||||
sentsize += ret;
|
||||
}
|
||||
break;
|
||||
case SOCK_CLOSED:
|
||||
*comm2 = 0;
|
||||
if ((ret = socket(sn, Sn_MR_UDP, any_port, 0x00)) != sn) {
|
||||
return ret;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return 1;
|
||||
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
Reference in new issue
Block a user