void lcdDisplaySensors(unsigned int fd,struct sGENERAL patient){ //while(GPIORead(changeDisplay)!=HIGH){ char Nokia_Temp[10],Nokia_BPM[10]; Sensors.BPM = (unsigned int)serialGetchar(fd); Sensors.Temp = ((float)serialGetchar(fd)*5/(1023))/0.01; Sensors.BPMState = healthState(patient,Sensors.BPM); Sensors.TempState = isNormal(Sensors.Temp); snprintf(Nokia_Temp,10,"%.1f*C",Sensors.Temp); snprintf(Nokia_BPM,10,"%dBPM",Sensors.BPM); LCDclear(); printf("Temp:%.1f\n",Sensors.Temp); printf("BPM:%d\n",Sensors.BPM); LCDdrawstring(20,0,"SENSORES"); LCDdrawstring(25,10,Nokia_Temp); LCDdrawstring(20,20,Sensors.TempState); LCDdrawstring(25,30,Nokia_BPM); LCDdrawstring(20,40,Sensors.BPMState); LCDdisplay(); //} }
void Controller::runUart() { int fd = serialOpen(UART_DEVICE, 9600); int bSize = 0; // ilość znaków w buforze while(!endThreads) { bSize = serialDataAvail(fd); if(bSize==8) { // dwa inty czekają na odczyt union IntOrByte { char b[4]; int i; } u; // odczyt kierunku wiatru for (int i =0; i < 4; i++) { //odczytaj 4 bajty u.b[i] = serialGetchar(fd); } this->windDirection = u.i; // odczyt prędkości wiatru u.i = 0; // resetujemy union for (int i =0; i < 4; i++) { //odczytaj 4 bajty u.b[i] = serialGetchar(fd); } this->windSpeed = u.i; } else if(bSize>8) { // zbyt dużo informacji w buforze serialFlush(fd); } chrono::milliseconds sleepDuration(500); this_thread::sleep_for(sleepDuration); } serialClose(fd); dlog << "koniec threadWorker"; }
void lcdDisplayMain(unsigned int display,unsigned int fd){ //while(GPIORead(changeDisplay)!=HIGH){ char Nokia_Temp[10],Nokia_BPM[10],INFO[15]={0,}; Sensors.BPM = (unsigned int)serialGetchar(fd); Sensors.Temp = ((float)serialGetchar(fd)*5/(1023)); snprintf(Nokia_Temp,10,"%.1f*C",Sensors.Temp); snprintf(Nokia_BPM,10,"%dBPM",Sensors.BPM); NOKIAClear(display); getClockInformation(&info); printf("Data:%s\n",info.date); printf("Hora:%s\n",info.time); printf("Temp:%.1f\n",Sensors.Temp); printf("BPM:%d\n",Sensors.BPM); NOKIAString(display,0,0,"PRINCIPAL"); NOKIAString(display,1,0,Nokia_Temp); NOKIAString(display,1,50,Nokia_BPM); NOKIAString(display,2,0,info.date); NOKIAString(display,2,50,info.time); // LCDdrawline(0, 35, 83, 35, BLACK); // informações // LCDdisplay(); //} }
static int myAnalogRead (struct wiringPiNodeStruct *node, int pin) { int vHi, vLo ; serialPutchar (node->fd, 'a') ; serialPutchar (node->fd, pin - node->pinBase) ; vHi = serialGetchar (node->fd) ; vLo = serialGetchar (node->fd) ; return (vHi << 8) | vLo ; }
int drcSetup (const int pinBase, const int numPins, const char *device) { int fd ; int ok, tries ; time_t then ; struct wiringPiNodeStruct *node ; if ((fd = serialOpen (device, 115200)) < 0) return wiringPiFailure (WPI_ALMOST, "Unable to open DRC device (%s): %s", device, strerror (errno)) ; delay (10) ; // May need longer if it's an Uno that reboots on the open... // Flush any pending input while (serialDataAvail (fd)) (void)serialGetchar (fd) ; ok = FALSE ; for (tries = 1 ; tries < 5 ; ++tries) { serialPutchar (fd, '@') ; then = time (NULL) + 2 ; while (time (NULL) < then) if (serialDataAvail (fd)) { if (serialGetchar (fd) == '@') { ok = TRUE ; break ; } } if (ok) break ; } if (!ok) { serialClose (fd) ; return wiringPiFailure (WPI_FATAL, "Unable to communidate with DRC device") ; } node = wiringPiNewNode (pinBase, numPins) ; node->fd = fd ; node->pinMode = myPinMode ; node->pullUpDnControl = myPullUpDnControl ; node->analogRead = myAnalogRead ; node->digitalRead = myDigitalRead ; node->digitalWrite = myDigitalWrite ; node->pwmWrite = myPwmWrite ; return 0 ; }
int main () { int fd,i=0; fd= serialOpen("/dev/ttyAMA0",115200); if(fd < 0) { printf("Opening serial failed.\n"); return 0; } while(i<10) { delay(500); serialPutchar(fd,i); i++; //printf("serialDataAvail: %d\n", serialDataAvail(fd)); if(serialDataAvail(fd) >= 1) { printf ("->%d\n", serialGetchar(fd)); fflush (stdout); } } printf("Serial port closing.\n"); serialClose(fd); return 0; }
void* serialComms(void* arg) { fdSerial = serialOpen("/dev/ttyAMA0",115200); if(fdSerial == -1) { std::cout <<"error opening serial port"<<std::endl; } else { int lastCh,bufLen; unsigned char buf[SERIALBUFLEN]; //struct termios options; // tcgetattr(fdSerial, &options); //options.c_cc[VTIME]=100;// 5 x 0.1 sec = 0.5 sec timeout //tcsetattr(fdSerial, TCSANOW, &options); while(!shutdown) { bufLen=0; do { lastCh=serialGetchar(fdSerial); if(lastCh>0) { buf[bufLen]=((unsigned char)lastCh) % 256; bufLen++; } } while(lastCh>0 && bufLen<SERIALBUFLEN && !shutdown); for(int i=0;i<bufLen;i++) { // process buffer std::cout << buf[i]<<std::flush; } if(lastCh<=0) { // timeout could indicate problem with mcu //std::cout<<"|timeout|"<<std::flush; } } serialClose(fdSerial); } return NULL; }
char* getstr(char* buff) { int i = 0; int j = 0; //serialFlush(fd); while(!serialDataAvail(fd) && i<1000) { i++; delay(10); } if (i== 1000) { printf("sem dados no serial\n"); buff = "erro"; return(buff); } while(serialDataAvail(fd)>0) { delay(10); buff[j] = serialGetchar(fd); j++; } buff[j] = 0; printf("%s \n", buff); //debug return(buff); }
int main () { printf("Program started.\n"); int ser_handle; // the serial connection (file descriptor) if ((ser_handle = serialOpen("/dev/ttyAMA0", BAUD_RATE)) < 0) { fprintf(stderr, "Unable to open serial device: %s\n", strerror(errno)); return 1 ; } else printf("Serial open\n"); int counter = 0; int avail_bytes; for(;;) { if(avail_bytes = serialDataAvail(ser_handle)) { serialPutchar(ser_handle,serialGetchar(ser_handle)); counter = counter + 1; if(counter%100 == 0) { printf("Byte %i has been passed on\n", counter); } } } }
int16 i16RPISerialGetChars(int fd) { int data = 0; data = serialGetchar(fd); return data; }
static ERL_NIF_TERM serial_get_char_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) { int handle; if (!enif_get_int(env, argv[0], &handle)) { return enif_make_badarg(env); } int c = serialGetchar(handle); return enif_make_int(env, c); }
uint8_t serial_readbyte(int serial_id) { struct serial_bus *sb = serial_buses[serial_id]; if(!sb) return -1; if(sb->fd < 0) { sb->fd = serialOpen(sb->bus, sb->speed); } return serialGetchar (sb->fd); }
int main(void) { int handle = serialOpen ("/dev/ttyAMA0", 9600); //Open the Serial port serialFlush(handle); //Clear the stream of old data packets while(1) { serialPutchar (handle, 's'); //Send 's' to the Arduberry char inp=serialGetchar(handle); //Receive the characters from the Arduberry printf("%c",inp); } return 0; }
//========thread start=========// //AA C0 IDH IDL STATE1 STATE2 STATE3 REV CRC AB void thread(void) { uint8_t ch = 0; uint8_t i = 0; uint8_t temp2=0; while (1) { ch = serialGetchar(usart_fd); switch (rx_step) { case START_STATE: if (ch == 0xAA) rx_step = AA_STATE; break; case AA_STATE: if (ch == 0xC0) rx_step = C0_STATE; break; case C0_STATE: if(i < 7) { rebuf[i++] = ch; } else { rx_step = START_STATE; i=0; if(ch == 0xAB) { temp2 = (rebuf[0]+rebuf[1]+rebuf[2]+rebuf[3]+rebuf[4]+rebuf[5]); //temp = temp2&0xff; if(rebuf[6] == (temp2&0xff))//01=pm25 23=pm10 { printf("%02X ",rebuf[0]); printf("%02X ",rebuf[1]); printf("%02X ",rebuf[2]); printf("%02X ",rebuf[3]); printf("%02X ",rebuf[4]); printf("%02X ",rebuf[5]); printf("%02X ",rebuf[6]); } } } break; default: break; } } }
/* * Return recieved string from serial line. Function returns number of characters recieved. ****************************************************************************** */ int serialReceive(char * response, int serialLine ){ int i = 0; delay(300); while ( serialDataAvail (serialLine) > 0 ) { response[i] = serialGetchar (serialLine) ; i++; } response[i] = 0; return i; }
void serialthread::run() { buffer.clear(); while(app_exit == false) { if ( device_id > 0 ) { //std::cout<< "read"<<std::endl; if ( serialDataAvail(device_id) != 0) { //std::cout<<"check"<<std::endl; countert1 = 0; int a = serialGetchar(device_id); char lc = (char)a; if (lc != '\n' ) { buffer.push_back(lc); } else { //process buffer process(); } } else { countert1++; if (countert1 > 50 && buffer.size() != 0) { countert1 = 0; //process buffer process(); } if ( countert1 > 55 ) { countert1 = 0; } } } msleep(1); } }
int main(void) { int handle = serialOpen("/dev/ttyAMA0", 9600); for (;;) { char c = serialGetchar(handle); printf("%c", c); } serialClose(handle); return 0; }
std::string RemoteDevice::getCompleteResponse() { LOG(""); std::string resp; bool isComplete = false; while (!isComplete) { resp += serialGetchar(_fd); LOG("temp_resp: %s", resp.c_str()); if (resp[resp.size() - 1] == ';') isComplete = true; } LOG("completeResponse: %s", resp.c_str()); return resp; }
int uartTxtoRxTest(int txfd, int rxfd, unsigned char tCh) { int flg; serialPutchar(txfd, tCh); delay(100); //delay 100ms if(tCh == serialGetchar(rxfd)) { flg = 1; } else { flg = 0; } return flg; }
bool SerialComm::ReadRAW() { // printf("%s\n", __FUNCTION__); bool bReturn = false; sBuffer bufferIn; //= &VariablesIn.Buffer; unsigned char CRC = 0; int j=0; while (serialDataAvail(fd)) { int ch = serialGetchar(fd); // printf("%d:%X ", j, ch); bufferIn.u8[j++] = (unsigned char) ch; //CRC ^= (unsigned char) ch; } // printf("\n"); for (int i=0; i<(j-1); i++) { CRC ^= bufferIn.u8[i]; } // printf("%s::Red %d bytes\n", __FUNCTION__, j); if ((bufferIn.u8[j-1]==CRC)&&(143==j)) { bufferIn.len = j; for (int i=0; i<j; i++) { VariablesIn.Buffer.u8[i] = bufferIn.u8[i]; } for (int i=0; i<7; i++) { // printf("%d:%d ", i, VariablesIn.Names.Digital[i]); } for (int i=0; i<54; i++) { // printf("%d:%d ", i, VariablesIn.Names.Analog[i]); } // printf("\n"); VariablesIn.Buffer.len = bufferIn.len; bReturn = true; // printf("%s::CRC %d\n", __FUNCTION__, CRC); printf("%s::Temp: %d\t%d\t%d\t%d\t%d\t%d\n", __FUNCTION__, VariablesIn.Names.Analog[41], VariablesIn.Names.Analog[42], VariablesIn.Names.Analog[43], VariablesIn.Names.Analog[44], VariablesIn.Names.Analog[45], VariablesIn.Names.Analog[46]); printf("%s::Analog: %d\t%d\t%d\t%d\t%d\t%d\n", __FUNCTION__, VariablesIn.Names.Analog[9], VariablesIn.Names.Analog[12], VariablesIn.Names.Analog[29], VariablesIn.Names.Analog[28], VariablesIn.Names.Analog[34], VariablesIn.Names.Analog[37]); } return bReturn; }
void loop(){ // Pong every 3 seconds if(millis()-time>=3000){ serialPuts (fd, "Pong!\n"); // you can also write data from 0-255 // 65 is in ASCII 'A' serialPutchar (fd, 65); time=millis(); } // read signal if(serialDataAvail (fd)){ char newChar = serialGetchar (fd); printf("%c", newChar); fflush(stdout); } }
int main () { int fd ; int count ; unsigned int nextTime ; if ((fd = serialOpen ("/dev/ttyAMA0", 115200)) < 0) { fprintf (stderr, "Unable to open serial device: %s\n", strerror (errno)) ; return 1 ; } if (wiringPiSetup () == -1) { fprintf (stdout, "Unable to start wiringPi: %s\n", strerror (errno)) ; return 1 ; } nextTime = millis () + 300 ; for (count = 0 ; count < 256 ; ) { if (millis () > nextTime) { printf ("\nOut: %3d: ", count) ; fflush (stdout) ; serialPutchar (fd, count) ; nextTime += 300 ; ++count ; } delay (3) ; while (serialDataAvail (fd)) { printf (" -> %3d", serialGetchar (fd)) ; fflush (stdout) ; } } printf ("\n") ; return 0 ; }
int start_capturing() { serialPutchar(fd,0x56); serialPutchar(fd,0x00); serialPutchar(fd,0x36); serialPutchar(fd,0x01); serialPutchar(fd,0x00); int i = 0; char temp[5]; char return_val[] = {0x76,0x0,0x36,0x0,0x0}; while(i<5) { temp[i] = serialGetchar(fd); if(temp[i] != return_val[i]) return -1; i++; } return 1; }
std::string CV7::refreshData() { const int NON_BREAKING_SPACE = 255; const int BUFF_SIZE = 256; char buffer[BUFF_SIZE]; int index = 0; while(index < BUFF_SIZE) { buffer[index] = serialGetchar(m_fd); fflush(stdout); if(NON_BREAKING_SPACE == ((int)buffer[index])) { std::stringstream text; text << "CV7::refreshData: Serial read timeout"; throw text.str().c_str(); } index++; } return buffer; }
int serial_readbytes(int serial_id, uint8_t *buf, int length) { struct serial_bus *sb = serial_buses[serial_id]; int i; uint8_t c; if(!sb) return -1; if(sb->fd < 0) { sb->fd = serialOpen(sb->bus, sb->speed); } for (i=0; i<length; i++) { c = serialGetchar(sb->fd); if(c != -1) buf[i] = c; else return i; } return length; }
char *readRFID(){ int data; int i; int number; char *rfid_number = (char *) malloc(25 * sizeof(char)); char read = '0'; init(); while(read == '0'){ number = 0; data=serialDataAvail(handle); if(data > 0){ for(i=1;i<=data;i++){ number += snprintf(rfid_number+number,15-number,"%03d",serialGetchar(handle)); } read = '1'; } } printf("%s\n\r",rfid_number); return rfid_number; }
int initUart() { int i = 0; fd = serialOpen("/dev/ttyACM0",9600); if(fd < 0){return 1;} delay(1000); serialPutchar(fd,'t'); delay(1000); while(extension == " "){ while(serialDataAvail(fd) >= 1) { extensionID[i] = serialGetchar(fd); if(i == 10){ break; } i++; } extension = extensionID; } for(i=0;i<10;i++){printf("%c",extensionID[i]);} printf("\n%s \n",extension); return 0; }
int setup_ctl_ctl(const char *table){ // Pong every 3 seconds if(millis()-t >= 5000){ // you can also write data from 0-255 command_ctl("rd!"); //command("vo!"); // command("vc!"); t=millis(); } // read signal /*if(serialDataAvail(fd)){ char newChar = serialGetchar(fd); printf("%c", newChar); fflush(stdout); }*/ if(serialDataAvail(fd)){ char c = serialGetchar(fd); //printf("XX: %c\n", c); if(c == '#') { str_to_struct(str, table); i = 1; count_raute++; pthread_cond_signal(¬ready); pthread_mutex_unlock(&lock); }else{ str[i] = c; i++; } fflush(stdout); } return count_raute; }
int main () { int fd ; int b = 0; int a = 0; int i = 0; char mac[] = "78A5048C47AF"; //green module mac char check[20] = "\0"; if ((fd = serialOpen ("/dev/ttyAMA0", 9600)) < 0) { fprintf (stderr, "Unable to open serial device: %s\n", strerror (errno)) ; return 1 ; } fflush(stdout); serialPuts (fd,"AT+MODE1"); //serve para acordar o modulo delay(500); serialFlush (fd) ; printf("go\n"); serialPuts (fd,"AT+CON"); serialPuts (fd,mac); while(serialDataAvail(fd)==0); //espera a chegada de algum byte na porta serial //o bloco abaixo recebe as informacoes do buffer e copia para a string check i=0; while((serialDataAvail(fd))!=0) { delay(10); check[i] = serialGetchar(fd); i++; } check[i] = 0; //fim do bloco que copia os dados para a string check printf("%s \n", check); //debug if (strcmp(check,"OK+CONNA") == 0) //se verificado que o módulo entendeu o comando, limpa a string e prossegue { check[0] = 0; //se o ble entendeu o comando, limpa a string e prossegue printf("comando recebido\n"); //debug } else { if (strcmp(check,"OK+CONNAOK+CONN") == 0) { check[0] = 0; printf("conectou!\n"); //para o caso da conexão ser estabelecida entre uma tentativa e outra } else return printf("erro!\n"); //se nao, retornar erro desconhecido } while(serialDataAvail(fd)==0); //espera a segunda parte da mensagem //o bloco abaixo recebe as informacoes do buffer e copia para a string check i=0; while((serialDataAvail(fd))!=0) { delay(10); check[i] = serialGetchar(fd); i++; } check[i] = 0; //fim do bloco que copia os dados para a string check if ((strcmp(check,"OK+CONN") == 0)) //verifica se a conexao foi efetuada { check[0] = 0; printf("conectou!\n"); } else { check[0] = 0; printf("conexao falhou!\n"); } }
int main (){ int fd,i,Flag,FirstBuf,t; int Num_Avail; char buffer[80]; unsigned int TimeNow,TimeStart; unsigned char counter; int all_count=0; unsigned char ucStra[6],ucStrw[6],ucStrAngle[6]; float Value[3]; if ((fd = serialOpen ("/dev/ttyAMA0", 115200)) < 0) { fprintf (stderr, "Unable to open serial device: %s\n", strerror (errno)) ; return 1 ; } softPwmCreate(7,0,20); TimeStart = millis(); for(;;) { if (Flag == 0) { Flag = 1; while(1) { FirstBuf = serialGetchar(fd); if(FirstBuf == 0x55) { for(t = 0;t < 10;t++) { FirstBuf = serialGetchar(fd); } break; } } } serialFlush(fd); delay(10); Num_Avail = serialDataAvail (fd); read(fd,buffer,Num_Avail); for(counter;counter < strlen(buffer) - 7;counter++) { if(buffer[counter]==0x55) { switch(buffer [counter + 1]) { case 0x51: ucStra[0]=buffer[counter + 2]; ucStra[1]=buffer[counter + 3]; ucStra[2]=buffer[counter + 4]; ucStra[3]=buffer[counter + 5]; ucStra[4]=buffer[counter + 6]; ucStra[5]=buffer[counter + 7]; break; case 0x52: ucStrw[0]=buffer[counter + 2]; ucStrw[1]=buffer[counter + 3]; ucStrw[2]=buffer[counter + 4]; ucStrw[3]=buffer[counter + 5]; ucStrw[4]=buffer[counter + 6]; ucStrw[5]=buffer[counter + 7]; break; case 0x53: ucStrAngle[0]=buffer[counter + 2]; ucStrAngle[1]=buffer[counter + 3]; ucStrAngle[2]=buffer[counter + 4]; ucStrAngle[3]=buffer[counter + 5]; ucStrAngle[4]=buffer[counter + 6]; ucStrAngle[5]=buffer[counter + 7]; TimeNow = millis(); Value[0] = ((short)(ucStra[1]<<8| ucStra[0]))/32768.0*16; Value[1] = ((short)(ucStra[3]<<8| ucStra[2]))/32768.0*16; Value[2] = ((short)(ucStra[5]<<8| ucStra[4]))/32768.0*16; system("clear"); printf("Num_Avail; %d",Num_Avail); printf("a:%.3f %.3f %.3f ",Value[0],Value[1],Value[2]); Value[0] = ((short)(ucStrw[1]<<8| ucStrw[0]))/32768.0*2000; Value[1] = ((short)(ucStrw[3]<<8| ucStrw[2]))/32768.0*2000; Value[2] = ((short)(ucStrw[5]<<8| ucStrw[4]))/32768.0*2000; printf("w:%.3f %.3f %.3f \n",Value[0],Value[1],Value[2]); Value[0] = ((short)(ucStrAngle[1]<<8| ucStrAngle[0]))/32768.0*180; Value[1] = ((short)(ucStrAngle[3]<<8| ucStrAngle[2]))/32768.0*180; Value[2] = ((short)(ucStrAngle[5]<<8| ucStrAngle[4]))/32768.0*180; printf("A:%.2f %.2f %.2f\r\n",Value[0],Value[1],Value[2]); all_count++; printf("count: %d time: %d\n",all_count,TimeNow - TimeStart); break; } } } } }