请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。
器件型号:LAUNCHCC3235MOD 主题中讨论的其他器件: CC3235SF
首先、我认为这样的代码可能会让我受益匪浅。 我当时正在 tcpecho_CC3235SF_LAUNCHXL_tirtos7_ticlang 中查看 tcpWorker 、我希望以此为起点来传输长度恒定的消息、就像个字符一样。 我希望在回复之前先解释和回复消息。 但到目前为止,我不能得到一个兜售者的消息的答复。 我正在使用.net 框架网流(可以读取、可以写入)向 LAUNCHCC3235MOD 发送消息、并调整 tcpWorker。
。
void tcpWorker1(uint32_t arg0, uint32_t arg1)
{
int idbg=0;
int iclientfd= (int)arg0;
int ibytesRcvd=0;
int ibytesMSG=0;
int ibytesSent=0;
char cbuf[TCPPACKETSIZE];
int bdone=-1;
//Display_printf(display, 0, 0, "tcpWorker: start clientfd = 0x%x\n", clientfd);
while (bdone!=1){
while ((ibytesRcvd < recv(iclientfd, cbuf +ibytesMSG, MSGSZ, 0)) > 0) {
ibytesMSG+=ibytesRcvd;
idbg=ibytesMSG;
if(ibytesMSG >= MSGSZ){
ibytesMSG=0;
break;
}
}
// process buffer compose response
if (cbuf[4]==7) for (int i=MSGDG;i<MSGSZ;i++) cbuf[i]=(char)toupper(cbuf[i]);
if (cbuf[0]==0x80) bdone=1;
ibytesSent=0;int ibytesx=0;
while (ibytesSent<(MSGSZ)){
ibytesx=send(iclientfd, cbuf, MSGSZ, 0);
if (ibytesx<0){
ibytesx=0;
}
ibytesx=send(iclientfd, cbuf, MSGSZ, 0);
ibytesSent +=ibytesx;
}
}
close(iclientfd);
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.IO;
using System.Net;
using System.Runtime.CompilerServices;
namespace csMSG
{
internal class Program
{
const int TCPPACKETSIZE = 0x500;
const int MSGSZ = 0x4e0;
const int MSGDG = 0x20;
const int MSGDD = 0x4c0;
static void Main(string[] args)
{
String server = "trog3235"; // Server name or IP address
byte[] aBMSGRcvd = new byte[MSGSZ];
byte[] aBMSG = new byte[MSGSZ];
for (int i = 0; i < MSGSZ; i++) aBMSG[i] = 0;
int servPort = 58800;
TcpClient client = null;
NetworkStream netStream = null;
StreamReader sr = new StreamReader("MSG1.txt");
String smsg1 = sr.ReadToEnd();
byte[] aBMSGDD = Encoding.ASCII.GetBytes(smsg1);
Array.Resize(ref aBMSGDD, MSGDD);
byte[] aBMSGDG = new byte[MSGDG];
for (int i = 0; i < MSGDG; i++) aBMSGDG[i] = 0;
aBMSGDG[0] = 0x80; aBMSGDG[4] = 7;
aBMSG[0] = aBMSGDG[0]; aBMSG[4] = aBMSGDG[4];
for (int i = 0; i < MSGDD; i++) aBMSG[MSGDG + i] = aBMSGDD[i];
try
{
// Create socket that is connected to server on specified port
client = new TcpClient(server, servPort);
Console.WriteLine("Connected to server... sending message");
netStream = client.GetStream();
// Send the message to server
netStream.Write(aBMSG, 0, aBMSG.Length);
Console.WriteLine("Sent {0} bytes to server...", aBMSG.Length);
//
int totalBytesRcvd = 0; // Total bytes received so far
int bytesRcvd = 0; // Bytes received in last read
// Receive the same string back from the server
int ix = 0;
while (totalBytesRcvd < aBMSGRcvd.Length)
{
bytesRcvd = netStream.Read(aBMSGRcvd, totalBytesRcvd, aBMSGRcvd.Length - totalBytesRcvd);
if (bytesRcvd < 0) bytesRcvd = 0;
Thread.Sleep(40);
totalBytesRcvd += bytesRcvd;
ix++;
if (ix > 20) break;
}
Console.WriteLine("Received {0} bytes from server: {1}", totalBytesRcvd,
Encoding.ASCII.GetString(aBMSGRcvd, MSGDG, totalBytesRcvd));
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
finally
{
netStream.Close();
if (client.Connected) client.Close();
Console.WriteLine("Client ... connection closed");
}
}
}
}