/* * Class: com_pi4j_wiringpi_Serial * Method: serialClose * Signature: (I)V */ JNIEXPORT void JNICALL Java_com_pi4j_wiringpi_Serial_serialClose (JNIEnv *env, jobject obj, jint fd) { serialClose(fd); }
void * SerialFunction(void *argument) { #ifdef LogMode Log(s, __func__, __LINE__, "Serial Function entry\n"); #endif int fd; char temp_output[RS232_Length]; int string_count = 0; int getString = 0; int readyToStart = 0; if((fd = serialOpen("/dev/ttyAMA0", 19200)) < 0) { printf("Unable to open serial device: %s\n", strerror(errno)); pthread_exit((void*)"fail"); } while(SerialThreadFlag) { while(serialDataAvail(fd)) { char temp_char_1; temp_char_1 = serialGetchar(fd); //printf("%x ", temp_char_1); //if(temp_char_1 == 0x7D) printf("\n"); if (temp_char_1 == 0x7D && getString == 0 && readyToStart == 1) { //count plus one and memset Count[Total]++; memset(temp_output, 0, sizeof(char)*RS232_Length); string_count = 0; } else if (temp_char_1 == 0x7B) { //default memset(temp_output,0, sizeof(char)*RS232_Length); getString = 1; Count[Total]++; temp_output[string_count] = temp_char_1; string_count++; readyToStart = 1; } else if(temp_char_1 == 0x7D && getString == 1) { //package if(string_count == 48) { pthread_mutex_lock(&mutex_2); memset(output, 0, sizeof(char)*RS232_Length); /*int vers_test_count = 0; for(vers_test_count = 0; vers_test_count < string_count; vers_test_count++) { output[vers_test_count] = temp_output[vers_test_count]; }*/ memcpy(output, temp_output, string_count); updateFlag = 1; pthread_mutex_unlock(&mutex_2); } memset(temp_output, 0, sizeof(char)*RS232_Length); string_count = 0; getString = 0; } else if( getString == 1 && string_count < 48) { temp_output[string_count] = temp_char_1; string_count++; } else; fflush (stdout) ; } } if(fd >= 0) { serialClose(fd); } #ifdef LogMode Log(s, __func__, __LINE__, "Serial Function exit\n"); #endif }
void GPIO_end() { serialClose(LCD); free(LCD_buff1); free(LCD_buff2); }
SerialComm::~SerialComm() { serialClose(fd); }
void * ZHSerialFunction(void *argument) { int fd; char tempOutput[RS232LENGTH]; int frontCount = 0; int backCount = 0; int stringCount = 0; memset(tempOutput, 0, sizeof(char)*RS232LENGTH); if ((fd = serialOpen ("/dev/ttyAMA0", 9600)) < 0) { printf ("Unable to open serial device: %s\n", strerror (errno)) ; pthread_exit((void*)"fail"); } while(SerialFunctionFlag) { while(serialDataAvail(fd)) { char tempChar = serialGetchar(fd); if(frontCount < 4) { if(tempChar == 0xdd) { frontCount++; }else { frontCount = 0; } }else if(backCount == 3 && tempChar == 0xee && stringCount < RS232LENGTH) { tempOutput[stringCount] = tempChar; stringCount++; int forCount; pthread_mutex_lock(&MutexSerial); memset(FileOutput, 0, sizeof(char)*RS232LENGTH); for(forCount = 0; forCount < RS232LENGTH; ++forCount) { FileOutput[forCount] = tempOutput[forCount]; } memset(tempOutput, 0, sizeof(char)*RS232LENGTH); frontCount = backCount = 0; stringCount = 0; UpdateFlag = 1; pthread_mutex_unlock(&MutexSerial); }else if(stringCount < RS232LENGTH) { if(tempChar == 0xee) { backCount++; tempOutput[stringCount] = tempChar; stringCount++; }else { backCount = 0; tempOutput[stringCount] = tempChar; stringCount++; } }else { printf("array overflow\n"); memset(tempOutput, 0, sizeof(char)*RS232LENGTH); frontCount = backCount = stringCount = 0; } fflush (stdout) ; //if (SerialThreadFlag == 0) break; } } if(fd >= 0) { serialClose(fd); } printf("serial function exit\n"); }
int main(int argc, char *argv[]) { FILE *fp; char c; time_t start = time(NULL), end; if ((argc < 3) || (argc > 4)) { printf("Usage:\n%s /dev/port /path/to.hex [q]\n", argv[0]); return 1; } // Open HEX File if ((fp = fopen(argv[2], "r")) == NULL) { printf("Could not open file %s\n", argv[2]); return 1; } // Read it's contents if (readHex(fp) != 0) { printf("Could not parse HEX file %s\n", argv[2]); fclose(fp); return 1; } fclose(fp); if (!isValid()) { printf("HEX-File not valid!\n"); freeHex(); return 1; } uint32_t min = minAddress(); uint32_t max = maxAddress(); uint32_t length = dataLength(); printf("Hex File Path : %s\n", argv[2]); printf("Minimum Address : 0x%X\n", min); printf("Maximum Address : 0x%X\n", max); printf("Data payload : %i bytes\n\n", length); uint8_t *d = (uint8_t *)malloc(length * sizeof(uint8_t)); if (d == NULL) { fprintf(stderr, "Not enough memory (%lu bytes)!\n", length * sizeof(uint8_t)); freeHex(); return 1; } parseData(d); freeHex(); // Open serial port if ((fd = serialOpen(argv[1], 38400, 1)) == -1) { printf("Could not open port %s\n", argv[1]); free(d); return 1; } signal(SIGINT, intHandler); signal(SIGQUIT, intHandler); ping: printf("Pinging bootloader... Stop with CTRL+C\n"); if (argc > 3) { c = argv[3][0]; } else { c = 'f'; } serialWriteChar(fd, c); usleep(PINGDELAY * 1000); if ((serialReadRaw(fd, &c, 1) != 1) || (c != OKAY)) { goto ping; } printf("Got response... Acknowledging...\n"); serialWriteChar(fd, CONFIRM); serialReadChar(fd, &c); if (c != ACK) { printf("Invalid acknowledge! Trying again...\n", c, c); goto ping; } printf("Connection established successfully!\n"); printf("Sending target address...\n"); serialWriteChar(fd, (min & 0xFF000000) >> 24); serialWriteChar(fd, (min & 0x00FF0000) >> 16); serialWriteChar(fd, (min & 0x0000FF00) >> 8); serialWriteChar(fd, min & 0x000000FF); serialReadChar(fd, &c); if (c != OKAY) { printf("Invalid acknowledge from YASAB!\n", c, c); free(d); serialClose(fd); return 1; } printf("Sending data length...\n"); serialWriteChar(fd, (length & 0xFF000000) >> 24); serialWriteChar(fd, (length & 0x00FF0000) >> 16); serialWriteChar(fd, (length & 0x0000FF00) >> 8); serialWriteChar(fd, length & 0x000000FF); serialReadChar(fd, &c); if (c != OKAY) { printf("Invalid acknowledge from YASAB (%x)!\n", c, c); free(d); serialClose(fd); return 1; } printf("\n\n"); for (uint32_t i = 0; i < length; i++) { serialWriteChar(fd, d[i]); if (serialHasChar(fd)) { serialReadChar(fd, &c); if (c == OKAY) { printNextPage(); } else if (c == ERROR) { printf("YASAB aborted the connection!\n"); free(d); serialClose(fd); return 1; } else { printf("Unknown answer from YASAB (%x)!\n", c, c); } } printProgress(i + 1, length); } end = time(NULL); printf("\n\nUpload finished after %3.1f seconds.\n", difftime(end, start)); printf("YASAB - Yet another simple AVR Bootloader\n"); printf("By xythobuz - Visit www.xythobuz.org\n"); free(d); serialClose(fd); return 0; }
void * ZHSerialFunction(void *argument) { int fd; int count1 , count2; struct termios options; struct timeval now; struct timespec outtime; short errorCheckCount[EVENTSIZE]; int watchdogCooldown = WATCHDOGVALUE; memset(errorCheckCount, 0, sizeof(short)*EVENTSIZE); count1 = count2 = 0; if ((fd = serialOpen ("/dev/ttyAMA0", 9600)) < 0) { printf ("Unable to open serial device: %s\n", strerror (errno)) ; pthread_exit((void*)"fail"); } tcgetattr(fd, &options); options.c_cflag |= PARENB; options.c_cflag &= ~PARODD; options.c_cflag |= CSTOPB; //options.c_cflag |= PARENB; //options.c_cflag &= ~PARODD; options.c_cflag &= ~CSIZE; options.c_cflag |= CS7; tcsetattr(fd, TCSANOW, &options); //tcsetattr(fd, &options); while(SerialFunctionFlag) { char arraySender[RS232LengthSender]; int forCount = 0; int charCount = 0; unsigned char arrayReceiver[RS232LengthReceiver]; unsigned char prefixCheck[8]; short flagPack = 0; memset(Count, 0, sizeof(long)*EVENTSIZE); //3 memset(arraySender, 0, sizeof(char)*RS232LengthSender); memset(arrayReceiver, 0 , sizeof(unsigned char)*RS232LengthReceiver); memset(prefixCheck, 0, sizeof(unsigned char)*8); //strcpy(arraySender, "@11RD5420001054*"); strcpy(arraySender, "@11RD542100085C*"); arraySender[16] = 0x0d; arraySender[17] = 0x0a; charCount = 0; count1 = 0; flagPack = 0; for(forCount = 0; forCount < 18; ++forCount) { serialPutchar(fd, arraySender[forCount]); } usleep(100000); //sleep(1); printf("\n3\n"); while(serialDataAvail(fd)) { char tempChar1; tempChar1 = serialGetchar(fd); printf("%x ", tempChar1); if(flagPack == 1 && tempChar1 == '*') { int localCharCount = 0; forCount = 0; printf("\nlength: %d\n", charCount); for(localCharCount = 0; localCharCount <= charCount-3 ; localCharCount = localCharCount + 4) { Count[forCount+6] = TransferFormatLongFunction(arrayReceiver[localCharCount]) * 4096 + TransferFormatLongFunction(arrayReceiver[localCharCount+1]) * 256 + TransferFormatLongFunction(arrayReceiver[localCharCount+2]) * 16 + TransferFormatLongFunction(arrayReceiver[localCharCount+3]); forCount++; } flagPack = 0; }else if(flagPack == 1 && tempChar1 != '*') { arrayReceiver[charCount] = tempChar1; ++charCount; //printf("%c ", tempChar1); }else if(count1 == 7 && strncmp(prefixCheck, "@11RD00",7) == 0) { flagPack = 1; arrayReceiver[charCount] = tempChar1; ++charCount; printf("check\n"); }else if(count1 != 0 && count1 < 7) { prefixCheck[count1] = tempChar1; count1++; printf("%d\n", count1); }else if(tempChar1 == 0x40) { count1 = 1; prefixCheck[0] = tempChar1; printf("check\n"); }else { memset(prefixCheck, 0, sizeof(unsigned char)*8); memset(arrayReceiver, 0, sizeof(unsigned char)*RS232LengthReceiver); count1 = 0; charCount = 0; flagPack = 0; printf("clean\n"); } fflush(stdout); } usleep(100000); //sleep(1); printf("\n3 size: %d\n", charCount); //send to Server //1 memset(arraySender, 0 , sizeof(char)*RS232LengthSender); memset(arrayReceiver, 0 , sizeof(unsigned char)*RS232LengthReceiver); memset(prefixCheck, 0, sizeof(unsigned char)*8); strcpy(arraySender, "@11RD4900000853*"); arraySender[16] = 0x0d; arraySender[17] = 0x0a; charCount = 0; count1 = 0; flagPack = 0; for(forCount = 0; forCount < 18; ++forCount) { serialPutchar(fd, arraySender[forCount]); } //usleep(100000); //nanosleep((const struct timespec[]){{0, 50000000L}}, NULL); sleep(1); printf("\n1\n"); while(serialDataAvail(fd)) { char tempChar1; tempChar1 = serialGetchar(fd); printf("%x ", tempChar1); if(flagPack == 1 && tempChar1 == '*') { int localCharCount = 0; forCount = 0; for(localCharCount = 0; localCharCount <= charCount-3 ; localCharCount = localCharCount + 8) { Count[forCount] = TransferFormatLongFunction(arrayReceiver[localCharCount+4]) * 268435456 + TransferFormatLongFunction(arrayReceiver[localCharCount+5]) * 16777216 + TransferFormatLongFunction(arrayReceiver[localCharCount+6]) * 1048576 + TransferFormatLongFunction(arrayReceiver[localCharCount+7]) * 65536 + TransferFormatLongFunction(arrayReceiver[localCharCount]) * 4096 + TransferFormatLongFunction(arrayReceiver[localCharCount+1]) * 256 + TransferFormatLongFunction(arrayReceiver[localCharCount+2]) * 16 + TransferFormatLongFunction(arrayReceiver[localCharCount+3]); forCount++; //printf("\nans %ld %ld %ld %ld\n",(long)arrayReceiver[localCharCount], (long)arrayReceiver[localCharCount+1], // (long)arrayReceiver[localCharCount+2] , (long)arrayReceiver[localCharCount+3]); } flagPack = 0; }else if(flagPack == 1 && tempChar1 != '*') { arrayReceiver[charCount] = tempChar1; ++charCount; }else if(count1 == 7 && strncmp(prefixCheck, "@11RD00", 7) == 0) { flagPack = 1; arrayReceiver[charCount] = tempChar1; ++charCount; }else if(count1 != 0 && count1 < 7) { prefixCheck[count1] = tempChar1; count1++; }else if(tempChar1 == 0x40) { count1 = 1; prefixCheck[0] = tempChar1; printf("check\n"); }else { memset(prefixCheck, 0, sizeof(unsigned char)*8); memset(arrayReceiver, 0, sizeof(unsigned char)*RS232LengthReceiver); count1 = 0; charCount = 0; flagPack = 0; printf("clean\n"); } fflush(stdout); } //usleep(200000); printf(" 1111 size:%d\n",charCount); //nanosleep((const struct timespec[]){{0, 50000000L}}, NULL); sleep(1); //2 memset(arraySender, 0, sizeof(char)*RS232LengthSender); memset(arrayReceiver, 0 , sizeof(unsigned char)*RS232LengthReceiver); memset(prefixCheck, 0, sizeof(unsigned char)*8); strcpy(arraySender, "@11RD5410000254*"); arraySender[16] = 0x0d; arraySender[17] = 0x0a; charCount = 0; count1 = 0; flagPack = 0; for(forCount = 0; forCount < 18; ++forCount) { serialPutchar(fd, arraySender[forCount]); } //usleep(100000); //nanosleep((const struct timespec[]){{0, 50000000L}}, NULL); sleep(1); while(serialDataAvail(fd)) { char tempChar1; tempChar1 = serialGetchar(fd); printf("%x ", tempChar1); if(flagPack == 1 && tempChar1 == '*') { Count[4] = TransferFormatLongFunction(arrayReceiver[4]) * 268435456 + TransferFormatLongFunction(arrayReceiver[5]) * 16777216 + TransferFormatLongFunction(arrayReceiver[6]) * 1048576 + TransferFormatLongFunction(arrayReceiver[7]) * 65536 + TransferFormatLongFunction(arrayReceiver[0]) * 4096 + TransferFormatLongFunction(arrayReceiver[1]) * 256 + TransferFormatLongFunction(arrayReceiver[2]) * 16 + TransferFormatLongFunction(arrayReceiver[3]); //printf("%c %c %c %c\n", arrayReceiver[0], arrayReceiver[1], arrayReceiver[2], arrayReceiver[3]); flagPack = 0; } else if(flagPack == 1 && tempChar1 != '*') { arrayReceiver[charCount] = tempChar1; ++charCount; //printf("%c ", tempChar1); }else if(count1 == 7 && strncmp(prefixCheck, "@11RD00", 7) == 0) { flagPack = 1; arrayReceiver[charCount] = tempChar1; ++charCount; }else if(count1 != 0 && count1 < 7) { prefixCheck[count1] = tempChar1; count1++; }else if(tempChar1 == 0x40) { count1 = 1; prefixCheck[0] = tempChar1; }else { memset(prefixCheck, 0, sizeof(unsigned char)*8); memset(arrayReceiver, 0, sizeof(unsigned char)*RS232LengthReceiver); count1 = 0; charCount = 0; flagPack = 0; } fflush(stdout); } printf(" 2222 size:%d\n",charCount); //nanosleep((const struct timespec[]){{0, 200000000L}}, NULL); sleep(1); //printf("\n\n"); //4 memset(arraySender, 0, sizeof(char)*RS232LengthSender); memset(arrayReceiver, 0 , sizeof(unsigned char)*RS232LengthReceiver); memset(prefixCheck, 0, sizeof(unsigned char)*8); //strcpy(arraySender, "@11RD6200002959*"); strcpy(arraySender, "@11RD6200001053*"); arraySender[16] = 0x0d; arraySender[17] = 0x0a; charCount = 0; count1 = 0; flagPack = 0; for(forCount = 0; forCount < 18; ++forCount) { serialPutchar(fd, arraySender[forCount]); } usleep(100000); //sleep(1); while(serialDataAvail(fd)) { char tempChar1; tempChar1 = serialGetchar(fd); //printf("%c ", tempChar1); if(flagPack == 1 && tempChar1 == '*') { int localCharCount = 0; forCount = 0; for(localCharCount = 0; localCharCount <= charCount-3 ; localCharCount = localCharCount + 4) { Count[forCount+15] = TransferFormatLongFunction(arrayReceiver[localCharCount]) * 4096 + TransferFormatLongFunction(arrayReceiver[localCharCount+1]) * 256 + TransferFormatLongFunction(arrayReceiver[localCharCount+2]) * 16 + TransferFormatLongFunction(arrayReceiver[localCharCount+3]); forCount++; } flagPack = 0; } else if(flagPack == 1 && tempChar1 != '*') { arrayReceiver[charCount] = tempChar1; ++charCount; //printf("%c ", tempChar1); }else if(count1 == 7 && strncmp(prefixCheck, "@11RD00", 7) == 0) { flagPack = 1; arrayReceiver[charCount] = tempChar1; ++charCount; }else if(count1 != 0 && count1 < 7) { prefixCheck[count1] = tempChar1; count1++; }else if(tempChar1 == 0x40) { count1 = 1; prefixCheck[0] = tempChar1; }else { memset(prefixCheck, 0, sizeof(unsigned char)*8); memset(arrayReceiver, 0, sizeof(unsigned char)*RS232LengthReceiver); count1 = 0; charCount = 0; } fflush(stdout); } //printf("\n"); usleep(100000); //sleep(1); //nanosleep((const struct timespec[]){{0, 100000000L}}, NULL); //5 memset(arraySender, 0, sizeof(char)*RS232LengthSender); memset(arrayReceiver, 0 , sizeof(unsigned char)*RS232LengthReceiver); memset(prefixCheck, 0, sizeof(unsigned char)*8); //strcpy(arraySender, "@11RD6200002959*"); strcpy(arraySender, "@11RD6210001052*"); arraySender[16] = 0x0d; arraySender[17] = 0x0a; charCount = 0; count1 = 0; flagPack = 0; for(forCount = 0; forCount < 18; ++forCount) { serialPutchar(fd, arraySender[forCount]); } usleep(100000); //nanosleep((const struct timespec[]){{0, 100000000L}}, NULL); //sleep(1); while(serialDataAvail(fd)) { char tempChar1; tempChar1 = serialGetchar(fd); //printf("%c ", tempChar1); if(flagPack == 1 && tempChar1 == '*') { int localCharCount = 0; forCount = 0; for(localCharCount = 0; localCharCount <= charCount-3 ; localCharCount = localCharCount + 4) { Count[forCount+25] = TransferFormatLongFunction(arrayReceiver[localCharCount]) * 4096 + TransferFormatLongFunction(arrayReceiver[localCharCount+1]) * 256 + TransferFormatLongFunction(arrayReceiver[localCharCount+2]) * 16 + TransferFormatLongFunction(arrayReceiver[localCharCount+3]); forCount++; } flagPack = 0; } else if(flagPack == 1 && tempChar1 != '*') { arrayReceiver[charCount] = tempChar1; ++charCount; //printf("%c ", tempChar1); }else if(count1 == 7 && strncmp(prefixCheck, "@11RD00", 7) == 0) { flagPack = 1; arrayReceiver[charCount] = tempChar1; ++charCount; }else if(count1 != 0 && count1 < 7) { prefixCheck[count1] = tempChar1; count1++; }else if(tempChar1 == 0x40) { count1 = 1; prefixCheck[0] = tempChar1; }else { memset(prefixCheck, 0, sizeof(unsigned char)*8); memset(arrayReceiver, 0, sizeof(unsigned char)*RS232LengthReceiver); count1 = 0; charCount = 0; } fflush(stdout); } //printf("\n"); usleep(100000); //nanosleep((const struct timespec[]){{0, 100000000L}}, NULL); //sleep(1); //6 memset(arraySender, 0, sizeof(char)*RS232LengthSender); memset(arrayReceiver, 0 , sizeof(unsigned char)*RS232LengthReceiver); memset(prefixCheck, 0, sizeof(unsigned char)*8); //strcpy(arraySender, "@11RD6200002959*"); strcpy(arraySender, "@11RD6220001051*"); arraySender[16] = 0x0d; arraySender[17] = 0x0a; charCount = 0; count1 = 0; flagPack = 0; for(forCount = 0; forCount < 18; ++forCount) { serialPutchar(fd, arraySender[forCount]); } usleep(100000); //nanosleep((const struct timespec[]){{0, 100000000L}}, NULL); //sleep(1); while(serialDataAvail(fd)) { char tempChar1; tempChar1 = serialGetchar(fd); //printf("%c ", tempChar1); if(flagPack == 1 && tempChar1 == '*') { int localCharCount = 0; forCount = 0; for(localCharCount = 0; localCharCount <= charCount-3 ; localCharCount = localCharCount + 4) { if(localCharCount == 4 || localCharCount == 8 || localCharCount == 12 || localCharCount == 16); else { Count[forCount+35] = TransferFormatLongFunction(arrayReceiver[localCharCount]) * 4096 + TransferFormatLongFunction(arrayReceiver[localCharCount+1]) * 256 + TransferFormatLongFunction(arrayReceiver[localCharCount+2]) * 16 + TransferFormatLongFunction(arrayReceiver[localCharCount+3]); forCount++; } } flagPack = 0; } else if(flagPack == 1 && tempChar1 != '*') { arrayReceiver[charCount] = tempChar1; ++charCount; //printf("%c ", tempChar1); }else if(count1 == 7 && strncmp(prefixCheck, "@11RD00", 7) == 0) { flagPack = 1; arrayReceiver[charCount] = tempChar1; ++charCount; }else if(count1 != 0 && count1 < 7) { prefixCheck[count1] = tempChar1; count1++; }else if(tempChar1 == 0x40) { count1 = 1; prefixCheck[0] = tempChar1; }else { memset(prefixCheck, 0, sizeof(unsigned char)*8); memset(arrayReceiver, 0, sizeof(unsigned char)*RS232LengthReceiver); count1 = 0; charCount = 0; } fflush(stdout); } while(serialDataAvail(fd)) { char tempChar1; tempChar1 = serialGetchar(fd); printf("useless %c\n ", tempChar1); fflush(stdout); } //printf("\n"); for(forCount = 0; forCount < 39; ++forCount) { printf("%ld ", Count[forCount]); } printf("\n"); for(forCount = 0; forCount < EVENTSIZE; ++forCount) { //need set ExCount if(Count[forCount] < ExCount[forCount]) { // count reset ExCount[forCount] = 0; }else if((Count[forCount] != 0) && (ExCount[forCount] == 0)) { ExCount[forCount] = Count[forCount]; } else; } //avoid wrong info send form machine for(forCount = 0; forCount < EVENTSIZE; ++forCount) { if(abs(ExCount[forCount] - Count[forCount]) > 15 && errorCheckCount[forCount] < ERRORCHECKMAXRETRY) { Count[forCount] = ExCount[forCount]; errorCheckCount[forCount]++; } else { errorCheckCount[forCount] = 0; } } pthread_mutex_lock(&MutexFile); WriteFile(MachRUNNING); pthread_mutex_unlock(&MutexFile); memcpy(ExCount, Count, sizeof(long)*EVENTSIZE); TotalBadCount = ExCount[4]; printf("%s %s %s %s %s %s|Good Count: %ld|Total Bad: %ld\n", MachineNo, ISNo, ManagerCard, UserNo, CountNo, UploadFilePath, ExCount[GOODCOUNT], TotalBadCount); if(ScreenIndex == 1) { UpdateScreenFunction(1); } pthread_mutex_lock(&MutexSerial); gettimeofday(&now, NULL); outtime.tv_sec = now.tv_sec + WATCHDOGPERIOD; outtime.tv_nsec= now.tv_usec * 1000; pthread_cond_timedwait(&CondSerial, &MutexSerial, &outtime); pthread_mutex_unlock(&MutexSerial); //a timeout mechanism if(NewDataIncome == 1) { watchdogCooldown = WATCHDOGVALUE; NewDataIncome = 0; }else { watchdogCooldown = watchdogCooldown - WATCHDOGPERIOD; printf("%d\n", watchdogCooldown); }if(watchdogCooldown <= 0) { ZHResetFlag = 1; } /* //check network status int fd2 = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); struct ifreq ethreq; memset(ðreq, 0, sizeof(ethreq)); strncpy(ethreq.ifr_name, ZHNETWORKTYPE, IFNAMSIZ); ioctl(fd2, SIOCGIFFLAGS, ðreq); if(ethreq.ifr_flags & IFF_RUNNING) { //digitalWrite (ZHPIN15, HIGH); //digitalWrite (ZHPIN16, HIGH); //digitalWrite (ZHPIN18, LOW); }else { //digitalWrite (ZHPIN15, HIGH); //digitalWrite (ZHPIN16, LOW); //digitalWrite (ZHPIN18, LOW); } close(fd2); */ } if(fd >= 0) { serialClose(fd); } printf("serial function exit\n"); }
void SerialComm::Disconnect() { printf("disconnecting\n"); serialClose(fd); // close(fd); }
RS485::~RS485() { serialClose(dev); }
int main(int argc, char **argv) { SerialPort SP; int ftBuffer = -1; eventdef_t *evdef; UINT32_T sizetype, sizevalue, bufsize; char *valBuf; messagedef_t reqdef; message_t request, *response; char *confname; if (argc < 2) { confname = "serial_event.conf"; } else { confname = argv[1]; } if (parseConfig(&conf, confname) != 0) { printf("Errors during parsing the configuration file\n"); exit(1); } sizetype = wordsize_from_type(conf.type_type) * conf.type_numel; sizevalue = wordsize_from_type(conf.value_type) * conf.value_numel; bufsize = sizeof(eventdef_t) + sizetype + sizevalue; evdef = (eventdef_t *) malloc(bufsize); if (evdef == NULL) { printf("Out of memory\n"); exit(1); } /* prepare fixed fields */ reqdef.version = VERSION; reqdef.bufsize = bufsize; reqdef.command = PUT_EVT; request.def = &reqdef; request.buf = evdef; evdef->offset = conf.offset; evdef->duration = conf.duration; evdef->type_type = conf.type_type; evdef->type_numel = conf.type_numel; evdef->value_type = conf.value_type; evdef->value_numel = conf.value_numel; evdef->bufsize = sizetype + sizevalue; valBuf = (char *) evdef + sizeof(eventdef_t); memcpy(valBuf, conf.type_buf, sizetype); valBuf += sizetype; memcpy(valBuf, conf.value_buf, sizevalue); if (!serialOpenByName(&SP, conf.comport)) { printf("Could not open serial port.\n"); exit(1); } /* timeout = 1 decisecond (least common denominator) = 100 ms */ if (!serialSetParameters(&SP, conf.baudrate, conf.databits, conf.parity, conf.stopbits, 1)) { printf("Could not modify serial port parameters\n"); exit(1); } ftBuffer = open_connection(conf.hostname, conf.port); if (ftBuffer < 0) { printf("Connection to FieldTrip buffer failed.\n"); exit(1); } sample = conf.sample_start; udp_socket = create_udp_receiver(conf.udp_port); if (udp_socket != -1) { if (pthread_create(&udpThread, NULL, _udp_thread, NULL)) { printf("Warning: UDP socket thread could not be spawned.\n"); closesocket(udp_socket); udp_socket = -1; } } /* register CTRL-C handler */ signal(SIGINT, abortHandler); printf("Starting to listen - press CTRL-C to quit\n"); while (keepRunning) { char input; int n; n = serialRead(&SP, 1, &input); if (n<0) { printf("Error while reading from serial port - exiting\n"); break; } if (n==0) continue; /* timeout - just wait longer */ /* we got one */ if (conf.character != -1 && conf.character != input) { printf("Ignoring input %c\n", input); continue; } if (conf.set_value) *valBuf = input; pthread_mutex_lock(&sampleMutex); evdef->sample = sample; pthread_mutex_unlock(&sampleMutex); if (evdef->sample < 0) { printf("Ignoring negative sample (%i) event...\n", evdef->sample); } else { n = tcprequest(ftBuffer, &request, &response); if (n<0 || response == NULL) { printf("Error in FieldTrip connection\n"); } else { if (response->def == NULL || response->def->command != PUT_OK) { printf("FieldTrip server returned an error\n"); } else { printf("Sent off event (sample = %i, input = %c)\n", evdef->sample, input); } FREE(response->def); FREE(response->buf); free(response); } } pthread_mutex_lock(&sampleMutex); sample += conf.sample_increase; pthread_mutex_unlock(&sampleMutex); } if (udp_socket!=-1) { pthread_join(udpThread, NULL); closesocket(udp_socket); } close_connection(ftBuffer); serialClose(&SP); free(evdef); return 0; }
/** * close serial port */ void stm32Close() { serialClose(board.serial_fd); }
CV7::~CV7() { if(m_fd < 0) { serialClose(m_fd); } }
int main() { // int wiringPiI2CSetup (int devId); /* int r = wiringPiSetup(); if(r < 0) { std::cout << "error connecting gpio" << std::endl; return -1; } for(int i = 0; i < 8; i++) pinMode(i, OUTPUT); //digitalWrite(1, 1); digitalWriteByte(170); delay(100); digitalWriteByte(85); delay(100); digitalWriteByte(0); */ if(wiringPiSetup() == -1) { std::cout << "error seting up comm" << std::endl; return -1; } else { std::cout << "comm sucessfully set up" << std::endl; } int serialFD = serialOpen("/dev/ttyACM0", 9600); if(serialFD < 0) { std::cout << "error connecting usb [ACM0] device" << std::endl; serialFD = serialOpen("/dev/ttyAMA0", 9600); if(serialFD < 0) { std::cout << "error connecting usb [AMA0] device" << std::endl; serialFD = serialOpen("/dev/ttyUSB0", 9600); if(serialFD < 0) { std::cout << "error connecting usb [USB0] device" << std::endl; return -1; } } } std::cout << "connected USB device: " << serialFD << std::endl; if(wiringPiSetup() == -1) { std::cout << "error seting up comm" << std::endl; return -1; } else { std::cout << "comm sucessfully set up" << std::endl; } for(unsigned char outBuffer = 0; outBuffer < 'z'; outBuffer++) serialPutchar(serialFD, outBuffer); while(1) { // if(serialDataAvail(serialFD) > 0) // { int inbuffer = serialGetchar(serialFD); if(inbuffer == 'x' || inbuffer == -1 || inbuffer == 0) break; serialFlush(serialFD); std::cout << "received: " << (int)inbuffer - '0' << std::endl; // } } serialClose(serialFD); digitalWrite(0, 0); return 0; }
void serialthread::close() { if ( device_id < 0 ) return; serialClose(device_id); active = false; }
int main(){ lcd=serialOpen(SERIAL, BAUD_RATE); clearDisplay(); serialClose(lcd); return 0; }
void Serial::Close(){ //std::cout << "Serial port closed\n"; serialClose(this->fd); }
void intHandler(int dummy) { if (fd != -1) serialClose(fd); exit(2); }
int main (void) { int fSerial = 0; FILE * filehex; char hexBuf[256]; int line = 0; wiringPiSetup(); fSerial = serialOpen("/dev/ttyAMA0",115200); if(fSerial == -1) { printf("error while opening tty\r\n"); return 1; } filehex = fopen("domodin.hex","r"); if(filehex == NULL) { printf("error while opening hex file\r\n"); return 2; } pinMode (PIN_RST, OUTPUT); pinMode (BOOTLOADER_PIN1, OUTPUT); pinMode (BOOTLOADER_PIN2, OUTPUT); // Set IO to reset the PIC and to enable its bootloader digitalWrite (BOOTLOADER_PIN2, LOW); digitalWrite (BOOTLOADER_PIN1, HIGH); // reset PIC digitalWrite (PIN_RST, HIGH); delay(10); digitalWrite(PIN_RST, LOW); // disable the bootloader mode delay(10); digitalWrite(BOOTLOADER_PIN1, LOW); serialFlush(fSerial); while(fgets(hexBuf, sizeof(hexBuf), filehex) != NULL) { // only transmit HEX file lines with proper start code (':' character) if(hexBuf[0] != ':') continue; // transmit serialPuts(fSerial, hexBuf); if(serialGetchar(fSerial) != 0x06) { printf("Error line %d !\r\n", line); break; } line++; } pinModeAlt (BOOTLOADER_PIN1, 0b100); pinModeAlt (BOOTLOADER_PIN2, 0b100); serialClose(fSerial); fclose(filehex); printf("New firmware loaded !\r\n"); return 0 ; }
int main(int argc, char *argv[]) { int fd; int i; struct context ctx; if (0 != parse_options(argc, argv)) { usage(); return 1; } if ((fd = serialOpen(device_name)) < 0) { fprintf(stderr, "Failed to open %s\n", device_name); return 1; } ctx.fd = fd; if (opt_flash) { if (0 != check_for_bootloader(fd, opt_timeout)) { fprintf(stderr, "No bootloader detected\n"); return 1; } else { printf("Bootloader detected\n"); } for (i=0;i<FLASH_SIZE_KB/FLASH_PAGE_SIZE;i++) { printf("Erasing page %d\n", i); if (0 != erase_page(fd, i)) { fprintf(stderr, "Erasing page failed\n"); return 1; } } if (0 != read_hexfile(flash_filename, handle_record_00, handle_record_04, &ctx)) { fprintf(stderr, "Failed to read %s\n", flash_filename); return 1; } if (0 != reset(fd)) { fprintf(stderr, "Reset failed\n"); return 1; } } if (opt_console) { atexit(do_exit); printf("Connected to %s, ctrl-c to exit\n", device_name); do_console(fd); } serialClose(fd); return 0; }