增加以太网传入RTCM数据

This commit is contained in:
2025-11-22 21:45:02 +08:00
parent 2f580d0d3a
commit ef5126e4f4
6 changed files with 252 additions and 128 deletions

View File

@@ -28,7 +28,10 @@ u16 Desport = 7900;
u8 pc_ipAddr[4] = { 192, 168, 17, 2 }; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>IP address
u8 tcp_socket[WCHNET_NUM_TCP]; //Save the currently connected socket
u8 remote_base_ip[4] = { 192, 168, 10, 8 }; //Զ<>̻<EFBFBD>վIP address
u16 remote_port = 1008; //Զ<>̻<EFBFBD>վ<EFBFBD>˿<EFBFBD>
u8 tcp_socket[WCHNET_NUM_TCP]; //Save the currently connected socket
SystemDataRecord g_systemDataRecord = {0,0,0,0};
@@ -50,6 +53,8 @@ uint8_t portableOcuOnline = 0;
uint8_t remoteOcuOnline = 0;
Timer ethernet_timer_interface;
Timer ethernet_timer_interface1;
/*********************************************************************
* @fn mStopIfError
*
@@ -277,6 +282,33 @@ void WCHNET_UdpServerRecv2(struct _SCOK_INF *socinf, u32 ipaddr, u16 port, u8 *b
}
}
/*********************************************************************
* @fn WCHNET_CreateTcpSocket
*
* @brief Create TCP Socket
*
* @return none
*/
void WCHNET_CreateTcpSocket(void)
{
u8 i;
SOCK_INF TmpSocketInf;
memset((void *) &TmpSocketInf, 0, sizeof(SOCK_INF));
memcpy((void *) TmpSocketInf.IPAddr, remote_base_ip, 4);
TmpSocketInf.DesPort = remote_port;
TmpSocketInf.SourPort = 1008;
TmpSocketInf.ProtoType = PROTO_TYPE_TCP;
TmpSocketInf.RecvBufLen = RECE_BUF_LEN;
i = WCHNET_SocketCreat(&SocketId3, &TmpSocketInf);
printf("SocketId %d\r\n", SocketId3);
mStopIfError(i);
i = WCHNET_SocketConnect(SocketId3); //make a TCP connection
mStopIfError(i);
}
/*********************************************************************
* @fn WCHNET_CreateUdpSocket
*
@@ -286,7 +318,7 @@ void WCHNET_UdpServerRecv2(struct _SCOK_INF *socinf, u32 ipaddr, u16 port, u8 *b
*/
void WCHNET_CreateUdpSocket(void)
{
u8 i;
// u8 i;
SOCK_INF TmpSocketInf;
// memset((void *) &TmpSocketInf, 0, sizeof(SOCK_INF));
@@ -310,14 +342,15 @@ void WCHNET_CreateUdpSocket(void)
#endif
memset(tcp_socket, 0xff, WCHNET_NUM_TCP);
memset((void *) &TmpSocketInf, 0, sizeof(SOCK_INF));
TmpSocketInf.SourPort = srcport;
TmpSocketInf.ProtoType = PROTO_TYPE_TCP;
i = WCHNET_SocketCreat(&SocketId, &TmpSocketInf);
printf("SocketIdForListen %d\r\n", SocketId);
mStopIfError(i);
i = WCHNET_SocketListen(SocketId); //listen for connections
mStopIfError(i);
WCHNET_CreateTcpSocket();
// memset((void *) &TmpSocketInf, 0, sizeof(SOCK_INF));
// TmpSocketInf.SourPort = srcport;
// TmpSocketInf.ProtoType = PROTO_TYPE_TCP;
// i = WCHNET_SocketCreat(&SocketId, &TmpSocketInf);
// printf("SocketIdForListen %d\r\n", SocketId);
// mStopIfError(i);
// i = WCHNET_SocketListen(SocketId); //listen for connections
// mStopIfError(i);
//
memset((void *)&TmpSocketInf, 0, sizeof(SOCK_INF));
@@ -336,21 +369,77 @@ void WCHNET_CreateUdpSocket(void)
TmpSocketInf.RecvStartPoint = (u32) SocketRecvBuf2;
TmpSocketInf.RecvBufLen = RECE_BUF_LEN;
TmpSocketInf.AppCallBack = WCHNET_UdpServerRecv2;
i = WCHNET_SocketCreat(&SocketId3, &TmpSocketInf);
WCHNET_SocketCreat(&SocketId3, &TmpSocketInf);
printf("WCHNET_SocketCreat %d\r\n", SocketId3);
// mStopIfError(i);
// memset((void *) &TmpSocketInf, 0, sizeof(SOCK_INF));
// memcpy((void *) TmpSocketInf.IPAddr, DESIP, 4);
// TmpSocketInf.DesPort = desport;
// TmpSocketInf.SourPort = srcport++;
// TmpSocketInf.ProtoType = PROTO_TYPE_TCP;
// TmpSocketInf.RecvBufLen = RECE_BUF_LEN;
// i = WCHNET_SocketCreat(&SocketId4, &TmpSocketInf);
// printf("SocketId4 %d\r\n", SocketId4);
// mStopIfError(i);
// i = WCHNET_SocketConnect(SocketId4); //make a TCP connection
// mStopIfError(i);
}
/*********************************************************************
* @fn WCHNET_DataLoopback
*
* @brief Data loopback function.
*
* @param id - socket id.
*
* @return none
*/
void WCHNET_DataLoopback(u8 id)
{
#if 1
u8 i;
u32 len;
u32 endAddr = SocketInf[id].RecvStartPoint + SocketInf[id].RecvBufLen; //Receive buffer end address
if ((SocketInf[id].RecvReadPoint + SocketInf[id].RecvRemLen) > endAddr)
{ //Calculate the length of the received data
len = endAddr - SocketInf[id].RecvReadPoint;
}
else
{
len = SocketInf[id].RecvRemLen;
}
if(remote_port == SocketInf[id].DesPort)//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݣ<EFBFBD>Դ<EFBFBD>˿<EFBFBD><CBBF><EFBFBD>ȷ<EFBFBD><C8B7><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA>̫<EFBFBD><CCAB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>RTCM<43><4D><EFBFBD><EFBFBD>
{
memcpy(&RTCM_Buffer1.dma_buffer[RTCM_Buffer1.active_buf][0],(u8 *) SocketInf[id].RecvReadPoint,len);
RTCM_Buffer1.type = ethernet;
RTCM_Buffer1.buf_len = (uint16_t)len;
RTCM_Buffer1.active_buf ^= 1;
RTCM_Buffer1.eth_cnt ++;//<2F>ۼ<EFBFBD><DBBC>ж<EFBFBD>
publishMessage(&RTCM_Buffer1, 1);
}
// printf("Դ<>˿<EFBFBD>: %u, Ŀ<><C4BF><EFBFBD>˿<EFBFBD>: %u\n",
// SocketInf[id].SourPort, SocketInf[id].DesPort);
// i = WCHNET_SocketSend(id, (u8 *) SocketInf[id].RecvReadPoint, &len); //send data
if (i == WCHNET_ERR_SUCCESS) {
WCHNET_SocketRecv(id, NULL, &len); //Clear sent data
}
#else
u32 len, totallen;
u8 *p = MyBuf, TransCnt = 255;
len = WCHNET_SocketRecvLen(id, NULL); //query length
printf("Receive Len = %d\r\n", len);
totallen = len;
WCHNET_SocketRecv(id, MyBuf, &len); //Read the data of the receive buffer into MyBuf
while(1){
len = totallen;
WCHNET_SocketSend(id, p, &len); //Send the data
totallen -= len; //Subtract the sent length from the total length
p += len; //offset buffer pointer
if( !--TransCnt ) break; //Timeout exit
if(totallen) continue; //If the data is not sent, continue to send
break; //After sending, exit
}
#endif
}
/*********************************************************************
@@ -368,6 +457,7 @@ void WCHNET_HandleSockInt(u8 socketid, u8 intstat)
u8 i;
if (intstat & SINT_STAT_RECV) //receive data
{
WCHNET_DataLoopback(socketid);
}
if (intstat & SINT_STAT_CONNECT) //connect successfully
{
@@ -403,6 +493,7 @@ void WCHNET_HandleSockInt(u8 socketid, u8 intstat)
}
}
printf("TCP Timeout\r\n");
WCHNET_CreateTcpSocket();
}
}
@@ -498,7 +589,7 @@ uint8_t CalculateChecksum(const uint8_t* data, size_t len)
static void ethernetInterfaceTimerProcess(void *signal_id)
{
static u_int32_t navigation_output_len = 0;
static u_int32_t navigation_output_len1 = 0;
// static u_int32_t navigation_output_len1 = 0;
//----------------------------------------------------------------------
if (signal_id == &navigation_output1)
{
@@ -536,21 +627,21 @@ static void ethernetInterfaceTimerProcess(void *signal_id)
navigation_outputC1.bit_data.crc1 = CalculateChecksum(&navigation_outputC1.arr[0],sizeof(UnInfNavigationOutputC)-1);
for(uint8_t i = 0; i < WCHNET_NUM_TCP; i++)
{
if(tcp_socket[i] != 0xFF)
{
if(0 == navigation_output_len1)
{
navigation_output_len1 = sizeof(navigation_outputC1);
}
// for(uint8_t i = 0; i < WCHNET_NUM_TCP; i++)
// {
// if(tcp_socket[i] != 0xFF)
// {
// if(0 == navigation_output_len1)
// {
// navigation_output_len1 = sizeof(navigation_outputC1);
// }
i = WCHNET_SocketSend(tcp_socket[i], (uint8_t *)&navigation_outputC1, &navigation_output_len1); //send data
if (i == WCHNET_ERR_SUCCESS) {
WCHNET_SocketRecv(tcp_socket[i], NULL, &navigation_output_len1); //Clear sent data
}
}
}
// i = WCHNET_SocketSend(tcp_socket[i], (uint8_t *)&navigation_outputC1, &navigation_output_len1); //send data
// if (i == WCHNET_ERR_SUCCESS) {
// WCHNET_SocketRecv(tcp_socket[i], NULL, &navigation_output_len1); //Clear sent data
// }
// }
// }
}
else if (signal_id == &navigation_output2)
{
@@ -593,7 +684,7 @@ static void ethernetInterfaceTimerProcess(void *signal_id)
}
UdpSendToData(SocketId2, (uint8_t *)&navigation_output2, &navigation_output_len, &str_RequestMessage.ip_adesser[0],str_RequestMessage.port);//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
}
else
else
{
}
@@ -614,6 +705,22 @@ void ethInterruptProcess(void *signal_id)
timerStart(&ethernet_timer_interface, 1,1); //1ms<6D><73><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>
}
void ethernetFaultProcess(void *signal_id)//<2F><>̫<EFBFBD><CCAB>RTCM<43><4D><EFBFBD><EFBFBD><EFBFBD>жϡ<D0B6>
{
static uint8_t eth_cnt_last = 0;
if( (RTCM_Buffer1.eth_cnt == eth_cnt_last) && (ethernet == RTCM_Buffer1.type) )//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>5s<35><73><EFBFBD>ǶϿ<C7B6><CFBF><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ˡ<EFBFBD>
{
RTCM_Buffer1.type = init;
}
else
{
eth_cnt_last = RTCM_Buffer1.eth_cnt;
}
timerStart(&ethernet_timer_interface1, 8000,1); //8s<38><73><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>
}
void ethernetInterfaceInit(void)
{
@@ -622,7 +729,14 @@ void ethernetInterfaceInit(void)
subscribe(&navigation_output1, ethernetInterfaceTimerProcess);
subscribe(&navigation_output2, ethernetInterfaceTimerProcess);
subscribe(&ethernet_timer_interface, ethInterruptProcess);
subscribe(&ethernet_timer_interface1, ethernetFaultProcess); //1ms<6D><73><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>
timerStart(&ethernet_timer_interface, 1,1); //1ms<6D><73><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>
timerStart(&ethernet_timer_interface1, 5000,1); //5s<35><73><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>
printf( "ethernetInterface: initial OK %d\n",getCurrentTime());
}