int main(void) { int fd; char buff[BUFFER_SIZE]; if ((fd = open_port(HOST_COM_PORT)) < 0) { /* 打开串口 */ perror("open_port"); return 1; } if (set_com_config(fd, 115200, 8, 'N', 1) < 0) { /* 配置串口 */ perror("set_com_config"); return 1; } do { printf("Input some words(enter 'quit' to exit):"); memset(buff, 0, BUFFER_SIZE); if (fgets(buff, BUFFER_SIZE, stdin) == NULL) { perror("fgets"); break; } write(fd, buff, strlen(buff)); } while (strncmp(buff, "quit", 4)); close(fd); return 0; }
int main(void) { int fd; char buff[BUFFER_SIZE]; if ((fd = open_port(TARGET_COM_PORT)) < 0) { /* ´ò¿ª´®¿Ú */ perror("open_port"); return 1; } if (set_com_config(fd, 115200, 8, 'N', 1) < 0) { /* ÉèÖô®¿Ú */ perror("set_com_config"); return 1; } do { memset(buff, 0, BUFFER_SIZE); if (read(fd, buff, BUFFER_SIZE) > 0) { printf("The received words are : %s", buff); } } while (strncmp(buff, "quit", 4)); close(fd); return 0; }
/* * Class: com_advantech_advnfc_AdvNFC * Method: readCard * Signature: ()Ljava/lang/String; */ JNIEXPORT jstring JNICALL Java_com_advantech_advnfc_AdvNFC_readCard (JNIEnv *env, jobject thiz) { int i, fd; unsigned char buff[BUFFER_SIZE]; unsigned char readData[DATACOUNT]; if((fd = open_port(UART_PORT)) < 0) { ALOGE("open_port error"); //return -1; } if(set_com_config(fd, 9600, 8, 'N', 1) < 0) { ALOGE("set_com_config error"); //return -1; } write(fd, getCardSnrCmd, (sizeof(getCardSnrCmd) / sizeof(unsigned char))); write(fd, authCmd, (sizeof(authCmd) / sizeof(unsigned char))); write(fd, readDataCmd, (sizeof(authCmd) / sizeof(unsigned char))); for (i = 0; i < DATACOUNT; i++) { if (read(fd, buff, BUFFER_SIZE) > 0) { readData[i] = buff[i]; ALOGE("readData[%d]=%x\n", i, readData[i]); memset(buff, 0, BUFFER_SIZE); } } close(fd); }
/* * Class: com_advantech_advnfc_AdvNFC * Method: loadKey:将密匙0xffffffffffff装载到模块内第0区的A密匙区,该命令仅需调用一次就行了 * 自动保存在模块EEPROM里,若高强度刷写,容易出现EEPROM被写坏的问题。 * Signature: ()Z */ JNIEXPORT jboolean JNICALL Java_com_advantech_advnfc_AdvNFC_loadKey (JNIEnv *env, jobject thiz) { int i, fd; unsigned char buff[BUFFER_SIZE]; if((fd = open_port(UART_PORT)) < 0) { ALOGE("open_port error"); //return -1; } if(set_com_config(fd, 9600, 8, 'N', 1) < 0) { ALOGE("set_com_config error"); //return -1; } write(fd, loadKeyCmd, (sizeof(loadKeyCmd) / sizeof(unsigned char))); for (i = 0; i < (sizeof(loadKeyRtn) / sizeof(unsigned char)); i++) { if(read(fd, buff, BUFFER_SIZE) > 0){ if(buff[0] != loadKeyRtn[i]){ close(fd); return (jboolean) -1; } memset(buff, 0, BUFFER_SIZE); } } close(fd); return (jboolean) 1; }
int main(int argc,char*argv[]) { int fd=0; int HOST_COM_PORT=1; fd=open_port(HOST_COM_PORT); if(fd<0) { perror("open fail!"); } printf("open sucess!\n"); if((set_com_config(fd,4800,8,'N',1))<0) { perror("set_com_config fail!\n"); } printf("The received worlds are:\n"); read_data(fd); return 0; }
int init_uart() { int fd; char buff[BUFFER_SIZE]; unsigned char cardtype[2]; unsigned char databuf[16]; if((fd = open_port(UART_PORT)) < 0) { ALOGE("open_port error"); return -1; } if(set_com_config(fd, 9600, 8, 'N', 1) < 0) { ALOGE("set_com_config error"); return -1; } close(fd); return 1; }
int main() { int fd = 0; struct termios oldtio,newtio; int return_val = 0; char ch; fd = open_tty(); set_com_config(fd, BAUDRATE, 8, 'N', 1); while(1) { return_val = read(fd, buffer, 1000); if(return_val > 0) { printf("%s", buffer); } } // write(fd, buffer2, sizeof(buffer2)); //close dev tty. close(fd); return 0; }
//int main(void) int uart_thread(void *arg) { int serial_fd, recv_fd, tran_fd; fd_set inset, tmp_inset; struct timeval tv; unsigned loop = 1; int real_read, i, real_write, data_len; int rsp_msg, rsp_counter, write_len = 0; static int step = 0; unsigned char rcv_buf[BUFFER_SIZE]; unsigned char snd_buf[BUFFER_SIZE]; unsigned char data_t; unsigned char errvalue; int rcv_trans = 0; int ret; /*The serial port data written to this file*/ /*if ((recv_fd = open(RECV_FILE_NAME, O_CREAT|O_WRONLY, 0644)) < 0){ perror("open"); printf("\n\n\nzorro, can't open recv_file: %s\n", strerror(errno)); return 1; } printf("the recv_file fd = %d,\n", recv_fd); */ /*The transmit data written to this file*/ /*if ((tran_fd = open(TRAN_FILE_NAME, O_CREAT|O_WRONLY, 0644)) < 0){ perror("open"); printf("\n\n\nzorro, can't open tran_file: %s\n", strerror(errno)); return 1; } printf("the tran_file fd = %d,\n", tran_fd); */ //fds[0] = STDIN_FILENO; /*The standard input*/ if ((serial_fd = open_port(TARGET_COM_PORT)) < 0){ perror("open_port"); printf("\n\n\nzorro, can't open target com port: %s\n", strerror(errno)); return 1; } printf("the serial_file fd = %d,\n", serial_fd); /*config com*/ if (set_com_config(serial_fd, 115200, 8, 'N', 1) < 0){ perror("set_com_config"); printf("\n\n\nzorro, can't set com fonfig: %s\n", strerror(errno)); return 1; } printf("Input some words(enter 'quit' to exit):\n"); while (1) { if (!rcv_trans){ /*rcv uart data */ /*read frame head from serial port*/ real_read = read(serial_fd, &data_t, 1); if ((real_read <= 0) && (errno != EAGAIN)){ printf("\nzorro, ERROR:READ FRAME HEAD error1 real_read = %d, \n", real_read); } switch (step){ case 0x00: //DA_DECODE_SYN_HEAD if (data_t == 0xAA){ rcv_buf[0] = data_t; step=1; } break; case 0x01: //DA_DECODE_GET_SYN_COUNTER if (data_t == 0xAA) step = 0; else{ rcv_buf[1] = data_t; step = 2; } break; case 0x02: //DA_DECODE_GET_DATA_LENGTH if ((data_t < 2) ||(data_t > 40)) step = 0; else{ data_len = rcv_buf[2] = data_t; step = 3; i = 0; } break; case 0x03: //DA_DECODE_GET_DATA and DA_DECODE_CHECK rcv_buf[3+i++] = data_t; if (i == data_len){ step = 0; //reset the unpack step int check; check = check_frame_sum(rcv_buf, (data_len+2)); if (!check){ if (msg_unpack(rcv_buf,&rsp_msg, &errvalue) < 0){ printf("zorro, ERROR:cmd unpack err\n"); } else{ rcv_trans = 1; } //rcv_buf[data_len+3] = '\0'; /*write the data to commen file*/ //write(recv_fd, rcv_buf, data_len+4); } else if (check < 0){ printf("\nzorro, ERROR:check sum error \n"); } } break; } } else{ /*transmit uart data */ ret = msg_pack(snd_buf,rsp_msg, rsp_counter, errvalue, &write_len); /*ret > 0, send this frame and continue ; ret = 0, send this frame and , ret < 0, dont send this frame */ if (ret <= 0){ rcv_trans = 0; if (ret < 0) continue; } //snd_buf[write_len] = '\0'; /*write the data to commen file*/ //write(tran_fd, snd_buf, write_len+1); /*read frame head from serial port*/ real_write = write(serial_fd, &snd_buf, write_len); if ((real_write <= 0) && (errno != EAGAIN)){ printf("\nzorro, ERROR:write cmd error1 real_write = %d, \n", real_write); } if ((write_len - real_write) != 0) printf("error!\n"); else rsp_counter ++; } } }