It works fine, I process data from client and then show a message in wxTextCtrl (multiline) with information for user.
But when I move window or select text in wxTextCtrl my app doesn't send any kind of data to client.
I'm not working with secondary threads for this at the moment.
Code: Select all
BEGIN_EVENT_TABLE(ServerDialog,wxDialog)
//(*EventTable(ServerDialog)
//*)
EVT_BUTTON(ID_BTNHIDE, ServerDialog::ClickHide)
EVT_BUTTON(ID_BTNEXIT, ServerDialog::OnQuit)
EVT_CLOSE(ServerDialog::OnCloseWindow)
EVT_BUTTON(ID_BTNSTART, ServerDialog::OnServerStart)
EVT_SOCKET(SERVER_ID, ServerDialog::OnServerEvent)
EVT_SOCKET(SOCKET_ID_WEB, ServerDialog::OnSocketEventWeb)
EVT_SOCKET(SOCKET_ID_REG, ServerDialog::OnSocketEventReg)
END_EVENT_TABLE()
Code: Select all
void ServerDialog::OnSocketEventReg(wxSocketEvent& event)
{
// The socket that had the event
wxSocketBase* sock = event.GetSocket();
switch(event.GetSocketEvent())
{
case wxSOCKET_CONNECTION:
break;
case wxSOCKET_INPUT:
{
/****************************************************************************************************************************/
char buf[10] = {0,0,0,0,0,0,0,0,0,0};
wxDateTime dt = wxDateTime().Now();
//Formatear texto de hora y fecha para enviar al servidor...
wxString strdt = dt.Format("%H:%M:%S %d/%m/%y");
sock->Read(buf, sizeof(buf)-1); // Se reciben máximo 9 caracteres (se deja espacio para \0)
char c1er_ = buf[0];
// "(tipo)(space)(d1d2d3d4)" 6 caracteres + 1 nulo => máximo = 7
// Verificar integridad de formato de peso...
if((c1er_ == 'A' || c1er_ == 'B' || c1er_ == 'C' || c1er_ == 'D') && buf[1]== ' ' && sock->LastCount() <= 7)
{
wxString str_peso = &buf[2];
if(str_peso.IsNumber())
{
// Registro normal...
txtMsg->AppendText(buf); // Mostrar cadena de registro
txtMsg->AppendText(" ");
txtMsg->AppendText(strdt);
txtMsg->AppendText("\n");
// Acá se envia la información de tiempo para registro en arduino
sock->Write(strdt.c_str(), 18);
// Modo TEST
if(bmodoTest)
txtMsg->AppendText("(MODO PRUEBA)\n");
else
{ // Finalmente se registra...
RegData(buf, strdt);
}
}
}
else
{
int ncar = sock->LastCount();
// Si es solo 1 byte y es 'N' => Fin de molinada actual...
if(c1er_ == 'N' && ncar == 1)
{
// Respuesta... (no necesaria)
txtMsg->AppendText("Fin de molinada actual\n");
// Leer agua (para conocer valor inicial antes del consumo de agua).
if(bmodoTest)
{
wxString str;
str.Printf("%.1f",cntAguam3);
txtMsg->AppendText("Contador acumulado (m3): " + str + "\n");
txtMsg->AppendText("(MODO PRUEBA)\n");
}
else
{
cntAguam3 = this->LeerAgua(); // devuelve valor de agua en m3...
}
// Este valor se usará para registrar y ya no la lectura actual en cada pesaje.
}
else
// Si el primer caracter y único byte es 'F' => Obtener formulación...
if(c1er_ == 'F' && ncar == 1)
{
wxString strFormula = strCmdFormula;
if(bmodoTest == true)
{
strFormula = "S0000 0000 0000 0000 ";
}
txtMsg->AppendText("F: " + strFormula + "\n");
sock->Write(strFormula.c_str(), strFormula.length()+1);
}
else
// Si solo es H enviar respuesta de Fecha y hora
if(c1er_ == 'H' && ncar == 1)
{
// Acá se envia la información...
sock->Write(strdt.c_str(), 18);
//txtMsg->AppendText("FyH " + strdt.c_str() + "\n");
}
else
// Si solo es H enviar respuesta de Fecha y hora
if(c1er_ == 'K' && ncar == 1)
{
// Acá se envia la información...
sock->Write("x", 1);
txtMsg->AppendText("ACK\n");
}
else
{
txtMsg->AppendText("Reset\n");
sock->Discard();
}
}
/*****************/
break;
}
case wxSOCKET_LOST:
{
wxIPV4address remote;
sock->GetPeer(remote);
txtMsg->AppendText(" Se ha cerrado la conexión con el registrador...\n");
txtMsg->AppendText("IP Cliente: " + remote.IPAddress()+"\n");
unsigned short nport = remote.Service();
txtMsg->AppendText("Puerto: " + wxString().Format("%d", nport) +"\n");
nclients--;
if(nclients <0)
nclients = 0;
txtMsg->AppendText("N° Clientes: " + wxString::Format("%i",nclients) +"\n");
st_estado->SetLabel("No Conectado");
Led1->SwitchOff(); // Apagar led de conexión aceptada
sock->Close();
//sock->Destroy();
break;
}
case wxSOCKET_OUTPUT:
break;
}
}