//*************************************************************************************************************** int main(int argc, char **argv) { int loop = 0; int command =0; fd = open(PORT, O_RDWR | O_NOCTTY | O_NDELAY); if (fd == -1) { perror("open_port: Unable to open PORT - "); //opps something bad happened... return 1; } else { fcntl(fd, F_SETFL, 0); } initport(fd); //check to see if commandline arguments are present if (argc >= 2) { for (loop =1; loop < argc; loop++) { if ((command = parse_input(argv[loop] )) >=0 ) send_command( command ); usleep(1000000/4); } printf("\n"); return 0; //exit } show_commands(); return 0; }
int main() { if((serial = initport("/dev/ttyUSB0", B115200, 0)) < 0) { return -1; } Send_SYNC_Cmd(); return 0; }
int main() { char devicename[] = "/dev/ttyUSB0"; int fd = 0; fd = open(devicename, O_RDWR | O_NOCTTY ); int set_bits = 4; ioctl(fd, TIOCMSET, &set_bits); initport(fd); printf("YHY502 reader started\n"); //~ yhy502_send_command(fd, YHY502_CMD_READ_CARD_SERIAL, 0, 0); unsigned char response_buffer[255]; int count_recieved; count_recieved = yhy502_send_recieve(fd, YHY502_CMD_READ_CARD_SERIAL, 0,0, response_buffer); printf("recieved %d: ", count_recieved); int i; for (i = 0; i < count_recieved; i++) { printf("%x ",response_buffer[i]); } printf("\n"); }
int main(int /*argc*/, char** /*argv*/) { char logbuf[k_LogBufSize]; int retval; printf("Starting Emperor\n"); //printf("sizeof(long long int) = %d\n", sizeof(long long int)); //printf("sizeof(double) = %d\n", sizeof(double)); //printf("timestamp: %.6f\n", emperor_current_time()); //6 decimal places is microseconds printf("Please ensure that driver-gui.sc (viewer '()) is running on %s\n", k_Server); pan_fd = open(pan_file, O_WRONLY); if (pan_fd < 1) { perror("pan:"); emperor_signal_handler(SIGTERM); return -1; } tilt_fd = open(tilt_file, O_WRONLY); if (tilt_fd < 1) { perror("tilt:"); emperor_signal_handler(SIGTERM); return -1; } motor_fd = initport(); if (motor_fd < 1) { perror("motor:"); emperor_signal_handler(SIGTERM); return -1; } gpio_fd = open(gpio_file, O_RDONLY); if (gpio_fd < 1) { perror("gpio:"); emperor_signal_handler(SIGTERM); return -1; } //start network stuff and wait for connection printf("Connecting to %s on port %s for commands...\n", k_Server, k_CommandPort); sockfd = -1; while (sockfd == -1) { sockfd = ClientConnect(k_Server, k_CommandPort); } printf("success!\n"); printf("Connecting to %s on port %s for data logging...\n", k_Server, k_LogPort); log_sockfd = -1; while (log_sockfd == -1) { log_sockfd = ClientConnect(k_Server, k_LogPort); } sprintf(logbuf, "Logging started"); emperor_log_data(logbuf, log_sockfd); printf("success!\n"); //printf("log_sockfd = %d\n", log_sockfd); //start bump switch monitoring thread here gpio_thread_should_die = FALSE; pthread_attr_t attributes; pthread_attr_init(&attributes); pthread_attr_setdetachstate(&attributes, PTHREAD_CREATE_JOINABLE); pthread_create(&gpio_thread, &attributes, emperor_monitor_bump_switches, NULL); sprintf(logbuf, "Bump switch monitoring active"); retval = emperor_log_data(logbuf, log_sockfd); if (retval != 0) printf("logging failed for \'%s\'\n", logbuf); pthread_attr_destroy(&attributes); printf("%s\n", logbuf); //***START IMU AND GPS THREAD(S) HERE*** //start the functions from run-sensors printf("Starting sensors..."); //call setup function if (!run_sensors_setup()) { printf("run_sensors_setup() failed\n"); emperor_signal_handler(SIGTERM); return -1; } //call start function run_sensors_start(); printf("sensors started successfully\n"); //register signal handler for termination signal(SIGINT, emperor_signal_handler); signal(SIGTERM, emperor_signal_handler); char msgbuf[k_maxBufSize]; char prevmsgbuf[k_maxBufSize]; memset(prevmsgbuf, 0, sizeof(prevmsgbuf)); memset(motor_prev, 0, sizeof(motor_prev)); memset(pan_prev, 0, sizeof(pan_prev)); memset(tilt_prev, 0, sizeof(tilt_prev)); //int retval; //loop on listening for commands while(1) { memset(msgbuf, 0, sizeof(msgbuf)); //clear buffer retval = recv(sockfd, &msgbuf, sizeof(msgbuf), 0); if (retval < 0) { printf("Error in recv\n"); emperor_signal_handler(SIGTERM); return -1; } if (retval == 0) { printf("Sender closed connection\n"); emperor_signal_handler(SIGTERM); return -1; } //if we get here, we have something useful in msgbuf, so do something with it if (strncmp(msgbuf, prevmsgbuf, k_maxBufSize) != 0) //received new command { // printf("Received %d bytes: %s\n", retval, msgbuf); strncpy(prevmsgbuf, msgbuf, k_maxBufSize); } else //got a repeat of the last received command, so ignore it continue; //do stuff with commands received retval = emperor_parse_and_execute(msgbuf); if (retval != 0) { printf("Error in emperor_parse_and_execute\n"); emperor_signal_handler(SIGTERM); return -1; } } //cleanup done in signal handler return 0; }
void main() { //CALL THE INITIALIZING FUNCTION initport(); initpwm(); while(1) { indicator(); CalcError(); if((error == 0) && (s4+s5==2)) { T1CON.TMR1ON = 0; motor_LF(); //FWD AT FULL SPEED motor_RF(); PWM1_CHANGE_DUTY(255); PWM2_CHANGE_DUTY(255); delay_ms(10); } if((s1+s2+s3+s4+s5+s6+s7+s8) == 0) //ROBOT HAS OVERSHOOT { T1CON.TMR1ON = 0; if(lastreading == 'r') //CHECKS IF THE LAST SENSOR ACTIVATED WAS RIGHT { T1CON.TMR1ON = 0; motor_RB(); //TURN RIGHT AT FULL SPEED motor_LF(); PWM1_CHANGE_DUTY(255); PWM2_CHANGE_DUTY(255); delay_ms(10); //error=0; } else if(lastreading == 'l') //CHECKS IF THE LAST SENSOR ACTIVATED WAS LEFT { T1CON.TMR1ON = 0; motor_LB(); //TURN LEFT AT FULL SPEED motor_RF(); PWM1_CHANGE_DUTY(255); PWM2_CHANGE_DUTY(255); delay_ms(10); //error=0; } } if ( counter>200) { T1CON.TMR1ON = 0; PORTC.F7 = 0; PORTC.F6 = 0; PORTC.F5 = 0; PORTC.F4 = 0; while(1); } if( (s1+s2+s3+s4+s5+s6+s7) == 7 || (s2+s3+s4+s5+s6+s7+s8) == 7 || (s1+s2+s3+s4+s5+s6+s7+s8) == 8) // TO STOP THE MOTOR AT THE END OF LINE { T1CON.TMR1ON = 1; // enable timer1 // delay_ms(3) ; // if((s1+s2+s3+s4+s5+s6+s7+s8) == 0) /* { PORTC.F7 = 0; PORTC.F6 = 0; PORTC.F5 = 0; PORTC.F4 = 0; */ } else //ROBOT ON THE LINE { T1CON.TMR1ON = 0; PROPORTIONAL = error * kp; INTEGRAL += error ; INTEGRAL *= ki; DERIVATIVE = (error - perror); correction = ( (PROPORTIONAL) + (INTEGRAL) + (DERIVATIVE*kd)); rightpulse = basespeed + (correction/2); leftpulse = basespeed - (correction/2); motor_RF(); motor_LF(); if(leftpulse > 255) //LEFT CORRECTION EXCEED leftpulse = 255; if(rightpulse > 255) //RIGHT CORRECTION EXCEED rightpulse = 255; if(leftpulse < 0) //LEFT CORRECTION EXCEED leftpulse = 0; if(rightpulse < 0) //RIGHT CORRECTION EXCEED rightpulse = 0; PWM1_CHANGE_DUTY(rightpulse); PWM2_CHANGE_DUTY(leftpulse); } delay_ms(10); } }
int Manual(int argc, unsigned char **argv) { /* Serial port initialization */ fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY); if (fd == -1) { perror("open_port: Unable to open /dev/ttyS0 - "); return 1; } else { fcntl(fd, F_SETFL, 0); } getbaud(fd); initport(fd); int PVi, k; K= uatof(Gain); /* Things will be drawn more quickly if you always acquire the screen before trying to draw onto it. */ acquire_screen(); Graphics(); /* Call to graphics from a different function to avoid messy code */ RLE_SPRITE *rle; sCmd[0]= 'C'; writeport(fd, sCmd); PVi= 0; k= 100; M=0; while (!key[KEY_ESC]) /* Only pressing ESC can Exit */ { char ValveState; if(ValveState=='O') { sCmd[0]= 'A'; writeport(fd, sCmd); } if(ValveState=='C') { sCmd[0]= 'C'; writeport(fd, sCmd); } M++; /* Check if user needs some help */ if(key[KEY_F1]) Help(); k++; i= 897; Captura = create_sub_bitmap(screen, 72, 350, 898, 368); rle = get_rle_sprite(Captura); destroy_bitmap(Captura); draw_rle_sprite(screen, rle, 71, 350); destroy_rle_sprite(rle); /* This line reads data from the interfase which is the process variable(measured variable) of the system */ fcntl(fd, F_SETFL, FNDELAY); // don't block serial read readport(fd,sResult); PVi= (int) *sResult; PV= PVi; /* if(PVi<=40) { PV= 51; system("festival --tts Messages/Disconnected&"); blit(Disconnected, screen, 0, 0, 70, 290, 887, 52); } */ if(PV<=48) PVi= 51; PV= 1.794117647*(PVi-51); SP= PV; if(key[KEY_RIGHT]) { //fd = close("/dev/parport0"); Simulator(); } if(key[KEY_PGUP]) OP= (OP+46); if(key[KEY_PGDN]) OP= (OP-46); if(key[KEY_UP]) { OP=162; //(OP+3.66); sCmd[0]= 'A'; writeport(fd, sCmd); ValveState='O'; } if(key[KEY_DOWN]) { if(OP>=1) OP= 0;//(OP-3.66); sCmd[0]= 'C'; writeport(fd, sCmd); ValveState='C'; } if(key[KEY_PRTSCR]) { Captura = create_sub_bitmap(screen, 0, 0, 1024, 768); save_bitmap("images/User/Captura.pcx", Captura, pal); destroy_bitmap(Captura); } Timer++; if(OP<=0) OP= 0; // if (PV>=40) // { textprintf_ex(screen, font, 230, 297, BLACK, WHITE, "%3.1f", (PV/368)*100); // Medición textprintf_ex(screen, font, 450, 297, BLACK, WHITE, "%3.1f", (SP/368)*100); // SP textprintf_ex(screen, font, 710, 297, BLACK, WHITE, "%3.1f", (OP/368)*100); // Controlador // } if(k>=100) { k= 0; vline(screen, 967, 351, 717, GRAY); blit(Clean, screen, 0, 0, 968, 350, 2, 368); } int Recorder; Recorder++; if(Recorder>=900) { Recorder= 0; Captura = create_sub_bitmap(screen, 258, 350, 715, 368); } Captura = create_sub_bitmap(screen, 248, 350, 705, 368); if(PV>=362) PV= 365; if(OP>=367) OP= 365; if(PV<=0) PV= 0; if(OP<=0) OP= 1; /* OPi= fixtoi(itofix((OP*0.69234783)/255*100)); sCmd[0]= (unsigned char)OPi+50; sCmd[0]= sCmd[0]+0.00; writeport(fd, sCmd); */ // textprintf_ex(screen, font, 30, 297, BLACK, WHITE, "%i - %s - %3.1f", PVi, sResult, PV); // Medición /* Draw the behaviour of the PV, SP and OP over time */ line(screen, 71+i, 717-PV, 71+i, 717-PVj, RED); PVj= PV; /* Flag for line y2 as a precedent state */ line(screen, 71+i, 717-OP, 71+i, 717-OPj, BLUE); OPj= OP; /* Flag for line y2 as a precedent state */ fprintf(outfile,"%i\t%f\t %f\t %f\n", M, ((PV/368)*100), ((SP/368)*100), ((OP/368)*100)); rest(Delay); } int ScreenWide; RLE_SPRITE *rle0, *rle1; BITMAP *Screen3; Screen3 = load_bitmap("images/Close/Base.pcx", pal); system("mp3blaster /home/mentesuprema/Residencia/sounds/swing2.wav &"); rest(5); rle0= get_rle_sprite(Screen2); rle1= get_rle_sprite(Screen3); for(ScreenWide=0;ScreenWide<=768;ScreenWide=ScreenWide+5) { draw_rle_sprite(screen, rle0, 0, 768); draw_rle_sprite(screen, rle1, 0, -768+ScreenWide); } destroy_rle_sprite(rle0); destroy_rle_sprite(rle1); destroy_bitmap(Screen2); destroy_bitmap(Screen3); destroy_bitmap(Clean); destroy_bitmap(Disconnected); release_screen(); Close(); exit(0); }
int main(int argc, char * argv[]) { unsigned char response_buffer[255]; int count_recieved; unsigned char delay = 100; verbose = 0; unsigned short cardtype; char * devicename = "/dev/ttyS0"; char server_send_buffer[1024] = ""; char server_recv_buffer[1024] = ""; int gpio = open("/dev/gpio2", O_WRONLY); write(gpio, "LED SWITCH 1\n", 13);//set gpio to output (what ioctl??) //~ logfd = fopen("server.log","w"); logfd = stdout; int argi; for (argi = 1; argi < argc; argi++) { if (strcmp("-v", argv[argi]) == 0 ) { verbose = 1; } else if (argv[argi][0] != '-') { devicename = argv[argi]; } } int fd = 0; fd = open(devicename, O_RDWR | O_NOCTTY ); int set_bits = 4; ioctl(fd, TIOCMSET, &set_bits); initport(fd); fprintf(logfd, "yhy632 reader started\n"); yhy632_beep(fd, 50); yhy632_set_led(fd, YHY632_LED_RED); unsigned char version[16]; count_recieved = yhy632_send_recieve(fd, YHY632_CMD_READ_FW_VERSION, 0, 0, version); fprintf(logfd, "version: %.*s\n", count_recieved, version); socket_connect(); unsigned char serial[16]; char matrix_serial[64]; unsigned char prev_serial[16] = ""; unsigned char prev_serial_len = 0; unsigned char prev_cardtype = 0; while (1) { count_recieved = yhy632_select(fd, &cardtype, serial); if (count_recieved > 0) { if ( ( memcmp(prev_serial, serial, count_recieved ) != 0 ) || (prev_serial_len != count_recieved) || (prev_cardtype != cardtype) ) { yhy632_serial_to_matrix_iii(cardtype, serial, count_recieved,matrix_serial); sprintf(server_send_buffer,"A %d %s\n",LOCATION,matrix_serial); if (verbose) { fprintf(logfd, "cardtype: %d\n", cardtype); fprintf(logfd, "count_recieved: %d\n", count_recieved); fprintf(logfd, "serial:%s\n",matrix_serial); fprintf(logfd, "server_buffer:%s\n",server_send_buffer); } while ( send(sock_fd,server_send_buffer, strlen(server_send_buffer),MSG_NOSIGNAL ) < 0) { fprintf(logfd, "error while sending\n"); socket_connect(); }; if (synchronous_read(sock_fd, server_recv_buffer, 8, 1) > 0) { if (strcmp(server_recv_buffer,"ACCEPTED")==0) { fprintf(logfd,"Access granted to %s \n",matrix_serial);fflush(logfd); // open the lock ioctl(gpio, 1, 2); usleep(1E3*50); ioctl(gpio, 0, 2); yhy632_set_led(fd, YHY632_LED_GREEN); yhy632_beep(fd, 100); yhy632_set_led(fd, YHY632_LED_RED); //~ usleep(1E3*500); } else { fprintf(logfd,"Access denied to %s, answer was %s \n",matrix_serial,server_recv_buffer );fflush(logfd); int i; for (i = 0; i< 3; i++ ) { yhy632_set_led(fd, 0); yhy632_beep(fd, 10); yhy632_set_led(fd, YHY632_LED_RED); //~ usleep(1E3*20); } } } prev_serial_len = count_recieved; prev_cardtype = cardtype; memcpy(prev_serial, serial, count_recieved); } } else { //~ printf("select error\n"); prev_cardtype = 0; } } }
int main(int argc, char **argv) { redisContext *c; redisReply *reply; json_t *root; // json_error_t error; char *msg; int ret; char buf[BUFSIZ]; progname = argv[0]; // Capture signals signal(SIGINT, sig_handler); signal(SIGKILL, sig_handler); // Sleep for a bit so everything settles down // pve: was 64 sleep(2); fprintf(stderr, "%s started\n", progname); // This used to be portux.local, but portux is not great at spreading its address, so use IP // since it now runs on the portux, use localhost const char *hostname = (argc > 1) ? argv[1] : "127.0.0.1"; int port = (argc > 2) ? atoi(argv[2]) : 6379; // initialize the stripping table init_table(); struct timeval timeout = { 1, 500000 }; // 1.5 seconds c = redisConnectWithTimeout(hostname, port, timeout); if (c == NULL || c->err) { if (c) { fprintf(stderr, "Connection error: %s\n", c->errstr); redisFree(c); } else { fprintf(stderr, "Connection error: can't allocate redis context\n"); } exit(1); } // open device for reading only if ((ret = initport(0)) == 0) { fprintf(stderr, "device opened ok\n"); } else { fprintf(stderr, "failed to initialize device\n"); close(fdDevice); fflush(stderr); exit(1); } // Create a JSON template root = json_object(); json_object_set (root, "t", json_pack("s", "all")); // set the type of message json_object_set (root, "e", json_pack("s", "newMessage")); // the event is newMessage // and the received entry goes into the parameter field // Main loop while ((ret = readln_time(buf,100))) { if (ret > 0 && strlen(buf) > 0) { // remove extraneous characters strip(buf, IS_CTRL); if (strlen(buf) > 0) { if (bStderr) fprintf (stderr, "Received Buf %s\n", buf); if (strncmp(buf, faulty, strlen(faulty)) != 0) { // and the received entry goes into the parameter field json_object_set (root, "p", json_pack("s", buf)); msg = json_dumps (root, JSON_COMPACT | JSON_ESCAPE_SLASH); if (bStderr) fprintf (stderr, "Publish %s\n", msg); /* Publish this set */ reply = redisCommand(c,"PUBLISH ss:event %s", msg); // printf("PUBLISH: %s\n", reply->str); freeReplyObject(reply); } } // if strlen > 0 } // if // Force the buffer to be empty buf[0] = (char) 0; } // while close(fdDevice); fprintf(stderr, "%s finished\n", progname); fflush(stderr); /* Disconnects and frees the context */ redisFree(c); return 0; }
int main(int argc, char *argv[]) { if(!init_cfg()) { fprintf(stderr, "Unable to open config file or invalid content in config file.\n"); exit(1); } parseopts(argc, argv, conf); if(conf->msg) { if(!open_producer()) { printf("Unable to contact Bluemote server!\n"); exit(1); } if(ipc_write(conf->device, strlen(conf->device))<=0) { printf("Unable to send message to Bluemote server!\n"); close_producer(); exit(1); } close_producer(); exit(0); } else { if(!get_lock()) { printf("Another instance of Bluemote already running!\n"); exit(1); } if(!open_consumer()) { printf("Unable to listen for messages!\n"); exit(1); } } strcpy(logfile, getenv("HOME")); strcat(logfile, BLUEMOTEDIR); strcat(logfile, LOGFILE); printf("Setting up signal handlers.\n"); logger("INIT", "Setting up signal handlers"); if(!init_signals()) { perror("init_signals()"); exit(1); } if(conf->daemon) { printf("Entering daemon mode.\n"); logger("INIT", "Entering daemon mode.\n"); if(daemon(TRUE, FALSE)==-1) { printf("Unable to enter daemon mode. Exiting.\n"); logger("ERROR", "Unable to enter daemon mode. Exiting.\n"); exit(1); } } while(TRUE) { closeport(); printf("Connecting to phone...\n"); logger("INIT", "Connecting to phone..."); while(openport(conf->device) == -1) { sprintf(buf,"Unable to connect to phone. Will retry after %d secs.",conf->retrysecs); logger("INIT", buf); sleep(conf->retrysecs); logger("INIT", "Retrying..."); } printf("Connected to phone.\n"); logger("INIT", "Connected to phone."); printf("Initialising the connection.\n"); logger("INIT", "Initialising the connection.\n"); if(!initport()) exit(1); printf("Waiting for commands from phone\n"); logger("INIT", "Waiting for commands from phone\n"); /* Not looping to take care of timeout because timeout is very unlikely and * this function is called again inside remote(). In the worst case, the I/O * commands for Connect event won't have any effect. */ if(init_mainmenu()==-1) continue; exec_event("Connect"); if(!manual) { exec_event("MoveIn"); } manual = FALSE; remote(); if(!manual) { exec_event("MoveOut"); } exec_event("Disconnect"); } return(0); }
int main(int argc, char * argv[]) { int fd = 0; unsigned char response_buffer[255]; int count_recieved; unsigned char delay = 100; verbose = 0; unsigned short cardtype; char *devicename = "/dev/ttyUSB0"; char server_send_buffer[1024] = ""; char server_recv_buffer[1024] = ""; logfd = stdout; int argi; for (argi = 1; argi < argc; argi++) { if (strcmp("-v", argv[argi]) == 0 ) { verbose = 1; } else if (argv[argi][0] != '-') { devicename = argv[argi]; } } fd = open(devicename, O_RDWR | O_NOCTTY ); int set_bits = 4; ioctl(fd, TIOCMSET, &set_bits); initport(fd); fprintf(logfd, "started ...\n"); beep(fd, 10); set_led(fd, LED_GREEN); unsigned char version[16]; count_recieved = send_recieve(fd, CMD_READ_FW_VERSION, 0, 0, version); fprintf(logfd, "version: %.*s\n", count_recieved, version); unsigned char serial[16]; char matrix_serial[64]; unsigned char prev_serial[16] = ""; unsigned char prev_serial_len = 0; unsigned char prev_cardtype = 0; unsigned char prev_card[256]; unsigned long prev_timestamp = 0; unsigned char card_status = 0, usb_status = 0; char slen, rlen; unsigned char ibuffer[1024], obuffer[1024], *in = ibuffer, *out; while (1) { count_recieved = mifare_select(fd, &cardtype, serial); if( access("/dev/sdb1", R_OK) == 0){ if( usb_status == 0 ){ beep(fd, 5); beep(fd, 5); beep(fd, 5); usb_status = 1; } } else usb_status = 0; if (count_recieved > 0 && card_status == 0) { if ( ( memcmp(prev_serial, serial, count_recieved ) != 0 ) || (prev_serial_len != count_recieved) || (prev_cardtype != cardtype) ) { serial_to_matrix_iii(cardtype, serial, count_recieved, matrix_serial); // wait 5 seconds if it's the same card serial if( strcmp( prev_card, matrix_serial ) == 0 && timestamp_sec() - prev_timestamp < 5 ) { usleep( 1000 * 100 ); continue; } set_led(fd, LED_RED); strcpy(prev_card, matrix_serial); prev_timestamp = timestamp_sec(); fprintf(logfd, "\nserial:%s\n",matrix_serial); if( access("/dev/sdb1", R_OK) == 0 ){ system("mount /dev/sdb1 /media/usb"); system("ls /media/usb"); if( access("/media/usb/.griver_token", R_OK) == 0){ // auth on usb // move .grive on card printf(" STORE TOKEN TO CARD \n "); } else { // save .grive on ram drive // ln on usb printf(" SYNC USING CARD DATA \n "); } system("umount /media/usb"); } memset(ibuffer, 0xFA, 32); memset(ibuffer, 0x3B, 16); int err = write_sector(fd, 4, 0x60, 0, ibuffer); fprintf(logfd, "write code [%d]\n", err); int size = dump(fd, 0x60, 0 , obuffer); out = obuffer; fprintf(logfd, "size is %d\n", size); while(out-obuffer < size){ fprintf(logfd, "%02X ", *out); if((out-obuffer) % 16 == 15 ) fprintf(logfd, "\n"); *out++; } fprintf(logfd,"\n"); card_status = 1; } else { //~ printf("select error\n"); prev_cardtype = 0; } } else { if( card_status == 1 ) { card_status = 0; beep(fd, 5); usleep( 1000 * 500 ); } } set_led(fd, LED_GREEN); } }
int main(int argc, char **argv) { char response[MAX_STR]; int i; int state, next_state; short header; int msg_type, msg_length; char msg_data[MAX_MSG_LEN]; int n, length; int r; char data; f = fopen("/tmp/log.txt", "wt"); assert(f != NULL); /* open and configure serial port */ fd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY | O_NDELAY); if (fd == -1) { perror("open_port: Unable to open /dev/ttyS0 - "); exit(1); } else { fcntl(fd, F_SETFL, 0); } initport(fd); fin = NULL; if (argc >= 2) { fin = fopen(argv[1],"rb"); assert(fin != NULL); } fout = NULL; if (argc == 3) { fout = fopen(argv[2],"wb"); assert(fout != NULL); } /* check DV Dongle is alive */ write_dongle(fd, target_name, sizeof(target_name)); read_dongle(fd, response, LEN_TARGET_NAME_RESPONSE); if (strcmp(&response[4],"DV Dongle") != 0) { printf("DV Dongle not responding\n"); exit(1); } printf("Found DV Dongle....\n"); c_in.header = 0x13ec; c_in.power = 0x0; c_in.control1 = 0x0; //#define RATE2000 #ifdef RATE2000 c_in.rate[0] = 0x0028; /* 2000 bit/s, no FEC */ c_in.rate[1] = 0x0000; c_in.rate[2] = 0x0000; c_in.rate[3] = 0x0000; c_in.rate[4] = 0x6248; #endif #define RATE3600_1200 #ifdef RATE3600_1200 c_in.rate[0] = 0x5048; /* 3600 bit/s, 1200 bit/s FEC */ c_in.rate[1] = 0x0001; c_in.rate[2] = 0x0000; c_in.rate[3] = 0x2412; c_in.rate[4] = 0x6860; #endif c_in.unused[0] = 0x0; c_in.unused[1] = 0x0; c_in.unused[2] = 0x0; c_in.dtmf = 0x00ff; c_in.control2 = 0x8000; /* put codec in run mode */ write_dongle(fd, run_state_run, sizeof(run_state_run)); //write_dongle(fd, data_item_1, sizeof(data_item_1)); //write_dongle(fd, (char*)&c_in, sizeof(c_in)); state = MSGSTATE_HDR1; header = msg_type = msg_length = n = length = 0; c_msg = uc_msg = 0; for(i=0; i<100000; i++) { /* We can only reliably read one byte at a time. Until I realised this there was "much wailing and gnashing of teeth". Trying to read() n bytes read() returns n but may actually reads some number between 1 and n. So it may only read 1 byte int data[] but return n. */ r = read(fd, &data, 1); assert(r == 1); /* used state machine design from ambetest103.zip, SerialPort.cpp */ next_state = state; switch(state) { case MSGSTATE_HDR1: header = data; next_state = MSGSTATE_HDR2; break; case MSGSTATE_HDR2: header |= data<<8; msg_length = header & LENGTH_MASK; msg_type = header & TYPE_MASK; //printf("%0x %d\n", msg_type, msg_length); if (length == 2) { parse_message(msg_type, msg_length, msg_data); next_state = MSGSTATE_HDR1; } else { if (msg_length == 0x0) length = 8192; else length = msg_length - 2; n = 0; next_state = MSGSTATE_DATA; } break; case MSGSTATE_DATA: msg_data[n++] = data; length--; if (length == 0) { parse_message(msg_type, msg_length, msg_data); next_state = MSGSTATE_HDR1; } break; } state = next_state; } printf("finished, c_msg = %d uc_msg = %d\n", c_msg, uc_msg); write_dongle(fd, run_state_stop, sizeof(run_state_stop)); close(fd); if (fin != NULL) fclose(fin); if (fout != NULL) fclose(fout); fclose(f); return 0; }
/*----------------------------------------------------------------------------*/ LRESULT CALLBACK DlgProc(HWND hWndDlg, UINT Msg, WPARAM wParam, LPARAM lParam) { switch(Msg) { case WM_INITDIALOG: { initport(hWndDlg, Msg, wParam, lParam); showdate(hWndDlg); return FALSE; // setfocus by ourself } break; // WM_INITDIALOG case WM_COMMAND: // Windows Controls processing { switch(LOWORD(wParam)) // This switch identifies the control { case IDC_CB_PORTS: cbport(hWndDlg, Msg, wParam, lParam); break; // case IDC_CB_PORTS case IDC_CB_BAUDRATE: cbbaudrate(hWndDlg, Msg, wParam, lParam); break; // case IDC_CB_BAUDRATE case IDC_CB_DATABITS: cbdatabits(hWndDlg, Msg, wParam, lParam); break; // case IDC_CB_DATABITS case IDC_CB_PARITY: cbparity(hWndDlg, Msg, wParam, lParam); break; // case IDC_CB_PARITY case IDC_CB_STOPBITS: cbstopbits(hWndDlg, Msg, wParam, lParam); break; // case IDC_CB_STOPBITS case IDC_CB_FLOWCONTROL: cbflowcontrol(hWndDlg, Msg, wParam, lParam); break; // case IDC_CB_FLOWCONTROL case IDC_EDT_NUMBER: timerinterval(hWndDlg); break; // case IDC_EDT_NUMBER case IDC_BTN_OPEN: btnopen(hWndDlg); break; // case IDC_BTN_OPEN case IDC_BTN_CLOSE: btnclose(hWndDlg); break; // case IDC_BTN_CLOSE case IDC_BTN_CLEARSENT: btnclearsent(hWndDlg); break; // case IDC_BTN_CLEARSENT case IDC_BTN_CLEARMSENT: btnclearmsent(); break; // case IDC_BTN_CLEARMSENT case IDC_BTN_CLEARRECEIVED: btnclearreceived(); break; // case IDC_BTN_CLEARRECEIVED case IDC_BTN_RESET: btnreset(hWndDlg, Msg, wParam, lParam); break; // case IDC_BTN_RESET case IDC_BTN_EXIT: btnexit(hWndDlg); break; // case IDC_BTN_EXIT case IDM_SAVE: savedata(); break; // IDM_SAVE case IDM_EXIT: btnexit(hWndDlg); break; // IDM_EXIT case IDM_ABOUT: MessageBox(NULL, TEXT("Author: thanh nguyen \n Email: [email protected]"), TEXT("About"), MB_OK); break; // IDM_ABOUT case IDCANCEL: btnexit(hWndDlg); break; default: return FALSE; break; } } break; // WM_COMMAND case WM_TIMER: switch (wParam) { case ID_UPDATE_TIMER: infloop(hWndDlg); showdate(hWndDlg); break; // ID_UPDATE_TIMER case ID_TIMER: default: showdate(hWndDlg); break; // default } break; // WM_TIMER case WM_CLOSE: btnexit(hWndDlg); break; // case WM_CLOSE: default: return FALSE; break; // default } return TRUE; }