MyTetra Share
Делитесь знаниями!
net.c
Время создания: 24.11.2018 13:56
Раздел: Electronics - Microcontrollers - STM32 - Hunter - v3.1

#include "net.h"


extern UART_HandleTypeDef huart6;

extern struct ClientStruct *cs;

extern struct tcp_pcb *client_pcb;

extern uint8_t serverHasConnection;

extern uint8_t clientHasConnection;


USART_prop_ptr usartProp;


//char msgWrongCommand[] = "Wrong Command.";

//char cmdStr[30];


uint8_t uartByte;

char dataPacket[64];


//uint8_t ipAddrDest[4];

//uint16_t portDest;


void initNet(void)

{

usartProp.usartBuf[0] = 0;

usartProp.usartBufCnt = 0;

usartProp.isTcpConnect = TCP_NOPE;

usartProp.isText = 0;

}


void netCommandParse(char *bufStr)

{

bufStr[usartProp.usartBufCnt] = '\0';

//HAL_UART_Transmit(&huart6, (uint8_t*)bufStr, usartProp.usartBufCnt, 0x1000);

if(strncmp(bufStr, "cnct", 4) == 0)

{

uint8_t ipAddrDest[4] = {10, 124, 4, 25};

uint16_t portDest = 1234;

// HAL_UART_Transmit(&huart6, (uint8_t*)"Trying to connect...\n\r", 22, 0x0F);

tcpClientConnect(ipAddrDest, portDest);

}

else if(strncmp(bufStr, "ccc ", 4) == 0)

{

uint8_t ipAddrDest[4] = {10, 124, 4, 25};

uint16_t portDest = 3333;

// HAL_UART_Transmit(&huart6, (uint8_t*)"Trying to connect...\n\r", 22, 0x0F);

tcpClientConnect(ipAddrDest, portDest);

}

else if(strncmp(bufStr, "disc", 4) == 0)

{

tcpClientCloseConnection(client_pcb, cs);

}

else //if we want to send any data

{

if(clientHasConnection)

tcpClientSendString(bufStr);

if(serverHasConnection)

tcpServerSendString(bufStr);

usartProp.isText = 0;

}

}


//USART function for RX interrupt

void USART6_RxComplete(void)

{

HAL_UART_Transmit(&huart6, &uartByte, 1, 0x1000);

if(usartProp.usartBufCnt > 30 || uartByte == 0x0D) //enter

{

HAL_UART_Transmit(&huart6, (uint8_t*)"\n", 1, 0x0F);

usartProp.usartBuf[usartProp.usartBufCnt] = '\n';

usartProp.usartBuf[++usartProp.usartBufCnt] = '\0';

netCommandParse((char*)usartProp.usartBuf);

usartProp.usartBufCnt = 0;

}

else if(uartByte == 0x08) //backspace

{

if(usartProp.usartBufCnt)

--usartProp.usartBufCnt;

}

else

{

usartProp.usartBuf[usartProp.usartBufCnt] = uartByte;

++usartProp.usartBufCnt;

}

usartProp.usartBuf[usartProp.usartBufCnt] = '\0';

HAL_UART_Receive_IT(&huart6, &uartByte, 1);

}


Так же в этом разделе:
 
MyTetra Share v.0.53
Яндекс индекс цитирования