Knowledge Base Nr: 00214 megamattcp.cpp - http://www.swe-kaiser.de
Downloads:
Qt: hochregallageransteuerung megalift/megamat über tcp/ip
#include <qsocketdevice.h>
char g_szHost[100] = "192.168.0.160";
int g_nPort = 2002;
int hw_megamatPositionieren(char *command)
{
printf("\n\nMegamatschnittstelle senden: <%s>\n", command);
QHostAddress hadr;
hadr.setAddress(g_szHost);
QSocketDevice sock(QSocketDevice::Datagram);
bool bSucc = sock.connect(hadr, g_nPort);
if (bSucc)
{
sock.setBlocking(false);
int nWritten = 0;
char buf[]="XX55\r";
buf[0]=command[0]; //TurmAdresseumkopieren
buf[1]=command[1];
//
nWritten = sock.writeBlock(buf, strlen(buf));
printf("Megamatschnittstelle send %d bytes from %d <%s>\n", nWritten, strlen(buf), buf);
if (nWritten <= 0)
{
return -1;
}
//
strcat(command, "\r");
nWritten = sock.writeBlock(command, strlen(command));
printf("Megamatschnittstelle send %d bytes from %d <%s>\n", nWritten, strlen(command), command);
if (nWritten <= 0)
{
return -1;
}
//
buf[2]='5';
buf[3]='0';
nWritten = sock.writeBlock(buf, strlen(buf));
printf("Megamatschnittstelle send %d bytes from %d <%s>\n", nWritten, strlen(buf), buf);
if (nWritten <= 0)
{
return -1;
}
//
buf[2]='3';
buf[3]='0';
nWritten = sock.writeBlock(buf, strlen(buf));
printf("Megamatschnittstelle send %d bytes from %d <%s>\n", nWritten, strlen(buf), buf);
if (nWritten <= 0)
{
return -1;
}
//
//enum Error { NoError, AlreadyBound, Inaccessible, NoResources, InternalError, Bug = InternalError, Impossible, NoFiles, ConnectionRefused, NetworkFailure, UnknownError }
QSocketDevice::Error nErr = sock.error();
printf("Megamatschnittstelle QSocketDevice::Error = %d { NoError, AlreadyBound, Inaccessible, NoResources, InternalError, Bug = InternalError, Impossible, NoFiles, ConnectionRefused, NetworkFailure, UnknownError }\n", nErr);
if (nErr != QSocketDevice::NoError)
{
return -2;
}
}
else
{
printf("Megamatschnittstelle connect(%s, %d) failed\n", g_szHost, g_nPort);
return -3;
}
printf("Megamatschnittstelle finished\n");
return 0;
}
void Form2::newSlot()
{
strcpy(g_szHost, lineEditIP->text());
g_nPort = lineEditPort->text().toInt();
int nTurmNr = lineEditTurmNo->text().toInt();
int nTabNr =lineEditTabNo->text().toInt();
char szCommand[1000] = {0};
sprintf(szCommand, "%02d05%03d000000000000000 Tablar %03d X", nTurmNr, nTabNr, (nTurmNr*100)+nTabNr);
int nErr = hw_megamatPositionieren(szCommand);
QString str;
str.sprintf("LastErr: %d", nErr);
textLabelEMegamat->setText(str);
}