inside MainWindow:
plcTimer = new PlcTimer();
connect(plcTimer, SIGNAL(jack_sendToRS(char*,int)), rs_plc, SLOT(rs_plcDataAqustn(char*,int)));
plcTimer->start();
rs_plc->rs_plcOpenPort((char *)"/dev/ttyS0"); /*/dev/ttyS3*/
plcTimer = new PlcTimer();
connect(plcTimer, SIGNAL(jack_sendToRS(char*,int)), rs_plc, SLOT(rs_plcDataAqustn(char*,int)));
plcTimer->start();
rs_plc->rs_plcOpenPort((char *)"/dev/ttyS0"); /*/dev/ttyS3*/
To copy to clipboard, switch view to plain text mode
QThread signal or QTimer signal connects to serial SLOT which write n reads packet to and from serial port:
bool RS::rs_plcDataAqustn(char* data, int len)
{
// QByteArray built((char*)data, 6) ;
// qDebug() << built.toHex();
if(!rs_serialWrite(data, len))
{
qDebug() << "Failure:( rs_dataqustn: rs_plcWrite(data, len)";
emit plc_port_dscntd();
return false;
}
if(len == 3)
{
qDebug() << "len = 3";
rs_delay();
if(rs_plcRead(&rd15Bytes))
{
qDebug() << rd15Bytes.length();
//qDebug() << "->" << rd15Bytes.toHex();
if(rd15Bytes.isEmpty())
{
emit logMessage("Failure:( rs_dataAqustn, if(rd15Bytes.isEmpty())");
}
else
{
if(!rs_plcCheckChecksum(&rd15Bytes))
emit logMessage("Failure:( rs_dataAqustn, if(!rs_plcCheckChecksum(&rd15Bytes))");
else
{
emit rs_plcSendRdPacket(rd15Bytes);
if(!rs_serialWrite((char* )"0x06", 1))
{
qDebug() << "Failure:( rs_dataqustn: rs_plcWrite(PLC_ACK, 1)";
}
}
}
}
else
{
emit plc_hmiBoard_dscntd();
}
}
return true;
}
bool RS::rs_plcDataAqustn(char* data, int len)
{
QByteArray rd15Bytes;
// QByteArray built((char*)data, 6) ;
// qDebug() << built.toHex();
if(!rs_serialWrite(data, len))
{
qDebug() << "Failure:( rs_dataqustn: rs_plcWrite(data, len)";
emit plc_port_dscntd();
return false;
}
if(len == 3)
{
qDebug() << "len = 3";
rs_delay();
if(rs_plcRead(&rd15Bytes))
{
qDebug() << rd15Bytes.length();
//qDebug() << "->" << rd15Bytes.toHex();
if(rd15Bytes.isEmpty())
{
emit logMessage("Failure:( rs_dataAqustn, if(rd15Bytes.isEmpty())");
}
else
{
if(!rs_plcCheckChecksum(&rd15Bytes))
emit logMessage("Failure:( rs_dataAqustn, if(!rs_plcCheckChecksum(&rd15Bytes))");
else
{
emit rs_plcSendRdPacket(rd15Bytes);
if(!rs_serialWrite((char* )"0x06", 1))
{
qDebug() << "Failure:( rs_dataqustn: rs_plcWrite(PLC_ACK, 1)";
}
}
}
}
else
{
emit plc_hmiBoard_dscntd();
}
}
return true;
}
To copy to clipboard, switch view to plain text mode
Read from Plc:
{
unsigned char buff[128];
int len, tries;
tries = 0;
while(tries < 13)
{
len = read(fd, buff, PLC_READ_BYTE);
if((buff[0] == PLC_HEADER) && (len == PLC_READ_BYTE))
{
data->append((char *)buff, PLC_READ_BYTE);
//qDebug() << data->toHex();
return true;
}
else
{
tries++;
qDebug() << "retry";
}
}
return false;
}
bool RS::rs_plcRead(QByteArray *data)
{
unsigned char buff[128];
int len, tries;
tries = 0;
while(tries < 13)
{
len = read(fd, buff, PLC_READ_BYTE);
if((buff[0] == PLC_HEADER) && (len == PLC_READ_BYTE))
{
data->append((char *)buff, PLC_READ_BYTE);
//qDebug() << data->toHex();
return true;
}
else
{
tries++;
qDebug() << "retry";
}
}
return false;
}
To copy to clipboard, switch view to plain text mode
should you need more info, please note it.
Bookmarks