你(们)好
我将 tcpecho 与 TIRTOS 搭配使用。
如何修改此示例以在需要时将数据传输到 PC?
在此示例中、只要从 PC 接收数据、我就可以向 PC 发送数据。
This thread has been locked.
If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.
你(们)好
我将 tcpecho 与 TIRTOS 搭配使用。
如何修改此示例以在需要时将数据传输到 PC?
在此示例中、只要从 PC 接收数据、我就可以向 PC 发送数据。
您好、Alireza、
您希望从 PC 接收"start"命令、因此仍需要打开套接字并接收数据包。 您可以发送所需的数据,而不是 tcpWorker()中回显接收到的数据包的 while 循环。
有关插槽工作流程的更多详细信息、请参阅 NWP 编程人员指南 SWRU455的第6.5节
此致、
Sarah
您好、Sarah、
是的,我可以在 tcpworker()中的 while 循环中发送所需的数据。
但我可以在接收数据时发送它。 我希望在收到"start"命令后连续发送数据。
空 tcpWorker (uint32_t arg0、uint32_t arg1)
{
int clientfd =(int) arg0;
int bytesRcvd;
int bytesSent;
char buffer[TCPPACKETSIZE];
Display_printf (display、0、0、"tcpWorker:start clientfd = 0x%x\n"、
客户机 d);
while ((bytesRcvd = recv (clientfd、buffer、TCPPACKETSIZE、0))> 0){
字节 Sent = SEND (clientfd、buffer_trans、1000、0);
if (bytesSent < 0 || bytesSent!= bytesRcvd){
display_printf (display、0、0、"发送失败。\n"\});
中断;
}
}
display_printf (display、0、0、"tcpWorker stop clientfd = 0x%x\n"、clientfd);
close (clientfd);
}
这是代码。
抱歉、这是我第一次使用 TI 的模块、我对这个示例没有任何想法。 您能 告诉 我如何修改此示例吗?
谢谢、此致、
Alireza
您好、Alireza、
tcpWorker()是在 tcpHandler()中创建的线程。 tcpHandler()由 IP 获取的事件调用(在 SimpleLinkNetAppEventHandler 中),这意味着设备已连接到网络。
这里的示例可让您了解如何使用套接字 API。 您不需要按原样使用 tcpWorker()或 tcpHandler()函数。 如果要接收一条命令然后发送数据、请设置套接字、等待命令数据包、然后创建 while 循环以发送数据。
默认情况下、套接字处于阻塞状态。 对于非阻塞式套接字、请参阅 NWP 编程人员指南的第6.7节"工作模式"。
此致、
Sarah
您好、Alireza、
如果您使用的是非阻塞套接字、这意味着您必须轮询相关 API 以检查传入数据。 在 while 循环内,您可以在发送任何传感器数据之前调用 recv()并检查 stop 命令。 如果没有要接收的内容、您将看到从 recv ()返回的错误代码 SL_ERROR_BSD_EAGAIN (-11)。 如果有 STOP 命令、您可以从 while 循环中分出。
此致、
Sarah
您好、Sarah、
对于非阻塞式插座、我看到 了 NWP 编程人员指南的第6.7节"工作模式"。
为了将套接字设置为非阻塞、提供了一些设置:
将套接字设置为非阻塞的示例:
_i16状态;SlSockNonBlocking_t BlockingOption;
BlockingOption.NonBlockingEnabled = 1;
//启用或禁用非阻塞模式状态= sl_SetSockOpt (SD、sl_SOL_Socket、sl_SO_NONBLOCKING、(_u8*)&BlockingOption、sizeof (BlockingOption));
if (Status){// error}
非阻塞 TCP 连接的示例:
_i16状态;SlSockAddrIn_t Addr;
Addr.Sin_Family = SL_AF_iNet;
Addr.Sin_port = sl_Htons (5001);
addr.sin addr.s_addr = sl_Htonl (sl_IPV4_VAL (192、168、1、31));
状态= SL_ERROR_BSD_EALREADY;
while (0 > Status){Status = sl_Connect (SD、(SlSockAddr_t *)&Addr、sizeof (SlSockAddr_t));
if (0 > Status){if (sl_error_bSD_EALREADY!= Status){// errorbreak;}}}
在哪里以及如何在代码中设置这些设置?
感谢您的回答。
此致、
Alireza
您好、Sarah、
感谢您的回复、它很有帮助。
我按照 NWP 编程人员指南第6.7节"工作模式"中所述更改了代码、这是新代码。
void tcpHandler(uint32_t arg0, uint32_t arg1)
{
void *thread = NULL;
int status;
int clientfd;
int server;
struct sockaddr_in localAddr;
struct sockaddr_in clientAddr;
int optval;
int optlen = sizeof(optval);
socklen_t addrlen = sizeof(clientAddr);
memset(&localAddr, 0, sizeof(localAddr));
localAddr.sin_family = AF_INET;
localAddr.sin_addr.s_addr = htonl(INADDR_ANY);
localAddr.sin_port = htons(arg0);
Display_printf(display, 0, 0, "TCP Echo example started\n");
server = socket(AF_INET, SOCK_STREAM, 0);
if (server == -1) {
Display_printf(display, 0, 0, "tcpHandler: socket failed\n");
goto shutdown;
}
optval = 1;
status = setsockopt(server, SOL_SOCKET, SO_NONBLOCKING, &optval, optlen);
if (status == -1) {
Display_printf(display, 0, 0, "tcpHandler: setsockopt failed\n");
goto shutdown;
}
status=-1;
status = bind(server, (struct sockaddr *)&localAddr, sizeof(localAddr));
if (status == -1) {
Display_printf(display, 0, 0, "tcpHandler: bind failed\n");
goto shutdown;
}
status = listen(server, NUMTCPWORKERS);
if (status == -1) {
Display_printf(display, 0, 0, "tcpHandler: listen failed\n");
goto shutdown;
}
status=-1;
while(status<0){
status=connect(server,(struct sockaddr *)&localAddr,sizeof(localAddr));
if(status!=-1){
break;
}
}
while ((clientfd =
accept(server, (struct sockaddr *)&clientAddr, &addrlen)) != -1) {
Display_printf(display, 0, 0,
"tcpHandler: Creating thread clientfd = %x\n", clientfd);
thread = TaskCreate(tcpWorker, NULL, 3, 2048, (uintptr_t)clientfd,
0, 0);
if (!thread) {
Display_printf(display, 0, 0,
"tcpHandler: Error - Failed to create new thread.\n");
close(clientfd);
}
/* addrlen is a value-result param, must reset for next accept call */
addrlen = sizeof(clientAddr);
}
Display_printf(display, 0, 0, "tcpHandler: accept failed.\n");
shutdown:
if (server != -1) {
close(server);
}
}
但现在我遇到了一个问题、当调用"connect"函数时、它始终返回-1。
是否需要进一步更改配置?
此致、
Alireza
您好、Jan、
我阅读了该教程、然后重新编写了代码。
以下是新代码:
void tcpHandler(uint32_t arg0, uint32_t arg1)
{
void *thread = NULL;
int status;
int clientfd;
int server;
struct sockaddr_in localAddr;
struct sockaddr_in clientAddr;
int optval;
int optlen = sizeof(optval);
socklen_t addrlen = sizeof(clientAddr);
Display_printf(display, 0, 0, "TCP Echo example started\n");
server = socket(AF_INET, SOCK_STREAM, 0);
if (server == -1) {
Display_printf(display, 0, 0, "tcpHandler: socket failed\n");
goto shutdown;
}
memset(&localAddr, 0, sizeof(localAddr));
localAddr.sin_family = AF_INET;
localAddr.sin_addr.s_addr = htonl(INADDR_ANY);
localAddr.sin_port = htons(arg0);
status = bind(server, (struct sockaddr *)&localAddr, sizeof(localAddr));
if (status == -1) {
Display_printf(display, 0, 0, "tcpHandler: bind failed\n");
goto shutdown;
}
status = listen(server, NUMTCPWORKERS);
if (status == -1) {
Display_printf(display, 0, 0, "tcpHandler: listen failed\n");
goto shutdown;
}
optval = 1;
status = setsockopt(server, SOL_SOCKET, SO_NONBLOCKING, &optval, optlen);
if (status == -1) {
Display_printf(display, 0, 0, "tcpHandler: setsockopt failed\n");
goto shutdown;
}
clientfd=-1;
while (clientfd==-1) {
clientfd = accept(server, (struct sockaddr *)&clientAddr, &addrlen);
Display_printf(display, 0, 0,
"tcpHandler: Creating thread clientfd = %x\n", clientfd);
thread = TaskCreate(tcpWorker, NULL, 3, 2048, (uintptr_t)clientfd,
0, 0);
if (!thread) {
Display_printf(display, 0, 0,
"tcpHandler: Error - Failed to create new thread.\n");
close(clientfd);
}
/* addrlen is a value-result param, must reset for next accept call */
addrlen = sizeof(clientAddr);
}
Display_printf(display, 0, 0, "tcpHandler: accept failed.\n");
shutdown:
if (server != -1) {
close(server);
}
}
我希望持续发送一些数据、直到收到"stop"命令。 因此,我在 while 循环中调用了 send()函数以连续发送数据,并调用了一个 recv()函数以检查是否接收到"stop"命令。
代码如下:
void tcpWorker(uint32_t arg0, uint32_t arg1)
{
int clientfd = (int)arg0;
int bytesRcvd;
int bytesSent;
char buffer[TCPPACKETSIZE];
Display_printf(display, 0, 0, "tcpWorker: start clientfd = 0x%x\n",
clientfd);
while ((bytesRcvd = recv(clientfd, buffer, TCPPACKETSIZE, 0)) > 0) {
if (buffer[0]=='a'){
bytesSent = send(clientfd, buffer, bytesRcvd, 0);
}
while (buffer[0]=='s'){
bytesSent = send(clientfd, send_buffer, bytesRcvd, 0);
bytesRcvd = recv(clientfd, buffer, TCPPACKETSIZE, 0);
}
}
Display_printf(display, 0, 0, "tcpWorker stop clientfd = 0x%x\n", clientfd);
close(clientfd);
}
但我有一个问题,我认为 recv()函数仍在阻塞模式下运行,因为我在收到数据之前无法发送数据。 是否需要进一步更改配置?
谢谢、此致、
Alireza