0

问题:服务器应该向客户端发送一个 SerialPort 命令(例如“S”)并从客户端接收端口。Readline 并像发送另一个命令(例如“r01”)一样。

示例:服务器发送“S”并从客户端接收“N”,因此服务器应该发送另一个“S”。如果它收到 != "N" 然后发送 "r01" 等等。

我可以向客户发送消息,但我无法收到答案并存储/使用它。

到目前为止我尝试了什么:

.) 将接收到的信息保存到 serv.connectedClients[ID].receive

服务器:工人阶级:

 public void doSomething(Service serv, string ID)
    { 
       serv.SendMessageToClient(client_name, "S");   //send first message                     
        data = serv.connectedClients[ID].receive.ToString(); //the .receive is empty but should have the clients answer stored
       if(data != ""){
          serv.SendMessageToClient(client_name, "r01");  //send second message
          serv.connectedClients[ID].Information = serv.connectedClients[ID].receive.ToString();
          }
    }

在 Service.cs 中:

  public void getPortMsg(string msg)
    { 
        connectedClients[OperationContext.Current.Channel.SessionId].receive = msg;
    }

在客户端:

 void callback_OnMessageReceivedEvent(string message)
    {
      string portmsg = "";
      portmsg = rfid.send(message); //portmsg gets the port.readline info
       client.getPortMsg(portmsg); //send portmsg to server

    }

和 rfid.send:

 public string send(string senden)
    {

        string data = "";
        port.WriteLine(senden);
        data = port.ReadLine();

            return data;
      }

命令:服务器向客户端发送消息。客户端收到消息并调用其中包含答案的服务方法。服务器应该使用这个答案,但在他得到答案之前发送下一条消息。

希望有人知道答案。

4

1 回答 1

0

我有一个答案。我不认为这是解决问题的最佳方法,但它有效:

服务器向客户端发送消息+方法的名称。

客户端处理消息并向服务器+方法名称写入答案。

服务器现在知道如何处理答案并调用正确的方法。

代码示例:

服务器:

public void methodA(){
   sendToClient("i want an answer",methodA);
}

客户:

 public void callback_messageFromServer(string msg,string location){
     string answer = workWithMsg(msg);
     client.sendAnswer(answer,location);
 }

服务器:

public void sendAnswer(string msg, string location) {
      if(location == "methodA")
          methodB(msg);
 }

要调用相同的方法,我使用 int count 和 switch(count)。希望它可以帮助某人^^

此致

于 2012-08-28T10:56:04.660 回答