Пример #1
0
int
main(int argc, char **argv)
{
    int to;

    IBS = 256 * 1024;
    if ((IB = (char *) malloc((size_t) IBS)) == NULL) {
        printf("\n\nCan not allocate buffer with %d size...", IBS);
        fflush(stdout);
        return (0);
    }
    Version[0] = '\0';
    Serial[0] = '\0';
    Serial[4] = '\0';
    IBCP = 0;

#ifdef COMM_DEBUG
    printf("\nMemory allocated for %d bytes at %x", IBS, IB);
    fflush(stdout);
#endif

    SetUpSignals();
    zerotime();

    printf("\n... Opening port");
    fflush(stdout);
    if (OpenAndConfigurePort()) {
        return 1;
    }

    gettime();
    printf("\n%6d : Port opened. (%d)", curtime, Port);
    fflush(stdout);
    printf("\n... Clearing buffer");
    fflush(stdout);

    sprintf(Message, "\015\015\015");
    WriteToPort(Message);

    printf("\n... Sending Version Request");
    fflush(stdout);

    sprintf(Message, "V\015");  // get version
    WriteToPort(Message);

    to = 1000000;
    while (to) {
        if (Poll()) {
            if (Version[0] != '\0')
                to = 1;
        }
        --to;
    }
    gettime();

    printf("\n%6d : Version = HW : %c.%c  SW : %c.%c", curtime, Version[0], Version[1], Version[2],
           Version[3]);
    fflush(stdout);
    printf("\n... Sending Serial Request");
    fflush(stdout);

    sprintf(Message, "N\015");  // get Serial
    WriteToPort(Message);

    to = 1000000;
    while (to) {
        if (Poll()) {
            if (Serial[0] != '\0')
                to = 1;
        }
        --to;
    }

    printf("\n%6d : Serial ID = %s  ( %02x:%02x:%02x:%02x )", curtime, Serial, Serial[0], Serial[1],
           Serial[2], Serial[3]);
    fflush(stdout);


// When I need to switch from 230K to 115K and wise versa, I uncomment the #def on this one and change U1/U2...

//#define SET_NEW_UART_SPEED    1
#ifdef SET_NEW_UART_SPEED

    CRSignal = 0;
    sprintf(Message, "U1\015"); // UART with U0=230K, U1=115.2K U2=57.6K
    WriteToPort(Message);
    to = 1000000;
    while (to) {
        if (Poll()) {
            if (CRSignal)
                to = 1;
        }
        --to;
    }
    if (CRSignal)
        printf("Set Successfully...");
    fflush(stdout);

    GoodBye();
    return;
#endif

    CRSignal = 0;
    sprintf(Message, "S6\015"); // CAN with 500Kbps S0-10 S1-20 S2-50 S3-100 S4-125 S5-250 S7-800  S8-1M
    WriteToPort(Message);
    to = 1000000;
    while (to) {
        if (Poll()) {
            if (CRSignal)
                to = 1;
        }
        --to;
    }


    printf("\nCAN speed setup.");
    fflush(stdout);

// The below needs to be done only once...
#if 0
    CRSignal = 0;
    sprintf(Message, "X1\015"); // Turn on out poll
    WriteToPort(Message);
    to = 1000000;
    while (to) {
        if (Poll()) {
            if (CRSignal)
                to = 1;
        }
        --to;
    }

    printf("\nAuto Poll setup.");
    fflush(stdout);

#endif


    CRSignal = 0;
    sprintf(Message, "O\015");  // Open the CAN channel
    WriteToPort(Message);
    to = 1000000;
    while (to) {
        if (Poll()) {
            if (CRSignal)
                to = 1;
        }
        --to;
    }

    printf("\nChannel opened.");
    fflush(stdout);

    CRSignal = 0;
    sprintf(Message, "t0000\015");      // Send 0 byte message to test CAN
    WriteToPort(Message);
    to = 1000000;
    while (to) {
        if (Poll()) {
            if (CRSignal)
                to = 1;
        }
        --to;
    }

    printf("\nBogus message sent, waiting 5 sec., check red LED, should be lit...");
    fflush(stdout);

    sleep(5);

    CRSignal = 0;
    sprintf(Message, "F\015");  // Get Status
    WriteToPort(Message);
    to = 1000000;
    while (to) {
        if (Poll()) {
            if (CRSignal)
                to = 1;
        }
        --to;
    }

    printf("\nStatus Flag = %02x", StatusFlag);
    fflush(stdout);

    to = 1000;
    while (to) {
        if (Poll()) {
        }
        --to;
    }

    GoodBye();

    printf("\n\n");
    fflush(stdout);
    return 0;
}
Пример #2
0
void SWrite3(unsigned char header,unsigned char data, unsigned char end)
{
    unsigned char Buf[3] = {header, data, end};
    std::cerr << "Plant data: "<< mySim.curr_time << " ["<< (int)header << "]["<< (int)data << "]["<< (int)end << "]"<< std::endl;
    WriteToPort(&gftHandle[giDeviceID], 3, &Buf[0]);
}
Пример #3
0
void SWrite(unsigned char data)
{
    unsigned char Buf[1] = {data};
    std::cerr << "Plant data: "<< mySim.curr_time << " ["<< data << "]"<< std::endl;
    WriteToPort(&gftHandle[giDeviceID], 1, &Buf[0]);
}
Пример #4
0
int main(int nargs, char** argv)
{
    struct  termios             oldTio;
    struct  addrinfo            *hostInfoList = nullptr; // Pointer to the linked list of host_info's.
    struct  ThreadSerialInfo    sInfo = {-1, -1, -1, 1, SIM_NONE, &mySim, 0, {0, 0, 0, 0}};
    auto                        serial = ARDUINO_SP_NAME;
    pthread_t                   threadID;
    
    gettimeofday(&gStartTime, NULL);

    signal(SIGINT, SIG_IGN);
    signal(SIGHUP, signalHandler);


    // Hardcode the serial communication for PC client
    mySim.verbose = atoi(argv[2]);

    if(!CreateSocket(&sInfo.gSocketHandle, hostInfoList))
        return 1;
    
    // Create the the thread
    if(pthread_create(&threadID, NULL, &SocketReadThread, &sInfo)) {
        print("Error: Couldn't create the thread\n");
        
        CloseSocket(&sInfo.gSocketHandle, &sInfo.gListenSocketHandle, hostInfoList);
        return 1;
    }
    
    while(sInfo.gSocketHandle==-1)
        usleep(WAIT_TIME_INF_LOOP);
    
    print("Waiting for python script app to connect...\n");
    
    while(sInfo.gListenSocketHandle==-1)
        usleep(WAIT_TIME_INF_LOOP);
    
    print("Python script connected\n");

    // The third parameter for the app is the port name
    if((gftHandle[giDeviceID] = open(serial, O_RDWR | O_NOCTTY | O_NONBLOCK | O_NDELAY))==-1) {
        print("Error: Could not connect to "); print(serial); print("\n");
        CloseSocket(&sInfo.gSocketHandle, &sInfo.gListenSocketHandle, hostInfoList);
        return 1;
    } else {
        print("Opened device "); print(serial); print("\n");
        
        SetDefaultPortSettings(&gftHandle[giDeviceID], &oldTio);
        
        sInfo.serialPortHandle = gftHandle[giDeviceID];
    
        gWaitDescriptors[0].fd = gftHandle[giDeviceID];
        gWaitDescriptors[0].events = POLLIN;
        gWaitDescriptors[1].fd = sInfo.gListenSocketHandle;
        gWaitDescriptors[1].events = POLLIN;
        
        TR_PL_ID *bufIDs = NULL;
        
        while(sInfo.gCommands!=SIM_END) {
            unsigned char Buf = 0, sParBuf[6], tmpBuf[2048];
            int idx = 0;
            ssize_t dwBytes;
            unsigned int nBufIDSize = 0, parValue = 0;
            int pollRc;
            size_t bytesRead = 0;
            unsigned int test = 0;
            
            //simulate a batch of trajectory
            if(sInfo.gCommands != SIM_NONE)
                mySim.SimulateSinglePath();
            
            //usleep(WAIT_TIME_INF_LOOP);
            
            switch (sInfo.gCommands)
            {
                case SIM_START:
                    
                    nAtriumBeats = 0;
                    nVentricleBeats = 0;
                    gettimeofday(&gStartTime, NULL);
                    
                    
                    gTranList.clear();
                    mySim.StartSimulation();
                    
                    Buf = 'W';
                    WriteToPort(&gftHandle[giDeviceID], 1, &Buf);
                    
                    break;
                    
                case SIM_END:
                    
                    Buf = 'S';
                    WriteToPort(&gftHandle[giDeviceID], 1, &Buf);
                    
                    break;
                    
                case SIM_STOP:
                    sInfo.gCommands = SIM_NONE;
                    
                    std::cerr << "********************************"<<std::endl;
                    std::cerr << "Client simulation stop at time: "<< mySim.curr_time << std::endl;
                    
                    Buf = 'S';
                    WriteToPort(&gftHandle[giDeviceID], 1, &Buf);
                    
                    Buf = 0;
                    while(Buf!=SIM_READY) {
                        pollRc = poll(&gWaitDescriptors[0], 2, -1);
                        
                        if (pollRc < 0) {
                            print("Error: setting the poll\n");
                            sInfo.gCommands = SIM_END;
                        } else if(pollRc > 0) {
                            
                            if( gWaitDescriptors[0].revents & POLLIN )
                                bytesRead = read(gftHandle[giDeviceID], &Buf, (int)1);
                            else if(gWaitDescriptors[0].revents & POLLHUP || gWaitDescriptors[0].revents & POLLERR) {
                                print("Error: poll read gftHandle\n");
                                sInfo.gCommands = SIM_END;
                            }
                        }
    
                    }
                    
                    std::cerr << "Number of atrium beats: "<< nAtriumBeats << std::endl;
                    std::cerr << "Number of ventricle beats: "<< nVentricleBeats << std::endl;
                    
                    break;
                    
                case SIM_GET_ID:
                    sInfo.gCommands = SIM_NONE;
                    
                    print("Number of transition IDs: "); print((float)(gTranList.size())); print("\n");
                    
                    //nBufIDSize = (gTranList.size())*sizeof(TR_PL_ID);
                    nBufIDSize = (gTranList.size())*sizeof(EventTime);
                    
                    if(bufIDs!=NULL) delete [] bufIDs;
                    
                    bufIDs = new TR_PL_ID[nBufIDSize];
                    
                    memset((void*)&bufIDs[0], 0, nBufIDSize);
                    
                    dwBytes = send(sInfo.gListenSocketHandle, (void*)&nBufIDSize, sizeof(nBufIDSize), 0);
                    
                    for (std::list<EventTime>::iterator it=gTranList.begin(); it != gTranList.end(); ++it) {
                        bufIDs[idx] = (*it).event;
                        memcpy(&bufIDs[idx+1], (const void*)&(*it).time, sizeof(unsigned int));
                        //print((float)(*it).time); print(" "); print((float)(*it).event); print("\n");
                        //idx++;
                        idx+=sizeof(EventTime);
                    }
                    
                    dwBytes = send(sInfo.gListenSocketHandle, (void*)bufIDs, (size_t)(nBufIDSize), 0);
                    
                    if(!dwBytes)
                        print("No IDs or write socket error\n");
                    else {
                        print("Number of bytes sent: "); print((float)(dwBytes)); print("\n");
                    }

                    break;
                    
                case SIM_SET_CPAR:
                    sInfo.gCommands = SIM_NONE;
                    
                    parValue = BytesToInt(sInfo.gParBuf);
                    print("Setting Client parameter ID: "); print((float)(sInfo.gParId)); print(" Value: "); print((float)parValue); print("\n");
                    
                    SetParameters(sInfo.gParId, (unsigned long)(parValue));
                    
                    Buf = SIM_READY;
                    dwBytes = send(sInfo.gListenSocketHandle, (void*)&Buf, 1, 0);
                    
                    break;
                    
                case SIM_SET_PPAR:
                    sInfo.gCommands = SIM_NONE;
                    
                    
                    parValue = BytesToInt(sInfo.gParBuf);
                    print("Setting Arduino parameter ID: "); print((float)(sInfo.gParId)); print(" Value: "); print((float)parValue); print("\n");
                    
                    sParBuf[0] = 'D';
                    sParBuf[1] = sInfo.gParId;
                    memcpy((void*)&sParBuf[2], (const void*)&sInfo.gParBuf[0], 4);
                    
                    WriteToPort(&gftHandle[giDeviceID], 6, &sParBuf[0]);
                    
                    
                    pollRc = poll(&gWaitDescriptors[0], 2, -1);
                    
                    if (pollRc < 0) {
                        print("Error: setting the poll\n");
                        sInfo.gCommands = SIM_END;
                    } else if(pollRc > 0) {
                        
                        if( gWaitDescriptors[0].revents & POLLIN ) {
                            bytesRead = read(gftHandle[giDeviceID], &Buf, (int)1);
                            if (Buf!=SIM_READY) {
                                print("Wrong Arduino reply: "); print(Buf); print("\n");
                                sInfo.gCommands = SIM_END;
                            }
                            
                            Buf=SIM_READY;
                            dwBytes = send(sInfo.gListenSocketHandle, (void*)&Buf, 1, 0);

                        }
                        else if(gWaitDescriptors[0].revents & POLLHUP || gWaitDescriptors[0].revents & POLLERR) {
                            print("Error: poll read gftHandle\n");
                            sInfo.gCommands = SIM_END;
                        }
                    }
                    break;
                    
                case SIM_NONE:
                    break;
                    
                default:
                    sInfo.gCommands = SIM_NONE;
            }
            
        }
        
        if(bufIDs!=NULL)
            delete [] bufIDs;
    }
    
    // End the thread
    sInfo.gEndThread = 0;

    // wait for the thread to exit
    pthread_join(threadID, NULL);

    CloseSocket(&sInfo.gSocketHandle, &sInfo.gListenSocketHandle, hostInfoList);
    ClosePortDevice(&gftHandle[giDeviceID], &oldTio);
    
    return (0);
}