int main(int argc, char **argv) { if (argc == 2) { port_name = argv[1]; } //printf("Using %s as serial device.\n", port_name); char ttybuf[255]; tty_fd = open_tty(); if (tty_fd < 0) { //printf("Error opening terminal.\n"); return (1); } install_sighandler(); if (init() < 0) { //printf("Cannot open port.\r\n"); exit(1); } while (1) { int n = read(tty_fd, ttybuf, sizeof(ttybuf)); int i; /* check for 0x3 (ctrl-c), clean exit */ for (i = 0; i < n; i++) { if (ttybuf[i] == 0x3) { if (i > 0) { write_serial_port(ttybuf, i); } close_serial_port(); close_tty(); system("tset -c"); return 0; } } write_serial_port(ttybuf, n); } close_tty(); close_serial_port(); return 0; }
void serial_test() { int a =20, b, ret, fd; unsigned char buffer[100]; if((fd = serial_init(0)) < 0) { perror("serial init failed\r\n"); } log_printf("serial fd = %d \r\n", fd); while(1) { log_printf("serial\r\n"); if ((ret = write_serial_port(fd,TEST_STRING, sizeof(TEST_STRING))) < 0) { log_printf("com error\r\n"); } else { sleep(1); log_printf("number data = %d\r\n", ret); } } }
void *serial_thread(void *arg) { int fd, nread, count, result ,iread; fd_set inputs,outputs; struct timeval serial_timeout; unsigned char buffer[100]; serial_timeout.tv_sec = 1; serial_timeout.tv_usec = 0; if((fd = serial_init(0)) < 0) { perror("serial init failed\r\n"); } FD_ZERO(&inputs); FD_SET(fd, &inputs); FD_ZERO(&outputs); FD_SET(fd, &outputs); for(;;) { result = select(fd + 1, &inputs, NULL , (fd_set *)NULL, &serial_timeout); switch(result) { case 0: //time out FD_ZERO(&inputs); FD_SET(fd, &inputs); serial_timeout.tv_sec = 10; serial_timeout.tv_usec = 0; perror("com select timeout\r\n"); break; case -1: //error return result; default: if(FD_ISSET(fd,&inputs)) { ioctl(fd, FIONREAD, &iread); if(iread == 0) { continue; } nread = read(fd, buffer, sizeof(buffer)); buffer[nread] = 0; log_printf("%s", buffer); }else if(FD_ISSET(fd,&outputs)){/*this meeans serial buffer is writravke*/ log_printf("serial writable\r\n"); if ((write_serial_port(fd,TEST_STRING, sizeof(TEST_STRING))) < 0) { log_printf("com write error\r\n"); } }else { assert(-1); } }//end of switch } }
static int device_request(size_t size) { int result; device_buffer[size] = device_checksum(device_buffer, size); if ((result = write_serial_port(device_buffer, size + 1))) return result; if ((result = read_serial_port(device_buffer, 1))) return result; return device_buffer[0] == 0x79 ? DONE : INVALID_DEVICE_REPLY; }
void do_term_input(gpointer data, int fd, GdkInputCondition cond) { char buf[256]; int num, flags; flags = fcntl(term_fd, F_GETFL); fcntl(term_fd, F_SETFL, flags | O_NONBLOCK); num = read(term_fd, buf, sizeof(buf)); fcntl(term_fd, F_SETFL, flags); if (num > 0) { if (download_in_progress) { download_rx_term(buf, num); } else { write_serial_port(buf, num); } } }
static int try_to_handshake_device(void) { int result; device_buffer[0] = 0x7F; if ((result = wait_serial_port(5))) return result; if ((result = flush_serial_port())) return result; if ((result = write_serial_port(&device_buffer, 1))) return result; if ((result = read_serial_port(&device_buffer, 1))) return result; return device_buffer[0] == 0x79 ? DONE : INVALID_DEVICE_REPLY; }
int writepacket(uint8_t *packet_buf, size_t size) { uint8_t packet_tmp[2 * BUFFER_SIZE]; uint8_t *byte_ptr = packet_buf; uint8_t *tmp_ptr = packet_tmp; if (2 * size + 1 > BUFFER_SIZE) { return -1; } while ((byte_ptr - packet_buf) < size) { switch(*byte_ptr) { case (END): { *byte_ptr = END_ESC; *tmp_ptr = ESC; tmp_ptr++; break; } case (ESC): { *byte_ptr = ESC_ESC; *tmp_ptr = ESC; tmp_ptr++; break; } default: { break; } } *tmp_ptr = *byte_ptr; byte_ptr++; tmp_ptr++; } *tmp_ptr++ = END; write_serial_port(packet_tmp, tmp_ptr - packet_tmp); return 0; }
int main(void) { int i; int reading1; int reading2; int address; int test_array[100]; for(i=0;i<100;i++) { motor(0,i); //spin the left motor forward motor(1,i); //spin the right motor forward } i=0; while(i>-100) { motor(0,i); //spin the left motor backwards motor(1,i); //spin the right motor backwards i--; } i=50; set_servo(0,i); //set servo motor 0 to move to 50 degrees set_servo(3,i); //set servo motor 3 to move to 50 degrees delay_milliseconds(100); //pause 100 milliseconds delay_seconds(1); //pause 1 second lcd_clear(); lcd_cursor(0,0); printf ("Test1\n"); //the LCD will be 8x2 (8chars x 2lines) printf ("Test2\n"); reading1 = analog(0); //get a reading from analog pin 0 reading2 = analog(5); //get a reading from analog pin 5 reading1 = digital(0); //get a reading from digital pin 0 reading2 = digital(1); //get a reading from digital pin 1 if (reading1 > 100) { printf ("%d\n", reading1); } reading1 = accelerometer(0); //read x-axis reading2 = accelerometer(1); //read y-axis reading1 = accelerometer(2); //read z-axis reading1 = battery_voltage(); //battery voltage reading1 = read_serial_port(); //get a byte from the serial port write_serial_port(reading1); //send a byte on the serial port led1(1); //turn on on-board led1 led1(0); //turn off on-board led1 reading1 = read_ir(); //get a reading from the IR receiver reset(); //reset the board write_eeprom(address, reading1); //write a value to the non-volatile eeprom (these values will be stored across resets) reading1 = read_eeprom(address); //get a reading from the non-volatile eeprom reading1 = button(); //read the state of the on-board button return 0; }