微芯片,通用 tcp 服务器
microchip, Generic tcp server
我是 Microchip 产品线的新手,我目前正在使用 Pic32 以太网入门套件 2。我一直在试用和测试该产品的所有方面,目前我正在使用通用 TCPIP 伺服来自 Harmony 的演示。我可以在芯片上加载应用程序并使用 Telnet 登录。如果我输入 say "h",我也可以看到它正常工作,它会按原样回显 "hH"。因为在此演示中,它在其上运行了一条 ToUpper 线。我想要尝试的是在 telnet 中键入 "hello" 然后让它发回 "World" 只是一些简单的事情让我开始使用它。下面是读取传入数据并将其转换为 Upper 并发回的当前代码部分。
case APP_TCPIP_SERVING_CONNECTION:
{
if (!TCPIP_TCP_IsConnected(appData.socket))
{
appData.state = APP_TCPIP_CLOSING_CONNECTION;
SYS_CONSOLE_MESSAGE("Connection was closed\r\n");
break;
}
int16_t wMaxGet, wMaxPut, wCurrentChunk;
uint16_t w, w2;
uint8_t AppBuffer[32];
//uint8_t AppBuffer2[] = "This is a Test";
// Figure out how many bytes have been received and how many we can transmit.
wMaxGet = TCPIP_TCP_GetIsReady(appData.socket); // Get TCP RX FIFO byte count
wMaxPut = TCPIP_TCP_PutIsReady(appData.socket); // Get TCP TX FIFO free space
// Make sure we don't take more bytes out of the RX FIFO than we can put into the TX FIFO
if(wMaxPut < wMaxGet)
wMaxGet = wMaxPut;
// Process all bytes that we can
// This is implemented as a loop, processing up to sizeof(AppBuffer) bytes at a time.
// This limits memory usage while maximizing performance. Single byte Gets and Puts are a lot slower than multibyte GetArrays and PutArrays.
wCurrentChunk = sizeof(AppBuffer);
for(w = 0; w < wMaxGet; w += sizeof(AppBuffer))
{
// Make sure the last chunk, which will likely be smaller than sizeof(AppBuffer), is treated correctly.
if(w + sizeof(AppBuffer) > wMaxGet)
wCurrentChunk = wMaxGet - w;
// Transfer the data out of the TCP RX FIFO and into our local processing buffer.
TCPIP_TCP_ArrayGet(appData.socket, AppBuffer, wCurrentChunk);
// Perform the "ToUpper" operation on each data byte
for(w2 = 0; w2 < wCurrentChunk; w2++)
{
i = AppBuffer[w2];
if(i >= 'a' && i <= 'z')
{
i -= ('a' - 'A');
AppBuffer[w2] = i;
}
else if(i == '\e') //escape
{
appData.state = APP_TCPIP_CLOSING_CONNECTION;
SYS_CONSOLE_MESSAGE("Connection was closed\r\n");
}
}
// Transfer the data out of our local processing buffer and into the TCP TX FIFO.
SYS_CONSOLE_PRINT("Server Sending %s\r\n", AppBuffer);
TCPIP_TCP_ArrayPut(appData.socket, AppBuffer, wCurrentChunk);
// No need to perform any flush. TCP data in TX FIFO will automatically transmit itself after it accumulates for a while. If you want to decrease latency (at the expense of wasting network bandwidth on TCP overhead), perform and explicit flush via the TCPFlush() API.
}
}
break;
提前致谢。
PBSnake
case APP_TCPIP_SERVING_CONNECTION:
{
static uint8_t message[] = "Hello";
static uint16_t pos = 0;
if (!TCPIP_TCP_IsConnected(appData.socket))
{
pos = 0;
appData.state = APP_TCPIP_CLOSING_CONNECTION;
SYS_CONSOLE_MESSAGE("Connection was closed\r\n");
break;
}
int16_t wMaxGet, wMaxPut, wCurrentChunk;
uint16_t w, w2;
uint8_t AppBuffer[32];
//uint8_t AppBuffer2[] = "This is a Test";
// Figure out how many bytes have been received and how many we can transmit.
wMaxGet = TCPIP_TCP_GetIsReady(appData.socket); // Get TCP RX FIFO byte count
wMaxPut = TCPIP_TCP_PutIsReady(appData.socket); // Get TCP TX FIFO free space
// Make sure we don't take more bytes out of the RX FIFO than we can put into the TX FIFO
if(wMaxPut < wMaxGet)
wMaxGet = wMaxPut;
// Process all bytes that we can
// This is implemented as a loop, processing up to sizeof(AppBuffer) bytes at a time.
// This limits memory usage while maximizing performance. Single byte Gets and Puts are a lot slower than multibyte GetArrays and PutArrays.
wCurrentChunk = sizeof(AppBuffer);
for(w2 = 0; w2 < wCurrentChunk; w2++)
{
i = AppBuffer[w2];
if (i == message[pos])
{
pos++;
if (pos == strlen(message))
{
pos = 0;
strcpy(AppBuffer, "World");
SYS_CONSOLE_PRINT("Server Sending %s\r\n", AppBuffer);
TCPPutArray(MySocket, AppBuffer, strlen(AppBuffer));
}
}
else
{
pos = 0;
}
if(i == '\e') //escape
{
TCPServerState = SM_CLOSING;
}
}
}
break;
我是 Microchip 产品线的新手,我目前正在使用 Pic32 以太网入门套件 2。我一直在试用和测试该产品的所有方面,目前我正在使用通用 TCPIP 伺服来自 Harmony 的演示。我可以在芯片上加载应用程序并使用 Telnet 登录。如果我输入 say "h",我也可以看到它正常工作,它会按原样回显 "hH"。因为在此演示中,它在其上运行了一条 ToUpper 线。我想要尝试的是在 telnet 中键入 "hello" 然后让它发回 "World" 只是一些简单的事情让我开始使用它。下面是读取传入数据并将其转换为 Upper 并发回的当前代码部分。
case APP_TCPIP_SERVING_CONNECTION:
{
if (!TCPIP_TCP_IsConnected(appData.socket))
{
appData.state = APP_TCPIP_CLOSING_CONNECTION;
SYS_CONSOLE_MESSAGE("Connection was closed\r\n");
break;
}
int16_t wMaxGet, wMaxPut, wCurrentChunk;
uint16_t w, w2;
uint8_t AppBuffer[32];
//uint8_t AppBuffer2[] = "This is a Test";
// Figure out how many bytes have been received and how many we can transmit.
wMaxGet = TCPIP_TCP_GetIsReady(appData.socket); // Get TCP RX FIFO byte count
wMaxPut = TCPIP_TCP_PutIsReady(appData.socket); // Get TCP TX FIFO free space
// Make sure we don't take more bytes out of the RX FIFO than we can put into the TX FIFO
if(wMaxPut < wMaxGet)
wMaxGet = wMaxPut;
// Process all bytes that we can
// This is implemented as a loop, processing up to sizeof(AppBuffer) bytes at a time.
// This limits memory usage while maximizing performance. Single byte Gets and Puts are a lot slower than multibyte GetArrays and PutArrays.
wCurrentChunk = sizeof(AppBuffer);
for(w = 0; w < wMaxGet; w += sizeof(AppBuffer))
{
// Make sure the last chunk, which will likely be smaller than sizeof(AppBuffer), is treated correctly.
if(w + sizeof(AppBuffer) > wMaxGet)
wCurrentChunk = wMaxGet - w;
// Transfer the data out of the TCP RX FIFO and into our local processing buffer.
TCPIP_TCP_ArrayGet(appData.socket, AppBuffer, wCurrentChunk);
// Perform the "ToUpper" operation on each data byte
for(w2 = 0; w2 < wCurrentChunk; w2++)
{
i = AppBuffer[w2];
if(i >= 'a' && i <= 'z')
{
i -= ('a' - 'A');
AppBuffer[w2] = i;
}
else if(i == '\e') //escape
{
appData.state = APP_TCPIP_CLOSING_CONNECTION;
SYS_CONSOLE_MESSAGE("Connection was closed\r\n");
}
}
// Transfer the data out of our local processing buffer and into the TCP TX FIFO.
SYS_CONSOLE_PRINT("Server Sending %s\r\n", AppBuffer);
TCPIP_TCP_ArrayPut(appData.socket, AppBuffer, wCurrentChunk);
// No need to perform any flush. TCP data in TX FIFO will automatically transmit itself after it accumulates for a while. If you want to decrease latency (at the expense of wasting network bandwidth on TCP overhead), perform and explicit flush via the TCPFlush() API.
}
}
break;
提前致谢。 PBSnake
case APP_TCPIP_SERVING_CONNECTION:
{
static uint8_t message[] = "Hello";
static uint16_t pos = 0;
if (!TCPIP_TCP_IsConnected(appData.socket))
{
pos = 0;
appData.state = APP_TCPIP_CLOSING_CONNECTION;
SYS_CONSOLE_MESSAGE("Connection was closed\r\n");
break;
}
int16_t wMaxGet, wMaxPut, wCurrentChunk;
uint16_t w, w2;
uint8_t AppBuffer[32];
//uint8_t AppBuffer2[] = "This is a Test";
// Figure out how many bytes have been received and how many we can transmit.
wMaxGet = TCPIP_TCP_GetIsReady(appData.socket); // Get TCP RX FIFO byte count
wMaxPut = TCPIP_TCP_PutIsReady(appData.socket); // Get TCP TX FIFO free space
// Make sure we don't take more bytes out of the RX FIFO than we can put into the TX FIFO
if(wMaxPut < wMaxGet)
wMaxGet = wMaxPut;
// Process all bytes that we can
// This is implemented as a loop, processing up to sizeof(AppBuffer) bytes at a time.
// This limits memory usage while maximizing performance. Single byte Gets and Puts are a lot slower than multibyte GetArrays and PutArrays.
wCurrentChunk = sizeof(AppBuffer);
for(w2 = 0; w2 < wCurrentChunk; w2++)
{
i = AppBuffer[w2];
if (i == message[pos])
{
pos++;
if (pos == strlen(message))
{
pos = 0;
strcpy(AppBuffer, "World");
SYS_CONSOLE_PRINT("Server Sending %s\r\n", AppBuffer);
TCPPutArray(MySocket, AppBuffer, strlen(AppBuffer));
}
}
else
{
pos = 0;
}
if(i == '\e') //escape
{
TCPServerState = SM_CLOSING;
}
}
}
break;