Beispiel #1
0
string Serial::readln(ofSerial& serialPort, unsigned char * bytesReturned, string& messageBuffer){

    // if we've got new bytes
    if(serialPort.available() > 0){
        // we wil keep reading until nothing is left
        while (serialPort.available() > 0){
            
            // we'll put the incoming bytes into bytesReturned
            serialPort.readBytes(bytesReturned, NUM_BYTES);
            
            // if we find the splitter we put all the buffered messages 
            // in the final message, stop listening for more data and 
            // notify a possible listener
            // else we just keep filling the buffer with incoming bytes. 
            if(*bytesReturned == '\n'){
                
                return messageBuffer;
                
            }else{
                if(*bytesReturned != '\r') messageBuffer += *bytesReturned;
            }
            //            cout << "  messageBuffer: (" << messageBuffer << ")\n";
        }
        
        // clear the message buffer
        memset(bytesReturned,0,NUM_BYTES);
    }

}
void StepperControl::read(ofSerial& serialPort, int stepperNum, unsigned char * bytesReturned, string& messageBuffer, string& message)
{
    
    // if we've got new bytes
    if(serialPort.available() > 0)
    {
        // we wil keep reading until nothing is left
        while (serialPort.available() > 0)
        {
            
            // we'll put the incoming bytes into bytesReturned
            serialPort.readBytes(bytesReturned, NUM_BYTES);
            
            // if we find the splitter we put all the buffered messages 
            //   in the final message, stop listening for more data and 
            //   notify a possible listener
            // else we just keep filling the buffer with incoming bytes. 
            if(*bytesReturned == '\n')
            {
                message = messageBuffer;
                messageBuffer = "";
                
                if (message == "done"){
                    message = "";
                    cout << "stepper"<<stepperNum<<" is done\n";
                    switch (stepperNum) {
                        case 0:
                            stepper0Ready = true;
                            break;
                            
                        case 1:
                            stepper1Ready = true;
                            break;
                            
                        case 2:
                            stepper2Ready = true;
                            break;
                            
                        default:
                            break;
                    }
                }else {
                    cout <<"UNEXPECTED MESSAGE ("<<message<<")\n";
                }
                
                break;
            }
            else 
            {
                if(*bytesReturned != '\r')
                    messageBuffer += *bytesReturned;
            }
            cout << "  messageBuffer: (" << messageBuffer << ") steppernumber:"<<stepperNum<<"\n";
        }
        
        // clear the message buffer
        memset(bytesReturned,0,NUM_BYTES);
    }
}
Beispiel #3
0
void Serial::read(ofSerial& serialPort, unsigned char * bytesReturned, string& messageBuffer, string& message){
    
    // if we've got new bytes
    if(serialPort.available() > 0){
        // we wil keep reading until nothing is left
        
//        cout << "reading"<<endl;
        while (serialPort.available() > 0){
            
//            cout << "port open"<<endl;
            
            // we'll put the incoming bytes into bytesReturned
            serialPort.readBytes(bytesReturned, NUM_BYTES);
            
            // if we find the splitter we put all the buffered messages 
            // in the final message, stop listening for more data and 
            // notify a possible listener
            // else we just keep filling the buffer with incoming bytes. 
            if(*bytesReturned == '\n'){
                
                message = messageBuffer;
                messageBuffer = "";
                
                messages.push_back(parseMessage(message));
                
                while ((messages[messages.size()-1].timestamp-messages[0].timestamp)>30000000){ //buffer 30 seconds
                    
                    if (messages.size()>0) messages.erase(messages.begin());
                    
                }
                
//                cout << "received message: "<<message<<endl;
                
//                cout << "messages in memory "<<messages.size()<<endl;
            
            }else{
                if(*bytesReturned != '\r') messageBuffer += *bytesReturned;
            }
//            cout << "  messageBuffer: (" << messageBuffer << ")\n";
        }
        
        // clear the message buffer
        memset(bytesReturned,0,NUM_BYTES);
    }
}
Beispiel #4
0
void Serial::print(ofSerial& serialPort, string line){
    
    unsigned char * charLine = new unsigned char[line.size()+1];
    charLine[line.size()]=0;
    memcpy(charLine,line.c_str(),line.size());
    
    serialPort.writeBytes(charLine,line.size());
    cout <<"output to serial: "<<charLine<<"\n";
    
}
Beispiel #5
0
bool ofxGetSerialString(ofSerial &serial, string &output_str, char until) {
    static string tmpstr; //cannot use output_str unless it's a member var of testApp. we want also support for local vars in functions. OR, we can try if this is really the case and needed
    stringstream ss;
    char ch;
    int ttl=1000;
    while ((ch=serial.readByte())>0 && ttl-->0 && ch!=until) {
        ss << ch;
    }
    tmpstr+=ss.str();
    if (ch==until) {
        output_str = tmpstr;
        tmpstr = "";
    }
    return tmpstr!="";
}
Beispiel #6
0
string ofxGetSerialString(ofSerial &serial, char until) {
    static string str;
    stringstream ss;
    char ch;
    int ttl=1000;
    while ((ch=serial.readByte())>0 && ttl-->0 && ch!=until) {
        ss << ch;
    }
    str+=ss.str();
    if (ch==until) {
        string tmp=str;
        str="";
        return ofxTrimString(tmp);
    } else {
        return "";
    }
}
Beispiel #7
0
void ofxSerialWriteLine(ofSerial &serial, string str) {
    str+="\n";
    serial.writeBytes((unsigned char*)str.c_str(), str.size());
}