Ejemplo n.º 1
0
/*!
 * \brief Rs232DevicePrivate::Rs232DevicePrivate - CTor
 * \param info
 * \param parent
 */
Rs232DevicePrivate::Rs232DevicePrivate(const QSerialPortInfo &info, QObject *parent) :
    QSerialPort(info, parent)
{
    m_debug = false;

    qDebug() << headDebug << "CTor" << info.portName();

    // Faccio partire un timer: se entro il suo timeout non ho trovato il converter mi autodistruggo
    connect (&m_timerAutodelete, SIGNAL(timeout()), this, SLOT(deleteLater()));
    m_timerAutodelete.start(2000);

    connect (&m_timerSendGetId, SIGNAL(timeout()), this, SLOT(sendMsgGetId()));

    // Sono riuscito a configurare la porta?
    if (configPort())
    {
        // Si, la porta e' fisica e non virtuale.
        m_statoParser = STATO_RS232_DLE_STX;
        m_checksum = 0;

        connect(this, SIGNAL(error(QSerialPort::SerialPortError)),
                this, SLOT(errorSlot(QSerialPort::SerialPortError)));
        connect(this, SIGNAL(readyRead()), this, SLOT(fromDeviceSlot()));

        // Spedisco il messaggio per sapere se e' collegato un converter
        sendMsgGetId();
    }
}
Ejemplo n.º 2
0
  bool SerialPort::openPort()
  {
      if((!_port_is_open)&&(_port_name.c_str()!=NULL))
      {
          _file_descriptor = open(_port_name.c_str(), O_RDWR | O_NOCTTY | O_NDELAY);
      }
      else if(_port_is_open)
           {
             ROS_ERROR_STREAM("open port unable to open port : "<< _port_name << " port is already open");
             return false;
           }
           else
           {
             ROS_ERROR_STREAM("open port unable to open port : no port name is given");
             return false;
           }

      if(_file_descriptor==-1)
      {
          ROS_ERROR_STREAM("open port unable to open port : "<< _port_name);
          return false;
      }
      else
      {
          fcntl(_file_descriptor,F_SETFL,0);
          _port_is_open = true;
          if(configPort())
          {
            return true;
          }
          else
          {
              ROS_ERROR_STREAM("Can't configure the port named :" << _port_name);
              return false;
          }
      }
  }
Ejemplo n.º 3
0
Archivo: io.c Proyecto: AndTH/GCA
void setupIO(byte clr) {
    int idx = 0;
    byte firstTime = eeRead(EE_FIRSTTIME);

    // all digital I/O
    ADCON0 = 0x00;
    ADCON1 = 0x0F;

    TRISBbits.TRISB6 = 0; /* LED1 */
    TRISBbits.TRISB7 = 0; /* LED2 */
    TRISBbits.TRISB5 = 0; /* LED3 */
    TRISAbits.TRISA2 = 1; /* Push button */

    LED1 = 0;
    LED2 = 0;
    LED3 = 0;

    if (firstTime == 0xFF) {
        eeWrite(EE_FIRSTTIME, 0);
        clr = TRUE;
    }


    // following presets are written to eeprom if the flim switch is preshed at boot

    // preset port 1-8 as output
    for (idx = 0; idx < 8; idx++) {
        Ports[idx].cfg = PORTCFG_OUT;
        Ports[idx].status = 0;
        Ports[idx].timedoff = 0;
        Ports[idx].timer = 0;
        Ports[idx].evtnn = 0;
        Ports[idx].addr = idx + 1;
        if (clr) {
            eeWrite(EE_PORTCFG + idx, Ports[idx].cfg);
            eeWriteShort(EE_PORTNN + (2 * idx), Ports[idx].evtnn);
            eeWriteShort(EE_PORTADDR + (2 * idx), Ports[idx].addr);
        }
    }

    // preset port 9-16 as input with off delay
    for (idx = 8; idx < 16; idx++) {
        Ports[idx].cfg = PORTCFG_IN | PORTCFG_OFFDELAY;
        Ports[idx].status = 0;
        Ports[idx].timedoff = 0;
        Ports[idx].timer = 0;
        Ports[idx].evtnn = 0;
        Ports[idx].addr = idx + 1;
        if (clr) {
            eeWrite(EE_PORTCFG + idx, Ports[idx].cfg);
            eeWriteShort(EE_PORTNN + (2 * idx), Ports[idx].evtnn);
            eeWriteShort(EE_PORTADDR + (2 * idx), Ports[idx].addr);
        }
    }

    if (clr) {
        eeWrite(EE_PORTSTAT + 0, 0);
        eeWrite(EE_PORTSTAT + 1, 0);
    }

    for (idx = 0; idx < 16; idx++) {
        Ports[idx].cfg = eeRead(EE_PORTCFG + idx);
        if ((Ports[idx].cfg & PORTCFG_IO) == PORTCFG_OUT)
            Ports[idx].status = eeRead(EE_PORTSTAT + idx);
        Ports[idx].timedoff = 0;
        Ports[idx].timer = 0;
        Ports[idx].evtnn = eeReadShort(EE_PORTNN + (2 * idx));
        Ports[idx].addr = eeReadShort(EE_PORTADDR + (2 * idx));
    }

    for (idx = 0; idx < 16; idx++)
        configPort(idx);

}
Ejemplo n.º 4
0
Archivo: commands.c Proyecto: AndTH/GCA
unsigned char parseCmd(CANMsg *cmsg) {
    unsigned char txed = 0;
    //mode_word.s_full = 0;
    switch (cmsg->b[d0]) {

        case OPC_ASRQ:
        {
            int addr = cmsg->b[d3] * 256 + cmsg->b[d4];
            if (SOD == addr && doSOD == 0) {
                ioIdx = 0;
                doSOD = 1;
            }
            break;
        }

        case OPC_ACON:
        case OPC_ASON:
        {
            ushort nn = cmsg->b[d1] * 256 + cmsg->b[d2];
            ushort addr = cmsg->b[d3] * 256 + cmsg->b[d4];
            setOutput(nn, addr, 1);
            break;
        }

        case OPC_ACOF:
        case OPC_ASOF:
        {
            ushort nn = cmsg->b[d1] * 256 + cmsg->b[d2];
            ushort addr = cmsg->b[d3] * 256 + cmsg->b[d4];
            setOutput(nn, addr, 0);
            break;
        }

        case OPC_RQNPN:
            // Request to read a parameter
            if (thisNN(cmsg) == 1) {
                doRqnpn((unsigned int) cmsg->b[d3]);
            }
            break;

        case OPC_SNN:
        {
            if (Wait4NN) {
                unsigned char nnH = cmsg->b[d1];
                unsigned char nnL = cmsg->b[d2];
                NN_temp = nnH * 256 + nnL;
                eeWrite(EE_NN, nnH);
                eeWrite(EE_NN + 1, nnL);
                Wait4NN = 0;
                LED2 = 0;
            }
            break;
        }

        case OPC_RQNP:
            if (Wait4NN) {
                CANMsg canmsg;
                canmsg.b[d0] = OPC_PARAMS;
                canmsg.b[d1] = params[0];
                canmsg.b[d2] = params[1];
                canmsg.b[d3] = params[2];
                canmsg.b[d4] = params[3];
                canmsg.b[d5] = params[4];
                canmsg.b[d6] = params[5];
                canmsg.b[d7] = params[6];
                canmsg.b[dlc] = 8;
                canbusSend(&canmsg);
                txed = 1;
            }
            break;

        case OPC_BOOT:
            // Enter bootloader mode if NN matches
            if (thisNN(cmsg) == 1) {
                eeWrite((unsigned char) (&bootflag), 0xFF);
                Reset();
            }
            break;

        case OPC_RTOF:
            if (NV1 & CFG_SAVEOUTPUT) {
                saveOutputStates();
            }
            break;

        case OPC_NNCLR:
            if (thisNN(cmsg) && isLearning) {
                setupIO(TRUE);
            }
            break;

        case OPC_QNN:
        {
            CANMsg canmsg;
            canmsg.b[d0] = OPC_PNN;
            canmsg.b[d1] = (NN_temp / 256) & 0xFF;
            canmsg.b[d2] = (NN_temp % 256) & 0xFF;
            canmsg.b[d3] = params[0];
            canmsg.b[d4] = params[2];
            canmsg.b[d5] = NV1;
            canmsg.b[dlc] = 6;
            canbusSend(&canmsg);
        }
            //LED2 = 1;
            txed = 1;
            break;

        case OPC_RTON:
            pnnCount = 0;
            /*
            setOutput(0,  9, (pnnCount & 0x01) ? 1:0);
            setOutput(0, 10, (pnnCount & 0x02) ? 1:0);
            setOutput(0, 11, (pnnCount & 0x04) ? 1:0);
            setOutput(0, 12, (pnnCount & 0x08) ? 1:0);
            setOutput(0, 13, (pnnCount & 0x10) ? 1:0);
            setOutput(0, 14, (pnnCount & 0x20) ? 1:0);
            setOutput(0, 15, (pnnCount & 0x40) ? 1:0);
            setOutput(0, 16, (pnnCount & 0x80) ? 1:0);
             */
            break;

        case OPC_PNN:
            pnnCount++;
            /*
            setOutput(0,  9, (pnnCount & 0x01) ? 1:0);
            setOutput(0, 10, (pnnCount & 0x02) ? 1:0);
            setOutput(0, 11, (pnnCount & 0x04) ? 1:0);
            setOutput(0, 12, (pnnCount & 0x08) ? 1:0);
            setOutput(0, 13, (pnnCount & 0x10) ? 1:0);
            setOutput(0, 14, (pnnCount & 0x20) ? 1:0);
            setOutput(0, 15, (pnnCount & 0x40) ? 1:0);
            setOutput(0, 16, (pnnCount & 0x80) ? 1:0);
             */
            break;

        case OPC_NVRD:
            if (thisNN(cmsg)) {
                CANMsg canmsg;
                byte nvnr = cmsg->b[d3];
                if (nvnr == 1) {
                    canmsg.b[d0] = OPC_NVANS;
                    canmsg.b[d1] = (NN_temp / 256) & 0xFF;
                    canmsg.b[d2] = (NN_temp % 256) & 0xFF;
                    canmsg.b[d3] = nvnr;
                    canmsg.b[d4] = NV1;
                    canmsg.b[dlc] = 5;
                    canbusSend(&canmsg);
                    txed = 1;
                } else if (nvnr < 18) {
                    canmsg.b[d0] = OPC_NVANS;
                    canmsg.b[d1] = (NN_temp / 256) & 0xFF;
                    canmsg.b[d2] = (NN_temp % 256) & 0xFF;
                    canmsg.b[d3] = nvnr;
                    canmsg.b[d4] = Ports[nvnr - 2].cfg;
                    canmsg.b[dlc] = 5;
                    canbusSend(&canmsg);
                    txed = 1;
                } else if (nvnr == 18) {
                    canmsg.b[d0] = OPC_NVANS;
                    canmsg.b[d1] = (NN_temp / 256) & 0xFF;
                    canmsg.b[d2] = (NN_temp % 256) & 0xFF;
                    canmsg.b[d3] = nvnr;
                    canmsg.b[d4] = getPortStates(0); // port status 1-8
                    canmsg.b[dlc] = 5;
                    canbusSend(&canmsg);
                    txed = 1;
                } else if (nvnr == 19) {
                    canmsg.b[d0] = OPC_NVANS;
                    canmsg.b[d1] = (NN_temp / 256) & 0xFF;
                    canmsg.b[d2] = (NN_temp % 256) & 0xFF;
                    canmsg.b[d3] = nvnr;
                    canmsg.b[d4] = getPortStates(1); // port status 9-16
                    canmsg.b[dlc] = 5;
                    canbusSend(&canmsg);
                    txed = 1;
                } else if (nvnr == 20) {
                    canmsg.b[d0] = OPC_NVANS;
                    canmsg.b[d1] = (NN_temp / 256) & 0xFF;
                    canmsg.b[d2] = (NN_temp % 256) & 0xFF;
                    canmsg.b[d3] = nvnr;
                    canmsg.b[d4] = CANID;
                    canmsg.b[dlc] = 5;
                    canbusSend(&canmsg);
                    txed = 1;
                }
            }
            break;

        case OPC_NVSET:
            if (thisNN(cmsg)) {
                byte nvnr = cmsg->b[d3];
                if (nvnr == 1) {
                    NV1 = cmsg->b[d4];
                    eeWrite(EE_NV, NV1);
                } else if (nvnr < 18) {
                    Ports[nvnr - 2].cfg = cmsg->b[d4];
                    eeWrite(EE_PORTCFG + (nvnr - 2), Ports[nvnr - 2].cfg);
                    configPort(nvnr - 2);
                } else if (nvnr == 20) {
                    CANID = cmsg->b[d4];
                    eeWrite(EE_CANID, CANID);
                }

            }
            break;

        case OPC_NNLRN:
            if (thisNN(cmsg)) {
                isLearning = TRUE;
            }
            break;

        case OPC_NNULN:
            if (thisNN(cmsg)) {
                isLearning = FALSE;
                LED2 = PORT_OFF;
            }
            break;

        case OPC_EVLRN:
            if (isLearning) {
                ushort evtnn = cmsg->b[d1] * 256 + cmsg->b[d2];
                ushort addr = cmsg->b[d3] * 256 + cmsg->b[d4];
                byte idx = cmsg->b[d5];
                byte val = cmsg->b[d6];
                if (idx < 16) {
                    Ports[idx].evtnn = evtnn;
                    Ports[idx].addr = addr;
                    eeWriteShort(EE_PORTNN + (2 * idx), evtnn);
                    eeWriteShort(EE_PORTADDR + (2 * idx), addr);
                }
                if (idx == 16) {
                    SOD = addr;
                    eeWrite(EE_SOD, addr / 256);
                    eeWrite(EE_SOD + 1, addr % 256);
                }
            }
            break;

        case OPC_NERD:
            if (thisNN(cmsg)) {
                doEV = 1;
                evIdx = 0;
                // start of day event
                {
                    CANMsg canmsg;
                    canmsg.b[d0] = OPC_ENRSP;
                    canmsg.b[d1] = (NN_temp / 256) & 0xFF;
                    canmsg.b[d2] = (NN_temp % 256) & 0xFF;
                    canmsg.b[d3] = 0;
                    canmsg.b[d4] = 0;
                    canmsg.b[d5] = SOD / 256;
                    canmsg.b[d6] = SOD % 256;
                    canmsg.b[d7] = 16;
                    canmsg.b[dlc] = 8;
                    canbusSend(&canmsg);
                }
                txed = 1;
            }
            break;


        default: break;
    }
    return txed;
}