int main(int argc, char *argv[]) { char uart_port[8]; unsigned i; bglib_output = output; for (i=0; i<100; ++i){ sprintf(uart_port, "COM%d", i); if (!uart_open(uart_port)) break; } // Reset dongle to get it into known state ble_cmd_system_reset(0); uart_close(); do { Sleep(500); // 0.5s } while (uart_open(uart_port)); // Execute action ble_cmd_gap_discover(gap_discover_observation); // Message loop while (state != state_finish) { if (read_message(UART_TIMEOUT) > 0) break; } uart_close(); return 0; }
bool pn532_uart_list_devices (nfc_device_desc_t pnddDevices[], size_t szDevices, size_t * pszDeviceFound) { /** @note: Due to UART bus we can't know if its really a pn532 without * sending some PN53x commands. But using this way to probe devices, we can * have serious problem with other device on this bus */ #ifndef SERIAL_AUTOPROBE_ENABLED (void) pnddDevices; (void) szDevices; *pszDeviceFound = 0; DBG ("%s", "Serial auto-probing have been disabled at compile time. Skipping autoprobe."); return false; #else /* SERIAL_AUTOPROBE_ENABLED */ *pszDeviceFound = 0; serial_port sp; const char *pcPorts[] = DEFAULT_SERIAL_PORTS; const char *pcPort; int iDevice = 0; while ((pcPort = pcPorts[iDevice++])) { sp = uart_open (pcPort); DBG ("Trying to find PN532 device on serial port: %s at %d bauds.", pcPort, SERIAL_DEFAULT_PORT_SPEED); if ((sp != INVALID_SERIAL_PORT) && (sp != CLAIMED_SERIAL_PORT)) { bool bComOk; // Serial port claimed but we need to check if a PN532_UART is connected. uart_set_speed (sp, SERIAL_DEFAULT_PORT_SPEED); // PN532 could be powered down, we need to wake it up before line testing. pn532_uart_wakeup ((nfc_device_spec_t) sp); // Check communication using "Diagnose" command, with "Communication test" (0x00) if (!pn532_uart_check_communication ((nfc_device_spec_t) sp, &bComOk)) continue; if (!bComOk) continue; uart_close (sp); snprintf (pnddDevices[*pszDeviceFound].acDevice, DEVICE_NAME_LENGTH - 1, "%s (%s)", "PN532", pcPort); pnddDevices[*pszDeviceFound].acDevice[DEVICE_NAME_LENGTH - 1] = '\0'; pnddDevices[*pszDeviceFound].pcDriver = PN532_UART_DRIVER_NAME; pnddDevices[*pszDeviceFound].pcPort = strdup (pcPort); pnddDevices[*pszDeviceFound].uiSpeed = SERIAL_DEFAULT_PORT_SPEED; DBG ("Device found: %s.", pnddDevices[*pszDeviceFound].acDevice); (*pszDeviceFound)++; // Test if we reach the maximum "wanted" devices if ((*pszDeviceFound) >= szDevices) break; } # ifdef DEBUG if (sp == INVALID_SERIAL_PORT) DBG ("Invalid serial port: %s", pcPort); if (sp == CLAIMED_SERIAL_PORT) DBG ("Serial port already claimed: %s", pcPort); # endif /* DEBUG */ } #endif /* SERIAL_AUTOPROBE_ENABLED */ return true; }
bool initUartOutput(flowOutputState state, char* port, unsigned int baud, size_t bufferSize) { // Initialize UART communication int uartErrno = uart_open(port, baud); if (uartErrno) { caerLog(CAER_LOG_ALERT, SUBSYSTEM_UART, "Failed to identify serial communication, errno=%i.",uartErrno); return (false); } // Test message char* testMessage = "DVS128UART"; if (uart_tx((int) strlen(testMessage), (unsigned char*) testMessage)) { caerLog(CAER_LOG_ALERT, SUBSYSTEM_UART, "Test transmission unsuccessful - connection closed."); uart_close(); return(false); } // Start output handler thread state->thread = 0; if (thrd_create(&state->thread, &outputHandlerThread, state) != thrd_success) { caerLog(CAER_LOG_ALERT, SUBSYSTEM_UART,"Failed to start output handler thread"); return (false); } state->buffer = ringBufferInit(bufferSize); if (state->buffer == NULL) { caerLog(CAER_LOG_ERROR, SUBSYSTEM_UART, "Failed to allocate transfer ring-buffer."); return (false); } atomic_store(&state->running, true); caerLog(CAER_LOG_NOTICE, SUBSYSTEM_UART, "Streaming flow events to port %s.",port); return (true); }
void rc522_uart_close(nfc_device * pnd) { rc522_powerdown(pnd); // Release UART port uart_close(DRIVER_DATA(pnd)->port); rc522_data_free(pnd); nfc_device_free(pnd); }
ER GPSRec_Close(void) { if (!bGPSRecOpened) return E_SYS; #if (RECEIVE_FROM_UART2) if (uart2_close() == E_OK) #else if (uart_close() == E_OK) #endif debug_msg(("UART2: close success\r\n")); else debug_err(("UART2: close fail!\r\n")); debug_msg("Terminate GPS receive task\r\n"); ter_tsk(GPSRECEIVE_ID); loc_cpu(); bGPSRecOpened = FALSE; unl_cpu(); return E_OK; }
serial_port uart_open(const char *pcPortName) { char acPortName[255]; struct serial_port_windows *sp = malloc(sizeof(struct serial_port_windows)); if (sp == 0) return INVALID_SERIAL_PORT; // Copy the input "com?" to "\\.\COM?" format sprintf(acPortName, "\\\\.\\%s", pcPortName); _strupr(acPortName); // Try to open the serial port sp->hPort = CreateFileA(acPortName, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL); if (sp->hPort == INVALID_HANDLE_VALUE) { uart_close(sp); return INVALID_SERIAL_PORT; } // Prepare the device control memset(&sp->dcb, 0, sizeof(DCB)); sp->dcb.DCBlength = sizeof(DCB); if (!BuildCommDCBA("baud=9600 data=8 parity=N stop=1", &sp->dcb)) { uart_close(sp); return INVALID_SERIAL_PORT; } // Update the active serial port if (!SetCommState(sp->hPort, &sp->dcb)) { uart_close(sp); return INVALID_SERIAL_PORT; } sp->ct.ReadIntervalTimeout = 30; sp->ct.ReadTotalTimeoutMultiplier = 0; sp->ct.ReadTotalTimeoutConstant = 30; sp->ct.WriteTotalTimeoutMultiplier = 30; sp->ct.WriteTotalTimeoutConstant = 0; if (!SetCommTimeouts(sp->hPort, &sp->ct)) { uart_close(sp); return INVALID_SERIAL_PORT; } PurgeComm(sp->hPort, PURGE_RXABORT | PURGE_RXCLEAR); return sp; }
void uart_socket_example(void *param) { char tx_data[] = {0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06}; uart_set_str uartset; struct timeval tv; fd_set readfds; int read_len = 0, count = 0; int ret = 0; char rxbuf[512]; int uart_fd; uart_socket_t *uart_socket = NULL; uartset.BaudRate = 9600; uartset.number = 8; uartset.StopBits = 0; uartset.FlowControl = 0; uartset.parity = 0; strcpy(uartset.UartName, "uart0"); uart_socket = uart_open(&uartset); if(uart_socket == NULL){ uart_printf("Init uart socket failed!\n"); goto Exit; } uart_fd = uart_socket->fd; uart_printf("\nOpen uart socket: %d\n", uart_fd); while(1) { FD_ZERO(&readfds); FD_SET(uart_fd, &readfds); tv.tv_sec = 0; tv.tv_usec = 20000; if(count++ == 50){ uart_write(uart_socket, tx_data, sizeof(tx_data)); //uart_print_data("TX:", tx_data, sizeof(tx_data)); count = 0; } ret = select(uart_fd + 1, &readfds, NULL, NULL, &tv); //uart_printf("[%d] select ret = %x count=%d\n", xTaskGetTickCount(), ret, count); if(ret > 0) { if(FD_ISSET(uart_fd, &readfds)) { read_len = uart_read(uart_socket, rxbuf, sizeof(rxbuf)); if(read_len > 0) { uart_print_data("RX:", rxbuf, read_len); if(rtl_strncmp(rxbuf, "close", 5) == 0) break; } } //else for other sockets } } uart_printf("Exit uart socket example!\n"); uart_close(uart_socket); Exit: vTaskDelete(NULL); }
void uart_start_h() { #define UART_SOCKET_STACK_SIZE 512 #define HAPNotifyHandle_STACK_SIZE 512*4 #define UART_SOCKET_PRIORITY 1 TaskHandle_t uart_socket_handle = NULL; TaskHandle_t HAPNotifyHandle_handle = NULL; const TickType_t xDelay = 10000; eTaskState TaskState; xSemaphoreHandle uart_socket_sema = NULL; xSemaphoreHandle HAPNotifyHandle_sema = NULL; struct tm timeinfo; //printf("==============[uart_start_h]rtc_isenabled : %d============\n", rtc_isenabled()); //vTaskDelay(xDelay*5); //printf("uart start\n"); vSemaphoreCreateBinary(uart_socket_sema); vSemaphoreCreateBinary(HAPNotifyHandle_sema); if(xTaskCreate(uart_socket_example, "uart_socket", UART_SOCKET_STACK_SIZE*2, &uart_socket_sema, UART_SOCKET_PRIORITY, &uart_socket_handle) != pdPASS) uart_printf("%s xTaskCreate failed", __FUNCTION__); if(xTaskCreate(HAPNotifyHandle, "HAPNotifyHandle", HAPNotifyHandle_STACK_SIZE, &HAPNotifyHandle_sema, UART_SOCKET_PRIORITY, &HAPNotifyHandle_handle) != pdPASS) uart_printf("%s xTaskCreate failed", __FUNCTION__); while(1) { vTaskDelay(xDelay); if (xSemaphoreTake(uart_socket_sema, ( TickType_t ) 2000) == pdTRUE) { //uart_printf("========uart_socket_right go on=========\n"); #if 0 read_locoltime(&timeinfo); printf("Time as a custom formatted string = %d-%d-%d %d:%d:%d wday:%d yday:%d\n", timeinfo.tm_year, timeinfo.tm_mon, timeinfo.tm_mday, timeinfo.tm_hour, timeinfo.tm_min,timeinfo.tm_sec,timeinfo.tm_wday,timeinfo.tm_yday); printf("==============rtc_isenabled : %d============\n", rtc_isenabled()); #endif } else { uart_printf("========uart_socket_wrong reset=========\n"); vTaskDelete(uart_socket_handle); uart_close(uart_socket); //uart_close(uart_socket_send); if(xTaskCreate(uart_socket_example, "uart_socket", UART_SOCKET_STACK_SIZE, &uart_socket_sema, UART_SOCKET_PRIORITY, &uart_socket_handle) != pdPASS) uart_printf("%s xTaskCreate failed", __FUNCTION__); } if (xSemaphoreTake(HAPNotifyHandle_sema, ( TickType_t ) 4000) != pdTRUE) { uart_printf("========HAPNotifyHandle reset=========\n"); vTaskDelete(HAPNotifyHandle_handle); if(xTaskCreate(HAPNotifyHandle, "HAPNotifyHandle", HAPNotifyHandle_STACK_SIZE, &HAPNotifyHandle_sema, UART_SOCKET_PRIORITY, &HAPNotifyHandle_handle) != pdPASS) uart_printf("%s xTaskCreate failed", __FUNCTION__); } } }
static void handle_close(const char *req, int *req_index) { (void) req; (void) req_index; if (uart_is_open(uart)) uart_close(uart); send_ok_response(); }
void closeUartOutput(flowOutputState state) { state->running = false; uart_close(); if ((errno = thrd_join(state->thread, NULL)) != thrd_success) { // This should never happen! caerLog(CAER_LOG_CRITICAL, SUBSYSTEM_UART, "Failed to join output handling thread. Error: %d.", errno); } ringBufferFree(state->buffer); }
int main(int argc, char** argv) { char* uart_port = NULL; if (argc == 2) uart_port = argv[1]; else { printf("Usage: %s <serial_port>\n\n", argv[0]); return 1; } bglib_output = output; if (uart_open(uart_port)) { printf("ERROR: Unable to open serial port\n"); return 1; } // Reset dongle to get it into known state ble_cmd_system_reset(0); uart_close(); do { usleep(500000); // 0.5s } while (uart_open(uart_port)); ble_cmd_gap_end_procedure(); ble_cmd_gap_discover(gap_discover_observation); while (1) { if (read_message(UART_TIMEOUT) > 0) break; } uart_close(); return 0; }
void main_cdc_close(uint8_t port) { switch(port){ case 0: // COMM0 uart_close(&USART_COMM0); // close COMM0, this also disables interrupts break; case 1: // Switch back to XBEE forwarding/programming, open COMM1 uart_open(&USART_COMM1); // restore communication with main processor on COMM1 usart_set_rx_interrupt_level(&USART_COMM1, USART_INT_LVL_HI); break; } }
static intptr_t uart_cls_por(intptr_t exinf) { SIOPCB *p_siopcb = (SIOPCB*)exinf; uart_close(&UART1); p_siopcb->openflag = false; ER ercd = dis_int(UART1_INT); assert(ercd == E_OK); return true; }
static int uart_diag_init(const struct diag *diag) { struct uart_diag *uart_diag = (struct uart_diag *) diag; uart_set_params(uart_diag->uart, uart_diag->params); /* XXX */ /* set params */ uart_open(uart_diag->uart); /* allow future `open' */ uart_close(uart_diag->uart); return 0; };
void main_cdc_open(uint8_t port) { switch(port){ case 0: // COMM0 lithneProgrammer.resetMain(); uart_open(&USART_COMM0); usart_set_rx_interrupt_level(&USART_COMM0, USART_INT_LVL_HI); // receive interrupt break; case 1: // Switched to XBEE direct: close COMM1 uart_close(&USART_COMM1); // close COMM1, this also disables interrupts usart_set_rx_interrupt_level(&USART_XBEE, USART_INT_LVL_HI); break; } }
void uart_test2(void) { int ret; char buf[1024]; int len; int total_len = 0; volatile int j; /* uart0 tx <---> uart 3 rx * to test baudrate is right */ dprintf("capture from uart0 start"); for(j =0 ; j<2000; j++) ; uart_close(3); ret = uart_open(3,115200,8,'n',1); /* send to uart 3 */ dprintf("abcdefghijklmnopq1234325454545\r\n"); for(j =0 ; j<2000; j++) ; memset(buf,0,sizeof(buf)); while((len = uart_rx(3, buf + total_len, 1024 - total_len))) { total_len += len; } uart_close(3); dprintf("%s",buf); dprintf("capture stop"); for(;;) ; }
static void arygon_close_step2(nfc_device *pnd) { // Release UART port uart_close(DRIVER_DATA(pnd)->port); #ifndef WIN32 // Release file descriptors used for abort mecanism close(DRIVER_DATA(pnd)->iAbortFds[0]); close(DRIVER_DATA(pnd)->iAbortFds[1]); #endif pn53x_data_free(pnd); nfc_device_free(pnd); }
int main(void) { volatile char inputBuffer[8]; char foo; uart_open(9600, inputBuffer, 8); sei(); while(1) { if (uart_read(&foo)){ uart_write(&foo,1); } } uart_close(); return 1; }
void uart_init(int fd) { //Setting the uart device parameter printf("Setting the uart device parameter\n"); struct termios serialAttr; memset(&serialAttr, 0, sizeof serialAttr); serialAttr.c_iflag = IGNPAR; serialAttr.c_cflag = B9600 | HUPCL | CS8 | CREAD | CLOCAL; serialAttr.c_cc[VMIN] = 1; if (tcsetattr(fd, TCSANOW, &serialAttr)) { printf("tcsetattr fail !\n"); uart_close(fd); exit(1); } printf("uart initial is OK\n"); }
static be_jse_symbol_t * uart_module_handle_cb(be_jse_vm_ctx_t *execInfo, be_jse_symbol_t *var, const char *name){ if(0 == strcmp(name,"open")){ return uart_open(); } if(0 == strcmp(name,"read")){ return uart_read(); } if(0 == strcmp(name,"write")){ return uart_write(); } if(0 == strcmp(name,"on")){ return uart_watch(); } if(0 == strcmp(name,"close")){ return uart_close(); } return (BE_JSE_FUNC_UNHANDLED); }
void uart_test3(void) { int ret; char buf[1024]; int i; volatile int j; for(i = 0; i<sizeof(buf); i++) buf[i] = i & 0xff; /* uart3 tx test */ ret = uart_open(3,115200,8,'n',1); dprintf("open uart 3 ret:%d\r\n", ret); for(;;) { uart_tx_str(3, "AT\r"); while(!uart_tx_finish(3)) ; while(!(ret = uart_rx(3, buf, sizeof(buf)))) ; if(ret < 0) { RERR(); continue; } j = 1000; while(j--) ; ret += uart_rx(3, buf + ret, sizeof(buf)); if(ret > 0) { buf[ret] = 0; dprintf("ret:%d buf:%s\r\n",ret, buf); } else { dprintf("uart rx 3 ret:%d\r\n",ret); } } uart_close(3); }
int main(int argc, char* argv[]) { csInit (); spiInit(SPI1); mySPI_SendData(Ctrl_Reg1, 0x40); //0x40 = 0100000, 0xC0 = 11000000 = 400 Hz output data rate), active uart_open(myUSART, 9600, 0); uint8_t ri; cmdi = 0; timInit(); // Infinite loop while (1) { } uart_close(myUSART); }
/* Clears the read buffer. */ void uart_flush_read(uart_s* uart){ uint8_t buffer[64]; int nbytes=1; if (!(uart->opened)){ //Let's do that first. if (!uart_open(uart)){ return; } } struct timeval timeout; // initialise the timeout structure timeout.tv_sec = 0; timeout.tv_usec = 1000; // Update File Descriptor so that it's correct as the state of the program changes. while(1){ fd_set readfds; //Do i want this in a uart_object? Or just leave it? FD_ZERO(&readfds); //Clear the set of file_descriptors. FD_SET(uart->fd, &readfds); //Add 'tty_fd' to 'readfds' set of file_descriptors. int n = select(uart->fd + 1, &readfds, NULL, NULL, &timeout); if(n>0){ nbytes = read(uart->fd,buffer,64); //This starts writing at bufferout[0]. uart_printf(CLR_CANCEL,0,"Number of bytes cleared: %i\n",nbytes); if (nbytes == 0){ //This can happen when the port becomes unavailable. uart_printf(CLR_CANCEL,0,"Port disconnected\n"); uart_close(uart); return; } }else{ //Let me know. //scc_printf(SRC_TCPSERV|LEVEL_WARNING,0,"No bytes where cleared.\n"); return; } } }
/* * Handle {name, kv_list} * * name is the serial port name * kv_list a list of configuration values (speed, parity, etc.) */ static void handle_open(const char *req, int *req_index) { int term_type; int term_size; if (ei_decode_tuple_header(req, req_index, &term_size) < 0 || term_size != 2) errx(EXIT_FAILURE, ":open requires a 2-tuple"); char name[32]; long binary_len; if (ei_get_type(req, req_index, &term_type, &term_size) < 0 || term_type != ERL_BINARY_EXT || term_size >= (int) sizeof(name) || ei_decode_binary(req, req_index, name, &binary_len) < 0) { // The name is almost certainly too long, so report that it // doesn't exist. send_error_response("enoent"); return; } name[term_size] = '\0'; struct uart_config config = current_config; if (parse_option_list(req, req_index, &config) < 0) { send_error_response("einval"); return; } // If the uart was already open, close and open it again if (uart_is_open(uart)) uart_close(uart); if (uart_open(uart, name, &config) >= 0) { current_config = config; send_ok_response(); } else { send_error_response(uart_last_error()); } }
int main(void) { uart_init(); q1 = init_robocol_queue(8); q2 = init_robocol_queue(8); q3 = init_robocol_queue(8); enqueue(q1, 5); display_queue(q1); while(1){ } /* command* c = init_command("ZAS", "PWD", ""); display_command(c); destroy_command(c); display_command(c); */ //float list[] = {1.0, 2.0, 3.0}; int val = 0; //printf("%s\n", format_string("name", list, sizeof(list) / sizeof(list[0]))); //format_string("name", list, sizeof(list) / sizeof(list[0]), &val); printf("%s\n", get_command_response("SEN", &val)); uart_close(); }
/*------------------------------------------- | Name:dev_stm32f4xx_uart_x_close | Description: | Parameters: | Return Type: | Comments: | See: ---------------------------------------------*/ int dev_stm32f4xx_uart_x_close(desc_t desc){ board_stm32f4xx_uart_info_t * p_uart_info = (board_stm32f4xx_uart_info_t*)ofile_lst[desc].p; // if(!p_uart_info) return -1; // if(ofile_lst[desc].oflag & O_RDONLY) { if(!ofile_lst[desc].nb_reader) { p_uart_info->desc_r = -1; } } // if(ofile_lst[desc].oflag & O_WRONLY) { if(!ofile_lst[desc].nb_writer) { p_uart_info->desc_w = -1; } } // if(p_uart_info->desc_r<0 && p_uart_info->desc_w<0) { uart_close(&p_uart_info->uart_descriptor); } return 0; }
void main_cdc_disable(uint8_t port) { main_b_cdc_enable = false; // Close communication uart_close(port); }
static int dev_uart_close(struct file_desc *desc) { struct uart *uart_dev = desc->file_info; return uart_close(uart_dev); }
void CloseProxmark() { // Clean up the port uart_close(sp); // Fix for linux, it seems that it is extremely slow to release the serial port file descriptor /dev/* unlink(serial_port_name); }
static size_t pn532_uart_scan(const nfc_context *context, nfc_connstring connstrings[], const size_t connstrings_len) { size_t device_found = 0; serial_port sp; char **acPorts = uart_list_ports(); const char *acPort; int iDevice = 0; while ((acPort = acPorts[iDevice++])) { sp = uart_open(acPort); log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "Trying to find PN532 device on serial port: %s at %d bauds.", acPort, PN532_UART_DEFAULT_SPEED); if ((sp != INVALID_SERIAL_PORT) && (sp != CLAIMED_SERIAL_PORT)) { // We need to flush input to be sure first reply does not comes from older byte transceive uart_flush_input(sp); // Serial port claimed but we need to check if a PN532_UART is opened. uart_set_speed(sp, PN532_UART_DEFAULT_SPEED); nfc_connstring connstring; snprintf(connstring, sizeof(nfc_connstring), "%s:%s:%"PRIu32, PN532_UART_DRIVER_NAME, acPort, PN532_UART_DEFAULT_SPEED); nfc_device *pnd = nfc_device_new(context, connstring); pnd->driver = &pn532_uart_driver; pnd->driver_data = malloc(sizeof(struct pn532_uart_data)); DRIVER_DATA(pnd)->port = sp; // Alloc and init chip's data pn53x_data_new(pnd, &pn532_uart_io); // SAMConfiguration command if needed to wakeup the chip and pn53x_SAMConfiguration check if the chip is a PN532 CHIP_DATA(pnd)->type = PN532; // This device starts in LowVBat power mode CHIP_DATA(pnd)->power_mode = LOWVBAT; #ifndef WIN32 // pipe-based abort mecanism if (pipe(DRIVER_DATA(pnd)->iAbortFds) < 0) { return 0; } #else DRIVER_DATA(pnd)->abort_flag = false; #endif // Check communication using "Diagnose" command, with "Communication test" (0x00) int res = pn53x_check_communication(pnd); pn53x_data_free(pnd); nfc_device_free(pnd); uart_close(sp); if (res < 0) { continue; } memcpy(connstrings[device_found], connstring, sizeof(nfc_connstring)); device_found++; // Test if we reach the maximum "wanted" devices if (device_found >= connstrings_len) break; } } iDevice = 0; while ((acPort = acPorts[iDevice++])) { free((void *)acPort); } free(acPorts); return device_found; }