C# 将代码从C++更改为C使用

C# 将代码从C++更改为C使用,c#,c++,at-command,C#,C++,At Command,我必须做一个小应用程序,它必须发送USSD消息并接收其答案。通过网络进行一些调查后,我使用AT命令。我用C语言开发了一个windows应用程序 这是一些简单的代码,我用它来发送USSD Port.WriteAT+CUSD=0,\*100*1*\,15\r\n 它收到了答案,但需要按摩才能关闭! 我不知道如何关闭它! 我在网上找到了一些用C写的帮助我的代码++ User::LeaveIfError(StartC32()); RCommServ commServer; CleanupClosePu

我必须做一个小应用程序,它必须发送USSD消息并接收其答案。通过网络进行一些调查后,我使用AT命令。我用C语言开发了一个windows应用程序

这是一些简单的代码,我用它来发送USSD

Port.WriteAT+CUSD=0,\*100*1*\,15\r\n

它收到了答案,但需要按摩才能关闭! 我不知道如何关闭它! 我在网上找到了一些用C写的帮助我的代码++

User::LeaveIfError(StartC32());

RCommServ commServer;
CleanupClosePushL( commServer );
User::LeaveIfError(commServer.Connect());

_LIT(KCsyName, "dataport");
User::LeaveIfError(commServer.LoadCommModule(KCsyName));

_LIT(KDataPort, "DATAPORT::1");
TBuf<20> port(KDataPort);
RComm commW;
CleanupClosePushL( commW );

User::LeaveIfError(commW.Open(commServer, port, ECommShared));

TBuf8<128> atCommand;
atCommand.Append(_L8("AT+CUSD=0,\""));
atCommand.Append(aUssdCode);
atCommand.Append(_L8("\",15\r\n"));

TRequestStatus callStatus;
commW.Write(callStatus, _L8("ATE0\r\n"));
User::WaitForRequest(callStatus);
User::LeaveIfError(callStatus.Int());
User::After(1000); 

commW.Write(callStatus, atCommand);
User::WaitForRequest(callStatus);
User::LeaveIfError(callStatus.Int());
User::After(1000); 

TBuf<128> temp;
temp.Copy(atCommand);
CEikonEnv::Static()->InfoWinL(_L("Message Sended:"),temp);

if(callStatus.Int() == KErrNone)
{
  TBuf8<160> replyBuf; 
  commW.Read(callStatus, TTimeIntervalMicroSeconds32(10000000), replyBuf);
  User::WaitForRequest(callStatus);
  User::LeaveIfError(callStatus.Int());
  User::After(1000); 

  TBuf<160> msg;
  msg.Copy(replyBuf);

  CEikonEnv::Static()->InfoWinL(_L("Message Received:"),msg);

  User::LeaveIfError(replyBuf.Compare(atCommand)==0);      
}else
 {
  User::Leave(callStatus.Int());
 }

CleanupStack::PopAndDestroy( &commW );
CleanupStack::PopAndDestroy( &commServer );
我希望有人帮我把一些代码从C++变成C 开玩笑,我要两件东西

首先,我希望代码停止应用程序,直到我得到答案 前

用户::WaitForRequestcallStatus

第二,我想做一个按钮来关闭按摩 前

CleanupStack::PopAndDestroy&commW; CleanupStack::PopAndDestroy&commServer;

我希望我的问题对你来说是清楚的
谢谢,

我想您需要的是C SerialPort类:

 using (System.IO.Ports.SerialPort port = new System.IO.Ports.SerialPort("COM1"))
        {
            port.Open();
            port.WriteLine("AT+CUSD=0,\"");
            var result = port.ReadLine();               
        }

这将在使用后自动关闭并处理串行线路。如果您需要一个按钮来执行此操作,请确保调用这两种方法来终止串行端口流。

谢谢您的回答,我尝试将代码用作:`private void button1\u Clickobject sender,EventArgs e{using System.IO.Ports.SerialPort port=new System.IO.Ports.SerialPortCOM7{port.Open;port.writelineeat+CUSD=0,\*147*1\,15\r\n;var result=port.ReadLine;}`当我按下按钮2时,信息未关闭,信息有一个选项,例如1.Eng 2.other,通过按下多个按钮发送USSD,它将不会作为新的USSD Comend发送!选择它