uint8_t WaspGPRS::sendDataFTP(char* file, char* path, uint8_t id) { char command[100]; char aux='"'; long previous=0; uint8_t answer=0; uint8_t end=0; uint32_t i,j=0; sprintf(command,"AT%s%c,,%c%c,%c%s%c,0%c%c", AT_FTP_SEND, id, aux, aux, aux, file, aux, '\r', '\n'); printString(command,PORT_USED); previous=millis(); while( (!serialAvailable(PORT_USED)) && ((millis()-previous)<10000) ); delay(10); answer=waitForData("CONNECT",20,0,0); if(answer!=1) return 0; serialFlush(PORT_USED); Utils.strExplode(path,'/'); while( path[i]!='\0' ) { if( path[i]== '/' ) j++; i++; } i=0; SD.ON(); while( j>0 ) { if(!SD.cd(Utils.arguments[i])){ SD.OFF(); return 0; } i++; j--; } i=0; j=0; while( !end ) { printString(SD.cat(file,250*i,250),PORT_USED); while( SD.buffer[j]!='\0' ) j++; if( j<249 ) end=1; i++; j=0; } printString(GPRS_PATTERN,PORT_USED); SD.OFF(); previous=millis(); while( (!serialAvailable(PORT_USED)) && ((millis()-previous)<10000) ); delay(10); answer=waitForData("OK",20,0,0); if(answer!=1) return 0; return 1; }
/* manageIncomingData() - manage incoming data from serial port, executing proper functions to store received data * * This function manages incoming data from serial port, executing proper functions to store received data * * Returns '1' on success and '0' if error */ uint8_t WaspGPRS::manageIncomingGSMData() { char byteIN[100]; uint8_t a=0; long previous=0; uint8_t answer=0; while(a<100){ byteIN[a]=0; a++; } a=0; serialFlush(PORT_USED); previous=millis(); while(!serialAvailable(PORT_USED) && (millis()-previous)<20000); previous=millis(); while( (millis()-previous) < 2000 ) { while( serialAvailable(PORT_USED) && (millis()-previous) < 2000 ) { byteIN[a]=serialRead(PORT_USED); a++; } } answer=parse_GSM(byteIN); switch( answer ){ case 0: readCall(byteIN); break; case 1: readSMS(byteIN); break; } if(answer==2) return 0; return 1; }
static void radioProcessInstance(radioInstance_t *r) { serialPort_t *s = r->serialPort; int8_t q; switch (r->radioType) { case RADIO_TYPE_SPEKTRUM11: case RADIO_TYPE_SPEKTRUM10: case RADIO_TYPE_DELTANG: while (serialAvailable(s)) if (spektrumCharIn(r, serialRead(s))) { r->lastUpdate = timerMicros(); radioReceptionQuality(r, 1); } break; case RADIO_TYPE_SBUS: while (serialAvailable(s)) if ((q = futabaCharIn(r, serialRead(s)))) { r->lastUpdate = timerMicros(); radioReceptionQuality(r, q); } break; case RADIO_TYPE_PPM: if (ppmDataAvailable(r)) r->lastUpdate = timerMicros(); break; case RADIO_TYPE_SUMD: while (serialAvailable(s)) if ((q = grhottCharIn(r, serialRead(s)))) { r->lastUpdate = timerMicros(); radioReceptionQuality(r, q); } break; case RADIO_TYPE_MLINK: while (serialAvailable(s)) if ((q = mlinkrxCharIn(r, serialRead(s)))) { r->lastUpdate = timerMicros(); radioReceptionQuality(r, q); } break; } // no radio signal? if (timerMicros() - r->lastUpdate > RADIO_UPDATE_TIMEOUT) radioReceptionQuality(r, -1); // minimum signal quality (0%) if no updates within timeout value else if (r->radioType == RADIO_TYPE_PPM) radioReceptionQuality(r, ppmGetSignalQuality(r)); // signal quality based on PPM status }
int main() { serialOpen("/dev/tty.usbserial-A4001o0F", 9600); serialWriteByte(0x31); //serialport_write("1"); usleep(100 * 1000); printf("available: %d \n", serialAvailable()); printf("read: %c \n", serialRead()); printf("available: %d \n", serialAvailable()); printf("read: %c \n", serialRead()); printf("available: %d \n", serialAvailable()); serialClose(); return 0; }
/* getCellInfo() - gets the information from the cell where the module is connected * * This function gets the information from the cell where the module is connected * * It stores in 'RSSI' and 'cellID' variables the information from the cell * * It modifies 'flag' if expected answer is not received after sending a command to GPRS module * * Returns '1' on success and '0' if error */ uint8_t WaspGPRS::getCellInfo() { char command[30]; uint8_t byteIN[200]; long previous=millis(); uint8_t counter=0; uint8_t a,b,c=0; serialFlush(PORT_USED); sprintf(command,"AT%s\r\n",AT_GPRS_CELLID); printString(command,PORT_USED); while( (!serialAvailable(PORT_USED)) && ((millis()-previous)<3000) ); previous=millis(); a=0; while( (millis()-previous) < 2000 ) { while( serialAvailable(PORT_USED) && (millis()-previous) < 2000 && (a<200) ) { byteIN[a]=serialRead(PORT_USED); a++; } } a=0; while( counter < 5 ) { while( (byteIN[a]!=',') && (a<200) ) { a++; } a++; counter++; } if(a>=200) return 0; counter=0; while( (byteIN[a]!=',') && (a<200) ) { cellID[c]=byteIN[a]; a++; c++; } a++; while( (byteIN[a]!=',') && (a<200) ) { RSSI[b]=byteIN[a]; delay(10); b++; a++; } return 1; }
/* Function: Parses module answer trying to find specified MAC, stops inquiry when mac is found. if dewvice is found, saved on position 0 Returns: Returs 1 if found. 0 if not found. Parameters: inquiryTime: wait for module answer during this time. mac: mac of desired device. Values: */ bool WaspBT_Pro::waitScanDeviceAnswer(long inquiryTime, char* mac) { delay(100); char dummy[4]; bool found = false; char * block; block = (char*)calloc(BLOCK_SIZE+1,sizeof(char)); Utils.setLED(LED1, LED_ON); // Inquiry while led on long previous=millis(); while( (millis()-previous<inquiryTime) && (!found) ) { // Looks for keyword and save device. if (serialAvailable(1)) { dummy[0]=serialRead(1); delay(50); if (dummy[0]=='I'){ dummy[1]=serialRead(1); delay(50); if (dummy[1]=='A'){ dummy[2]=serialRead(1); if (dummy[2]=='L'){ while(serialAvailable(1)<BLOCK_SIZE); for(uint8_t x=0;x<BLOCK_SIZE;x++) block[x]= serialRead(1); if (lookForAnswer(block,mac)>=1) { found=true; parseBlock(block); sendCommand("IC"); numberOfDevices = 1; delay(200); } } } } } #ifdef DEBUG_MODE printBuffer(); #endif // Condition to avoid an overflow (DO NOT REMOVE) if( millis()-previous < 0 ) previous=millis(); } free(block); block=NULL; Utils.setLED(LED1, LED_OFF); // Inquiry while led on return found; }
//------------------------------------------------------------------------------ // An adaptation of the loop in main() in the original Babuino code. // This is intended to be called from within the Arduino loop() function. //------------------------------------------------------------------------------ void Babuino::loop() { debounce(); switch (_states.getMachineState()) { case READY: if (serialAvailable() > 0) { _states.setMachineState(COMM); } else if (_states.getRunRequest() == RUNNING) { _states.setMachineState(RUN); } break; case COMM: doComm(); _states.setMachineState(READY); break; case RUN: _regs.pc.set(_storage.getStartAddress()); initStack(_stack); _states.setMachineState(RUN); code_exec(); _motors.off(); _states.setMachineState(READY); break; } }
unsigned char stmWaitAck(serialStruct_t *s, int retries) { unsigned char c; unsigned int i; for (i = 0; i < retries; i++) { if (serialAvailable(s)) { c = serialRead(s); if (c == 0x79) { // putchar('+'); fflush(stdout); return 1; } if (c == 0x1f) { putchar('-'); fflush(stdout); return 0; } else { printf("?%x?", c); fflush(stdout); return 0; } } usleep(500); } return 0; }
byte WaspGPRS::sendCommand(char* theText, char* endOfCommand, char* expectedAnswer, int MAX_TIMEOUT, int sendOnce) { int timeout = 0; for (int i = 0; i < 100; i++) received[i] = ' '; int length=sprintf(theCommand, "%s%s", theText,endOfCommand); // try sending the command // wait for serial response timeout = 0; serialFlush(PORT_USED); while(!serialAvailable(PORT_USED) && timeout < MAX_TIMEOUT) { if (!sendOnce || !timeout) { printString(theCommand,PORT_USED); USB.print('T'); } delay(DELAY_ON_SEND); timeout++; }; int answer= waitForData( expectedAnswer, MAX_TIMEOUT, timeout, 0); return answer; }
/* * * name: readBuffer * This function reads from UART rx buffer and stores the contents in _buffer * attribute. _length indicates the number of valid bytes in _buffer * * @param bool clearBuffer: indicates if the buffer has to be cleared before reading * * @return number of bytes read */ uint16_t WaspUART::readBuffer(uint16_t requestBytes, bool clearBuffer) { // index counter uint16_t i = 0; uint16_t nBytes = 0; if( clearBuffer == true ) { // clear _buffer memset( _buffer, 0x00, sizeof(_buffer) ); _length = 0; } // get actual instant //unsigned long previous = millis(); // check available data for 'timeout' milliseconds while( serialAvailable(_uart) ) { if ( (i < (sizeof(_buffer)-1)) && (requestBytes>0) ) { _buffer[i++] = serialRead(_uart); _length++; nBytes++; requestBytes--; latencyDelay(); } else { break; } } return nBytes; }
void loop() { int bytes = serialAvailable(); while (bytes--) { char byte; int val; byte = serialRead(); val = byte; switch(byte) { case '0' ... '9': val = val - '0'; // convert from character to number val = val * (180/9); // convert from number to degrees pulseWidth[servoIdx] = (val * 9) + minPulse; // convert angle to microseconds Serial.print(">>> Moving servo #"); Serial.print(servoIdx); Serial.print(" to "); Serial.print(pulseWidth[servoIdx], DEC); Serial.println(""); break; default: Serial.println("Unknown Command"); break; } } /* Now we do the servo jazz */ servo_loop(); }
/* uploadFile() - uploads a file to a FTP server * * This function uploads a file to a FTP server * * Returns '1' on success and '0' if error */ uint8_t WaspGPRS::downloadFile(char* file, char* path, char* user, char* passw, char* ftp_server, uint8_t ftp_port) { char command[70]; long previous=0; uint8_t answer=0; char aux='"'; uint16_t index=0; uint16_t i=0; uint8_t end=0; char* aux2; uint8_t j=0; uint8_t id=0; if(!setFlowControl()) return 0; if(!configureGPRS()) return 0; // Connect to FTP Server sprintf(command,"AT%s,%c%s%c,%c%s%c,%c%s%c,%u,1%c%c",AT_FTP_PARAM,aux,ftp_server,aux,aux,user,aux,aux,passw,aux,ftp_port,'\r','\n'); printString(command,PORT_USED); previous=millis(); while( (!serialAvailable(PORT_USED)) && ((millis()-previous)<10000) ); delay(10); answer=waitForData("+KFTPCFG: ",20,0,0); if(answer!=1) return 0; id=serialRead(PORT_USED); if( !readDataFTP(file, path, id) ) return 0; return 1; }
uint8_t serialReadUntil(byte* buf, uint8_t maxlen, byte terminator) { uint8_t i; for(i=0;i<_buffer_length;i++) { if((_buffer[i] == terminator) || i == (maxlen-1)) { memcpy(buf,_buffer,i+1); shift(i+1); return i+1; } } /* while(serialLast() != terminator) ; //в идеале так, но приходится делать как ниже.. */ while(1) { if(serialLast() == terminator) break; _delay_ms(1); //какойто глюк с gcc, надо вызывать задержку, иначе гуано } uint8_t length = serialAvailable(); if(length > maxlen) length = maxlen; memcpy(buf,_buffer,length); shift(length); return length; }
// Test purposes only void WaspBT_Pro::printBuffer2(){ int flag; flag= serialAvailable(1); //USB.println(flag); if (flag>900) USB.println("_-*OVERFLOW in summary*-_"); }
uint8_t SerialProtocol::send() { if (!serialAvailable()) { // There is no place to send the data return ProtocolState::NO_SERIAL; } uint8_t checksum = payloadSize; // Write the header sendData(HEADER); sendData(payloadSize); // Write the payload for (int i = 0; i < payloadSize; i++) { checksum ^= *(payload + i); sendData(*(payload + i)); } // Complete with the checksum sendData(checksum); return ProtocolState::SUCCESS; }
/* sendData(data,socket) - sends 'data' to the specified 'socket' * * This function sends 'data' to the specified 'socket' * * It modifies 'flag' if expected answer is not received after sending a command to GPRS module * * It gets from 'socket_ID' the TCP session ID assigned to the last call of creating a socket * * Returns '1' on success and '0' if error */ uint8_t WaspGPRS::sendData(const char* data, uint8_t* socket) { char command[30]; uint8_t answer=0; long previous=0; uint8_t byteIN=0; uint8_t counter=0; uint8_t i=0; while(data[counter]!='\0') counter++; counter+=2; while(socket[i]!='\r') i++; counter+=i-1; serialFlush(PORT_USED); switch(i) { case 1: sprintf(command,"%s%c,%u%c%c",AT_GPRS_TCP_SND,socket[0],counter,'\r','\n'); break; case 2: sprintf(command,"%s%c%c,%u%c%c",AT_GPRS_TCP_SND,socket[0],socket[1],counter,'\r','\n'); break; case 3: sprintf(command,"%s%c%c%c,%u%c%c",AT_GPRS_TCP_SND,socket[0],socket[1],socket[2],counter,'\r','\n'); break; } printString(command,PORT_USED); previous=millis(); while( (!serialAvailable(PORT_USED)) && ((millis()-previous)<3000) ); delay(10); answer=waitForData("CONNECT",20,0,0); if(answer!=1) return 0; answer=0; counter=0; while( (counter<3) && (answer!=1) ) { serialFlush(PORT_USED); delay(20); writeData(data); previous=millis(); while( (!serialAvailable(PORT_USED)) && ((millis()-previous)<3000) ); delay(10); answer=waitForData("OK",20,0,0); counter++; } if(answer!=1) return 0; return 1; }
///////////////////////////// // // runBitlash // // This is the main entry point where the main loop gives Bitlash cycles // Call this frequently from loop() // void runBitlash(void) { // Pipe the serial input into the command handler if (serialAvailable()) doCharacter(serialRead()); // Background macro handler: feed it one call each time through runBackgroundTasks(); }
/* sendMail() - sends an email * * This function sends an email * * Returns '1' on success and '0' if error */ uint8_t WaspGPRS::sendMail(char* from, char* to, char* subject, char* body, char* user, char* passw, char* smtp_server, uint16_t port) { uint8_t counter=0; char command[30]; long previous=0; uint8_t answer=0; if(!setFlowControl()) return 0; if(!configureGPRS()) return 0; if(!setEmailParams(smtp_server, port, from)) return 0; if(!setEmailPwd(user, passw)) return 0; if(!setEmailDestination(to)) return 0; if(!setEmailSubject(subject)) return 0; while( body[counter]!='\0' ) counter++; counter+=2; serialFlush(PORT_USED); sprintf(command,"AT%s1,%u%c%c",AT_SMTP_SEND,counter,'\r','\n'); printString(command,PORT_USED); previous=millis(); while( (!serialAvailable(PORT_USED)) && ((millis()-previous)<10000) ); delay(10); answer=waitForData("CONNECT",20,0,0); if(answer!=1) return 0; printString(body,PORT_USED); printByte('\r',PORT_USED); printByte('\n',PORT_USED); previous=millis(); while( (!serialAvailable(PORT_USED)) && ((millis()-previous)<10000) ); delay(10); answer=waitForData("OK",20,0,0); if(answer!=1) return 0; return 1; }
/* Function: Reads and stores module answer. Returns: Parameters: Values: */ void WaspBT_Pro::readCommandAnswer() { for (i = 0; i < RX_BUFFER; i++) commandAnswer[i] = ' '; // Clears variable i=0; while(serialAvailable(1)) { commandAnswer[i]=serialRead(1); i++; delay(10); } }
uint8_t serialWaitUntil(byte terminator) { while(1) { if(serialLast() == terminator) return serialAvailable(); _delay_ms(1); //какойто глюк с gcc, надо вызывать задержку, иначе гуано } return 0; }
/* getIMSI() - gets the IMSI from the SIM card * * This function gets the IMSI from the SIM card. It stores the IMSI into 'IMSI' variable. * * Returns '1' on success and '0' if error */ uint8_t WaspGPRS::getIMSI() { char command[15]; uint8_t byteIN[20]; long previous=millis(); uint8_t a,b=0; serialFlush(PORT_USED); sprintf(command,"%s\r\n",AT_GPRS_IMSI); printString(command,PORT_USED); while( (!serialAvailable(PORT_USED)) && ((millis()-previous)<3000) ); previous=millis(); a=0; while( (millis()-previous) < 2000 ) { while( serialAvailable(PORT_USED) && (millis()-previous) < 2000 && (a<20) ) { byteIN[a]=serialRead(PORT_USED); a++; } } a=0; while( (byteIN[a]!='\r') && (byteIN[a]!='\n') && (a<20) ) { a++; } if(a>=20) return 0; a++; if(a>=20) return 0; b=0; while( (byteIN[a]!='\r') && (a<20) ) { IMSI[b]=byteIN[a]; a++; b++; } IMSI[b]='\0'; if(b<=10) return 0; return 1; }
/* * available() - checks if there is available data waiting to be read * */ uint8_t WaspUSB::available() { beginSerial(USB_RATE, _uart); // configure multiplexor to USB port digitalWrite(MUX_PW,HIGH); digitalWrite(MUX_USB_XBEE,LOW); return serialAvailable( _uart); }
void serialWatch(void) { #ifdef SERIAL_UART1_PORT if (serialPort1 && serialAvailable(serialPort1)) CoSetFlag(serialPort1->waitFlag); #endif #ifdef SERIAL_UART2_PORT if (serialPort2 && serialAvailable(serialPort2)) CoSetFlag(serialPort2->waitFlag); #endif #ifdef SERIAL_UART3_PORT if (serialPort3 && serialAvailable(serialPort3)) CoSetFlag(serialPort3->waitFlag); #endif #ifdef SERIAL_UART4_PORT if (serialPort4 && serialAvailable(serialPort4)) CoSetFlag(serialPort4->waitFlag); #endif #ifdef SERIAL_UART5_PORT if (serialPort5 && serialAvailable(serialPort5)) CoSetFlag(serialPort5->waitFlag); #endif #ifdef SERIAL_UART6_PORT if (serialPort6 && serialAvailable(serialPort6)) CoSetFlag(serialPort6->waitFlag); #endif }
void initlbuf(void) { lbufptr = lbuf; #if defined(SERIAL_OVERRIDE) && 0 // don't do the prompt in serialIsOverridden mode if (serialIsOverridden()) return; #endif prompt(); // flush any pending serial input while (serialAvailable()) serialRead(); }
/* sendCommand(ATcommand, ATcommand_R) - sends any command to GPRS module * * This function sends any command to GPRS module * * It stores in 'answer_command' variable the answer returned by the GPRS module * * Returns '1' on success and '0' if error */ uint8_t WaspGPRS::sendCommand(char* ATcommand) { char command[30]; uint8_t timeout=0; uint8_t i=0; answer_command[0]='\0'; sprintf(command, "AT%s%c%c", ATcommand,'\r','\n'); serialFlush(PORT_USED); USB.print('d'); while(!serialAvailable(PORT_USED)) { printString(command,PORT_USED); delay(DELAY_ON_SEND); USB.print('e'); } USB.print('a'); while( timeout < 5 ) { while(!serialAvailable(PORT_USED) && timeout < 5) { timeout++; delay(1000); } USB.print('b'); while(serialAvailable(PORT_USED) && timeout < 5){ answer_command[i] = serialRead(PORT_USED); delay(20); i++; timeout=0; if(i>=199) timeout=5; } USB.print('c'); } answer_command[i]='\0'; if( i<5 ) return 0; return 1; }
byte serialRead(byte* buf, uint8_t n) { uint8_t counter = 0; uint8_t flag = 0; while(counter < 250) { if(serialAvailable() >= n) { flag = 1; break; } counter++; _delay_ms(4); } if(!flag) return 0; memcpy(buf,_buffer,n); shift(n); return n; }
void loop(){ seq.rotation.update(getAnalogValue(SEQUENCER_ROTATE_CONTROL)); seq.step.update(getAnalogValue(SEQUENCER_STEP_CONTROL)); seq.fill.update(getAnalogValue(SEQUENCER_FILL_CONTROL)); seq.update(); #ifdef SERIAL_DEBUG if(serialAvailable() > 0){ serialRead(); printString("a: ["); seq.dump(); printString("] "); seq.print(); if(clockIsHigh()) printString(" clock high"); if(resetIsHigh()) printString(" reset high"); printNewline(); } #endif }
//Read one byte from serial unsigned char nextChar (unsigned char * byte ,GioEndpoint *gioEndpoint){ unsigned char get = 1; #ifdef ARDUINO unsigned char timeoutCounter = 0; while(1){ if(serialAvailable(gioEndpoint))break; if(timeoutCounter++>TIMEOUT_AFTER){ get = 0; break; } delay(1); } #endif if(get){ serialRead(byte,gioEndpoint); return get; }else{ return 0; } }
/* * * name: latencyDelay * This function waits for specific time depending on the baudrate used. * * Latency time for ensure there is no more data is calculated suposing the * worst condition: 1start+8data+1parity+2stop = 12 bits per byte * */ void WaspUART::latencyDelay() { // In the case there are not any bytes left in the incoming uart // wait for the time one byte needs for tranmission regarding the baudrate if (serialAvailable(_uart) == 0) { switch (_baudrate) { case 115200: delayMicroseconds(105); break; case 57600: delayMicroseconds(209); break; case 38400: delayMicroseconds(313); break; case 19200: delayMicroseconds(625); break; case 9600: delay(1); break; case 4800: delay(3); break; case 2400: delay(5); break; case 1800: delay(7); break; case 1200: delay(10); break; case 600: delay(20); break; case 300: delay(40); break; default: break; } } }
/* Function: Reads internal temperature sensor of bluetooth module. Returns: Temperature Parameters: Values: */ long WaspBT_Pro::getTemp(){ long a; char dummy[3]; char temperature[4]; for (uint8_t i = 0; i < COMMAND_SIZE; i++) theCommand[i] = ' '; // Clear variable for (uint8_t i = 0; i < 3; i++) temperature[i] = ' '; sendCommand("temp"); delay(100); while(serialAvailable(1)){ dummy[0]=serialRead(1); if (dummy[0]=='M'){ dummy[1]=serialRead(1); if (dummy[1]=='P'){ dummy[0]=serialRead(1); temperature[0]=serialRead(1); temperature[1]=serialRead(1); temperature[2]=serialRead(1); a = Utils.array2long(temperature); } } } return a; }