bool TTYPort::Open(const TCHAR *path, unsigned _baud_rate) { if (IsAndroid()) { /* attempt to give the XCSoar process permissions to access the USB serial adapter; this is mostly relevant to the Nook */ TCHAR command[MAX_PATH]; StringFormat(command, MAX_PATH, "su -c 'chmod 666 %s'", path); system(command); } if (!tty.OpenNonBlocking(path)) { LogErrno(_T("Failed to open port '%s'"), path); return false; } baud_rate = _baud_rate; if (!SetBaudrate(baud_rate)) return false; valid.store(true, std::memory_order_relaxed); io_thread->LockAdd(tty.ToFileDescriptor(), Poll::READ, *this); StateChanged(); return true; }
K6BtPort::K6BtPort(Port *_port, unsigned _baud_rate, DataHandler &_handler) :Port(_handler), port(_port), baud_rate(0) { /* ensure that the K6Bt is not in command mode */ SendCommand(NOP); SetBaudrate(_baud_rate); }
void SetControlReg(unsigned char volatile *UCCTL1,unsigned char volatile *UCBR0, unsigned char volatile *UCBR1,unsigned char volatile *UCMCTL, unsigned char volatile *UCIE,unsigned long Baudrate) { *UCCTL1 |= UCSSEL_2; // CLK = ACLK SetBaudrate(MSP430Clock,Baudrate,UCBR0,UCBR1,UCMCTL); *UCCTL1 &= ~UCSWRST; // **Initialize USCI state machine** *UCIE |= UCRXIE; // Enable USCI_A1 RX interrupt }
bool TTYPort::Open() { fd = open(sPortName, O_RDWR | O_NOCTTY); if (fd < 0) { return false; } SetBaudrate(baud_rate); return true; }
bool K6BtPort::WaitConnected(OperationEnvironment &env) { if (!port->WaitConnected(env)) return false; /* ensure that the K6Bt is not in command mode */ SendCommand(NOP); /* now that the Bluetooth connection has been established, tell our preferred baud rate to the K6Bt */ SetBaudrate(baud_rate); return true; }
bool TTYPort::Open(const TCHAR *path, unsigned _baud_rate) { fd = open(path, O_RDWR | O_NOCTTY | O_NONBLOCK); if (fd < 0) { LogErrno(_T("Failed to open port '%s'"), path); return false; } baud_rate = _baud_rate; if (!SetBaudrate(baud_rate)) return false; valid.Set(); return true; }
int SetPortAttr (int fd, int baudrate, int databit, const char *stopbit, char parity) { bzero (&termios_new, sizeof (termios_new)); cfmakeraw (&termios_new); SetBaudrate (baudrate); termios_new.c_cflag |= CLOCAL | CREAD; /* | CRTSCTS */ SetDataBit (databit); SetParityCheck (parity); SetStopBit (stopbit); termios_new.c_oflag = 0; termios_new.c_lflag |= 0; termios_new.c_oflag &= ~OPOST; termios_new.c_cc[VTIME] = 1; /* unit: 1/10 second. */ termios_new.c_cc[VMIN] = 1; /* minimal characters for reading */ tcflush (fd, TCIFLUSH); return (tcsetattr (fd, TCSANOW, &termios_new)); }
int OpenComm(char *sDeviceName, int newrate) { int nMode = O_RDWR | O_NOCTTY | O_NDELAY; int k, ch, tSave; #ifndef COMMS_TEST fdComm = open(sDeviceName, nMode); if (fdComm < 0) return fdComm; // Set the line to RAW SetBaudrate(newrate); // Set non-blocking fcntl(fdComm, F_SETFL, FNDELAY); // Try to overcome a bug with the Raspberry Pi (or indeed, any other serial // port that sends a garbage character when you first open it), // by sending out dummy characters until we get a NAK back, hopefully // then the display sequencer will be in a stable state. tSave = TimeLimit4D; TimeLimit4D = 500; for (k = 0 ; k < 10 ; k++) { ch = 'X'; write(fdComm, (unsigned char *)&ch, 1); tcflush(fdComm, TCOFLUSH); ReadSerPort((unsigned char *)&ch, 1); if (ch == 0x15) break ; } TimeLimit4D = tSave; tcflush(fdComm, TCIOFLUSH); #else // Quietly signal no device available fdComm = -1; Error4D = Err4D_OK; #endif return 0; }
void UART_Init(void) { // Set RS485 node to receiver mode PORTB.0 = 0; // Reset UART buffer and flag; rx_buffer_overflow = 0; tx_wr_index=0; tx_rd_index=0; tx_counter=0; //#ifndef _OLD_FASHION_CMD_PROCESS_ rx_wr_index=0; rx_rd_index=0; rx_counter=0; //#endif // USART initialization // Communication Parameters: 8 Data, 1 Stop, No Parity // USART Receiver: On // USART Transmitter: On // USART Mode: Asynchronous #asm("cli") UCSRA=0x00; // Enable TXC interrupt, RX interrupt, disable UDRE interrupt UCSRB=0xD8; // UCSRC share IO address with reg UBRR, MSB bit for UCSRC must be 1. // 8 data, 1 stop, even parity // UCSRC=0xA6; // EVEN parity bit UCSRC=0x86; // No parity bit // Set Baud rate based on System setting // RS485.BaudRate_Index can be read out from EEPROM // or set by RS485 master via RS485 communication // UCSRC share IO address with reg UBRR, MSB bit for UBRR must be 0. // don't enable interrupt here. // enable global interrupt in main function after all modules are initialized //RS485._flash.baudrate = 0; SetBaudrate(); }
struct sserial_props *OpenPort(const char *portName, int nBaudrate) { struct sserial_props *pProps; struct termios attr; pProps = (struct sserial_props *)malloc(sizeof(struct sserial_props)); if (pProps == NULL) { LOGE("[%s]: Allocating pProps failed with error \"%s\"", __func__, strerror(errno)); return NULL; } pthread_mutex_init(&pProps->mtx_ctrl, NULL); pthread_mutex_init(&pProps->mtx_rx, NULL); pthread_mutex_init(&pProps->mtx_tx, NULL); LOGI("Port name: %s Baudrate: %d", portName, nBaudrate); if ((pProps->fd = open(portName, O_RDWR | O_NOCTTY | O_NDELAY)) == -1) { LOGE("[%s]: open() failed with error \"%s\"", __func__, strerror(errno)); goto error_open; } fcntl(pProps->fd, F_SETFL, /*FNDELAY*/0); if (tcgetattr(pProps->fd, &pProps->otinfo) == -1) { LOGE("[%s]: tcgetattr() failed with error \"%s\"", __func__, strerror(errno)); goto error_init; } attr = pProps->otinfo; // Set baudrate cfsetospeed (&attr, (speed_t)B38400); cfsetispeed (&attr, (speed_t)B38400); attr.c_cflag &= ~PARENB; attr.c_cflag &= ~CSTOPB; attr.c_cflag &= ~CSIZE; attr.c_cflag &= ~CRTSCTS; attr.c_cflag |= CS8 | CREAD | CLOCAL; attr.c_oflag &= ~OPOST; attr.c_iflag &= ~(IXON | IXOFF | IXANY); attr.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); attr.c_cc[VMIN] = 0; attr.c_cc[VTIME] = 150; if (tcflush(pProps->fd, TCIOFLUSH) == -1) { LOGE("[%s]: tcflush() failed with error \"%s\"", __func__, strerror(errno)); goto error_init; } //cfmakeraw(&attr); if (tcsetattr(pProps->fd, TCSANOW, &attr) == -1) { LOGE("[%s]: tcsetattr() failed with error \"%s\"", __func__, strerror(errno)); goto error_init; } #if 0 if (!SetBaudrate(pProps, nBaudrate)) { LOGE("[%s]: Configuring baudrate failed", __func__); goto error_init; } #endif // Flush port I/O queue tcflush(pProps->fd,TCIOFLUSH); return pProps; error_init: if (close(pProps->fd)) { LOGE("[%s]: Closing port file descriptor (%d) failed with error \"%s\"", __func__, pProps->fd, strerror(errno)); } error_open: free(pProps); return NULL; }