Knowledge Base Nr: 00201 megamat.cpp - http://www.swe-kaiser.de
Downloads:
megamat lagersystem ansteuern
//infos: www.megamat.com
beispiel-telegramme:
--------------------
tablar anfordern:
0105002000000000000000 Tablar 102 X\r
status abfragen:
0145\r
samplecode linux:
-------------------
int hw_megamatPositionieren(char *command)
{
//9600,n,8,1
char szMegamatport[1024] = {0};
strcpy(szMegamatport, g_base.getConfig("PORT_MEGAMAT"));
if (!szMegamatport[0] || (szMegamatport[0] == 0))
return 0;
int fd = open(szMegamatport, O_RDWR | O_NONBLOCK | O_NOCTTY, 0);
if (fd < 0)
{
LogMessage(LOG_PRIO_ERR, "Megamatschnittstelle <%s> kann nicht geöffnet werden <%d>", szMegamatport, fd);
return -1;
}
struct termios termio;
memset(&termio, 0, sizeof(termio));
cfsetospeed(&termio,B9600);
cfsetispeed(&termio,B9600);
termio.c_cflag |= (CLOCAL|CREAD);
termio.c_cflag |= CS8;
tcsetattr(fd, TCSANOW, &termio);
//Handspreicher loeschen
char buf[]="XX55\r";
buf[0]=command[0]; //TurmAdresseumkopieren
buf[1]=command[1];
LogMessage(LOG_PRIO_INFO, "Megamat schreiben <6> bytes : <%02x %02x %02x %02x %02x %02x", buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]);
int nErr = write(fd, buf, sizeof(buf));
if (nErr < 0)
{
LogMessage(LOG_PRIO_ERR, "Megamat Schreibfehler <%d> <%s>", nErr, buf);
return -2;
}
usleep(200*1000);
char szAnswer[100] = {0};
nErr = read(fd, szAnswer, sizeof(szAnswer));
if (nErr < 0)
{
LogMessage(LOG_PRIO_ERR, "Megamat Lesefehler <%d> <%s>", nErr, buf);
return -22;
}
//tablar anfordern
char buf2[100];
sprintf(buf2,"%s\r",command);
LogMessage(LOG_PRIO_INFO, "Megamat schreiben <%d> bytes : <%s>", sizeof(buf2), buf2);
nErr = write(fd, buf2, strlen(buf2));
if (nErr < 0)
{
LogMessage(LOG_PRIO_ERR, "Megamat Schreibfehler <%d> <%s>", nErr, buf);
return -4;
}
usleep(20*1000);
char buf50[]="XX50\r";
buf50[0]=command[0]; //Adresse
buf50[1]=command[1]; //umkopieren
char buf30[]="XX30\r";
buf30[0]=command[0]; //Adresse
buf30[1]=command[1]; //umkopieren
///
LogMessage(LOG_PRIO_INFO, "Megamat schreiben <%d> bytes : <%s>", sizeof(buf50), buf50);
nErr = write(fd, buf50, strlen(buf50));
if (nErr < 0)
{
LogMessage(LOG_PRIO_ERR, "Megamat Schreibfehler <%d> <%s>", nErr, buf50);
return -2;
}
usleep(200*1000);
nErr = read(fd, szAnswer, sizeof(szAnswer));
if (nErr < 0)
{
LogMessage(LOG_PRIO_ERR, "Megamat Lesefehler <%d> <%s>", nErr, buf50);
return -22;
}
///
LogMessage(LOG_PRIO_INFO, "Megamat schreiben <%d> bytes : <%s>", sizeof(buf30), buf30);
nErr = write(fd, buf30, strlen(buf30));
if (nErr < 0)
{
LogMessage(LOG_PRIO_ERR, "Megamat Schreibfehler <%d> <%s>", nErr, buf30);
return -2;
}
usleep(200*1000);
nErr = read(fd, szAnswer, sizeof(szAnswer));
if (nErr < 0)
{
LogMessage(LOG_PRIO_ERR, "Megamat Lesefehler <%d> <%s>", nErr, buf30);
return -22;
}
///
LogMessage(LOG_PRIO_INFO, "Megamat schreiben <%d> bytes : <%s>", sizeof(buf50), buf50);
nErr = write(fd, buf50, strlen(buf));
if (nErr < 0)
{
LogMessage(LOG_PRIO_ERR, "Megamat Schreibfehler <%d> <%s>", nErr, buf50);
return -2;
}
usleep(200*1000);
nErr = read(fd, szAnswer, sizeof(szAnswer));
if (nErr < 0)
{
LogMessage(LOG_PRIO_ERR, "Megamat Lesefehler <%d> <%s>", nErr, buf50);
return -22;
}
close(fd);
return 0;
}
samplecode windows:
-------------------
int hw_megamatPositionieren(CWnd* pErr, CWnd* pRecv, char *command)
{
pRecv->SetWindowText("?");
pErr->SetWindowText("?");
//9600,n,8,1
CSerialCom meg(0, "COM1", 9600, 'N', 8, 1, 'N');
int nErr = meg.Init();
if (nErr)
{
pErr->SetWindowText("Kommunikation zu Megamat failed (init)!");
return -1;
}
//Handspreicher loeschen
char buf[]="XX55\r";
buf[0]=command[0]; //TurmAdresseumkopieren
buf[1]=command[1];
nErr = meg.Write(buf, strlen(buf));
if (nErr)
{
pErr->SetWindowText("Kommunikation zu Megamat failed (write)!");
return -2;
}
Sleep(300);
char szAnswer[100] = {0};
int nLen = sizeof(szAnswer);
nErr = meg.Read(szAnswer, &nLen, 1000);
if (nErr)
{
pErr->SetWindowText("Kommunikation zu Megamat failed (write)!");
return -3;
}
Sleep(500);
pRecv->SetWindowText(szAnswer);
//tablar anfordern
char buf2[100];
sprintf(buf2,"%s\r", command);
nErr = meg.Write(buf2, strlen(buf2));
if (nErr < 0)
{
pErr->SetWindowText("Megamat Schreibfehler!");
return -4;
}
Sleep(20);
///////
//quittieren
char buf1[]="XX50\r";
buf1[0]=command[0]; //Adresse
buf1[1]=command[1]; //umkopieren
nErr = meg.Write(buf1, strlen(buf1));
if (nErr)
{
pErr->SetWindowText("Kommunikation zu Megamat failed (write)!");
return -5;
}
nLen = sizeof(szAnswer);
nErr = meg.Read(szAnswer, &nLen, 500);
if (nErr)
{
pErr->SetWindowText("Kommunikation zu Megamat failed (write)!");
return -3;
}
char buf3[]="XX30\r";
buf1[0]=command[0]; //Adresse
buf1[1]=command[1]; //umkopieren
nErr = meg.Write(buf3, strlen(buf3));
if (nErr)
{
pErr->SetWindowText("Kommunikation zu Megamat failed (write)!");
return -5;
}
nLen = sizeof(szAnswer);
nErr = meg.Read(szAnswer, &nLen, 500);
if (nErr)
{
pErr->SetWindowText("Kommunikation zu Megamat failed (write)!");
return -3;
}
nErr = meg.Write(buf1, strlen(buf1));
if (nErr)
{
pErr->SetWindowText("Kommunikation zu Megamat failed (write)!");
return -5;
}
nLen = sizeof(szAnswer);
nErr = meg.Read(szAnswer, &nLen, 500);
if (nErr)
{
pErr->SetWindowText("Kommunikation zu Megamat failed (write)!");
return -3;
}
///////
pErr->SetWindowText("ok!");
return 0;
}