问题:服务器应该向客户端发送一个 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;
}
命令:服务器向客户端发送消息。客户端收到消息并调用其中包含答案的服务方法。服务器应该使用这个答案,但在他得到答案之前发送下一条消息。
希望有人知道答案。