ssize_t gpsd_write(struct gps_device_t * session, const char *buf, size_t len) { ssize_t status; bool ok; if (session == NULL || session->context == NULL || session->context->readonly) return 0; status = write(session->gpsdata.gps_fd, buf, len); ok = (status == (ssize_t) len); (void)tcdrain(session->gpsdata.gps_fd); /* extra guard prevents expensive hexdump calls */ if (session->context->debug >= LOG_IO) gpsd_report(LOG_IO, "=> GPS: %s%s\n", gpsd_packetdump((char *)buf, len), ok ? "" : " FAILED"); return status; }
void serialport_flush(void) { static unsigned char b; unsigned int t; if(serial_port) { t = serialport_get_character_timeout_0_1s(); serialport_set_character_timeout_0_1s(0); tcdrain(serial_port); while(read(serial_port, &b, 1) > 0); serialport_set_character_timeout_0_1s(t); } }
int gecko_write(const void *buf, size_t count) { size_t left, chunk; #ifndef __WIN32__ size_t res; #else DWORD res; #endif left = count; while (left) { chunk = left; if (chunk > FTDI_PACKET_SIZE) chunk = FTDI_PACKET_SIZE; #ifdef USE_LIBFTDI if (gUsingFTDI) res = ftdi_write_data(&gFTDIContext, (unsigned char *)buf, chunk); else #endif #ifndef __WIN32__ res = write(fd_gecko, buf, count); if (res < 1) { perror("gecko_write"); return 1; } #else if (!WriteFile(handle_gecko, buf, chunk, &res, NULL)) { fprintf (stderr, "gecko_write\n"); return 1; } #endif left -= res; buf += res; #if !defined(__WIN32__) && !defined(USE_LIBFTDI) // does this work with ftdi-sio? if (tcdrain(fd_gecko)) { perror ("gecko_drain"); return 1; } #endif } return 0; }
void prmesg(int length) { FILE *outf; char *env_pager; if (use_pager && length > Lpp) { signal(SIGPIPE, SIG_IGN); signal(SIGQUIT, SIG_IGN); if ((env_pager = getenv("PAGER")) == NULL) { snprintf(cmdbuf, sizeof(cmdbuf), _PATH_PAGER, Lpp); } else { snprintf(cmdbuf, sizeof(cmdbuf), "%s", env_pager); } outf = popen(cmdbuf, "w"); if (!outf) outf = stdout; else setbuf(outf, NULL); } else outf = stdout; if (seensubj) putc('\n', outf); while (fgets(inbuf, sizeof inbuf, newmsg)) { fputs(inbuf, outf); if (ferror(outf)) { clearerr(outf); break; } } if (outf != stdout) { pclose(outf); signal(SIGPIPE, SIG_DFL); signal(SIGQUIT, SIG_DFL); } else { fflush(stdout); } /* force wait on output */ tcdrain(fileno(stdout)); }
void RS232Interface::WaitForTxEmpty() { #ifdef _WINDOWS DWORD evento; if ( hCom != INVALID_HANDLE_VALUE) { do { WaitCommEvent(hCom, &evento, NULL); } while ( !(evento & EV_TXEMPTY) ); } #endif #ifdef _LINUX_ if ( fd != INVALID_HANDLE_VALUE ) tcdrain(fd); #endif }
void gpsd_close(struct gps_device_t *session) { if (session->gpsdata.gps_fd != -1) { (void)ioctl(session->gpsdata.gps_fd, (unsigned long)TIOCNXCL); (void)tcdrain(session->gpsdata.gps_fd); if (isatty(session->gpsdata.gps_fd) != 0) { /* force hangup on close on systems that don't do HUPCL properly */ /*@ ignore @*/ (void)cfsetispeed(&session->ttyset, (speed_t) B0); (void)cfsetospeed(&session->ttyset, (speed_t) B0); /*@ end @*/ (void)tcsetattr(session->gpsdata.gps_fd, TCSANOW, &session->ttyset); } /* this is the clean way to do it */ session->ttyset_old.c_cflag |= HUPCL; /* keep the most recent baud rate */ /* * Don't revert the serial parameters if we didn't have to mess with * them the first time. Economical, and avoids tripping over an * obscure Linux 2.6 kernel bug that disables threaded * ioctl(TIOCMWAIT) on a device after tcsetattr() is called. * * Unfortunately the termios struct doesn't have c_ispeed/c_ospeed * on all architectures. Its missing on sparc, mips/mispel and hurd-i386 at least. */ #if defined(_HAVE_STRUCT_TERMIOS_C_ISPEED) if (session->ttyset_old.c_ispeed != session->ttyset.c_ispeed || (session->ttyset_old.c_cflag & CSTOPB) != (session->ttyset.c_cflag & CSTOPB)) { #endif /*@ ignore @*/ (void)cfsetispeed(&session->ttyset_old, (speed_t) session->gpsdata.dev.baudrate); (void)cfsetospeed(&session->ttyset_old, (speed_t) session->gpsdata.dev.baudrate); /*@ end @*/ (void)tcsetattr(session->gpsdata.gps_fd, TCSANOW, &session->ttyset_old); #if defined(_HAVE_STRUCT_TERMIOS_C_ISPEED) } #endif gpsd_report(LOG_SPIN, "close(%d) in gpsd_close(%s)\n", session->gpsdata.gps_fd, session->gpsdata.dev.path); (void)close(session->gpsdata.gps_fd); session->gpsdata.gps_fd = -1; } }
// ------------------------------------------------------------------------------ // Write Port with Lock // ------------------------------------------------------------------------------ void _write_port(char *buf, unsigned len) { // Lock pthread_mutex_lock(&lock); // Write packet via serial link write(fd, buf, len); // Wait until all data has been written tcdrain(fd); // Unlock pthread_mutex_unlock(&lock); return; }
int main(void) { int n1, n2, fd; if((fd=open("/dev/ttyS0", O_RDWR|O_NOCTTY|O_NONBLOCK))== -1){ fprintf(stderr, "Open port: Unable to open, %s\n",strerror(errno)); exit(EXIT_FAILURE); } write(fd, "Example of line control functions ", 34); tcdrain(fd); /* 等待輸出佇列中的資料全部送出 */ tcflow(fd, TCOOFF); /* 懸掛輸出傳輸 */ n1 = write(fd, "this line will be thrown over\n", 30); tcflush(fd, TCOFLUSH); /* 清除輸出佇列 */ n2 = write(fd, "this line will not be thrown over\n", 34); tcflow(fd, TCOON); /* 還原輸出傳輸 */ write(fd,"restart the output\n",19); exit(EXIT_SUCCESS); }
/** * @brief Update all information by bust read * @retval 0 Success * @retval -1 Failed * * - See burst read function at pp.14 * - Data resolution is 16 bit */ int Adis16470::update_burst(void) { unsigned char buff[64] = {0}; // 0x6800: Burst read function buff[0] = 0x61; buff[1] = 0x68; buff[2] = 0x00; int size = write(fd_, buff, 24); if (size != 24) { perror("update_burst"); return -1; } if (tcdrain(fd_) < 0) { perror("update_burst"); return -1; } size = read(fd_, buff, 30); if (size != 30) { perror("update_burst"); return -1; } int16_t diag_stat = big_endian_to_short(&buff[3]); if (diag_stat != 0) { fprintf(stderr, "diag_stat error: %04x\n", (uint16_t)diag_stat); return -1; } // X_GYRO_OUT gyro[0] = big_endian_to_short(&buff[5]) * M_PI / 180 / 10.0; // Y_GYRO_OUT gyro[1] = big_endian_to_short(&buff[7]) * M_PI / 180 / 10.0; // Z_GYRO_OUT gyro[2] = big_endian_to_short(&buff[9]) * M_PI / 180 / 10.0; // X_ACCL_OUT accl[0] = big_endian_to_short(&buff[11]) * M_PI / 180 / 10.0; // Y_ACCL_OUT accl[1] = big_endian_to_short(&buff[13]) * M_PI / 180 / 10.0; // Z_ACCL_OUT accl[2] = big_endian_to_short(&buff[15]) * M_PI / 180 / 10.0; return 0; }
int write_drain( int fd, const unsigned char *data, unsigned long length ) { int i, result, block; if ( length == 0 ) { return; } tcflush( fd, TCIFLUSH ); for ( result = 1, i = 0 ; result > 0 && i < length ; ) { if ( (length - i) > progress_block ) { block = progress_block; } else { block = length - i; } result = write( fd, data + i, block ); tcdrain( fd ); if ( result >= 0 ) { i += result; } else { perror( "Error writing to port\n" ); } if ( progress ) { printf( "." ); fflush( stdout ); } } if (dump_comms) { if ( progress ) { printf( "\n" ); } printf( "Sent:\n" ); dump( data, length ); } }
static int _srmio_ios_write( srmio_io_t h, const unsigned char *buf, size_t len, srmio_error_t *err ) { int ret; assert( h ); assert( buf ); ret = write( SELF(h)->fd, buf, len ); if( ret < 0 ){ srmio_error_errno( err, "ios write" ); } else { tcdrain( SELF(h)->fd ); } return ret; }
static int cached_stty(int fd,Sgttyb *buf){ int xfd = (0 <= fd && fd < MAXFD) ? fd: MAXFD-1; int rcode; if( cur_sgttyb[xfd].SG_flags != buf->SG_flags || cur_sgttyb[xfd].SG_lflag != buf->SG_lflag ){ cur_sgttyb[xfd] = *buf; #ifdef hpux tcdrain(fd); usleep(100*1000); #endif rcode = TCsetattr(fd,buf); #ifdef hpux set_cbreak(fd,!(buf->SG_lflag&ICANON)); #endif return rcode; }else return 0; }
void SerialConnection::sendCommand(QByteArray cmd) { #ifdef _WIN32 DWORD bytes_sent; #endif mode = MODE_NORMAL; if (context != NULL) { #ifdef _WIN32 if (!WriteFile(*((HANDLE*)context), cmd.constData(), cmd.length(), &bytes_sent, 0) || bytes_sent != (DWORD)cmd.length()) emit error(QString("unable to send command.")); #else if (write(*((int*)context), cmd.constData(), cmd.length()) != cmd.length()) emit error(QString("unable to send command")); tcdrain(*((int*)context)); #endif } else { emit error("cannot send command if disconnected from usb device."); } }
int serial_write(struct serial_dev * dev, const void * buf, unsigned int len) { int fd; int n; assert(dev != NULL); fd = dev->fd; if ((n = write(fd, buf, len)) < 0) { perror("write():"); return n; } tcdrain(fd); return n; }
// empty the output buffer, waiting for all of the chars to be delivered // Return 0 on success, nonzero on failure. // NOT CALLED! OBSOLETE? -- no ... used by vrpn_Flock int vrpn_drain_output_buffer(int comm) { #ifdef VERBOSE printf("vrpn_drain_output_buffer(): Entering\n"); #endif #if defined(hpux) || defined(__hpux) || defined(ultrix) || defined(__CYGWIN__) fprintf(stderr, "vrpn_drain_output_buffer: Not impemented on NT, ultrix, or HP\n"); return -1; #else #if defined(_WIN32) return FlushFileBuffers(commConnections[comm]) == 0; #else return tcdrain(comm); #endif #endif }
int serial_close(int filedescriptor) { if(tcdrain(filedescriptor) < 0){ perror("serial_close: tcdrain"); return -1; } if(tcflush(filedescriptor, TCIOFLUSH) < 0){ perror("serial_close: tcflush"); return -2; } if(close(filedescriptor) < 0){ perror("serial_close: close"); return -3; } return 0; }
int rpi_comport_setbaud(int baudrate) { struct termios tmptio; if (fd_comport <= 0) { fprintf(stderr, "Error in coding: RPi comport wasn't opened.\n"); exit(-1); } /* get current port settings */ tcgetattr(fd_comport, &tmptio); /* set new baud rate and preserve everything else */ cfsetspeed(&tmptio, baudrate); tcdrain(fd_comport); tcflush(fd_comport, TCIOFLUSH); //msleep(500); tcsetattr(fd_comport, TCSANOW, &tmptio); msleep(500); return 0; }
int ao_gps_open(const char *tty) { struct termios termios; int fd; fd = open (tty, O_RDWR); if (fd < 0) return -1; tcgetattr(fd, &termios); cfmakeraw(&termios); cfsetspeed(&termios, B4800); tcsetattr(fd, TCSAFLUSH, &termios); tcdrain(fd); tcflush(fd, TCIFLUSH); return fd; }
/* * Write command string to scope with delays. */ static void scope_write(int fd, unsigned int *cmd) { int i; unsigned char c; for (i = 1; i <= cmd[0]; i++) { if (cmd[i] >= 0x100) { tcdrain(fd); usleep((cmd[i] - 0x100) * 1000); } else { c = cmd[i]; write(fd, &c, 1); } } }
// // CloseSerialPort() // // Close the serial port. // We close the serial port when switching ports or quitting. // void TMSerialPort::Close() { if (fd != kSerialError) { // Block until all written output has been sent from the device. // Note that this call is simply passed on to the serial device driver. // See tcsendbreak(3) ("man 3 tcsendbreak") for details. if (tcdrain(fd) == kSerialError && output != NULL) fprintf(output, "Error waiting for drain - %s(%d).\n", strerror(errno), errno); // It is good practice to reset a serial port back to the state in // which you found it. This is why we saved the original termios struct // The constant TCSANOW (defined in termios.h) indicates that // the change should take effect immediately. if (tcsetattr(fd, TCSANOW, &originalAttribs) == kSerialError && output != NULL) fprintf(output, "Error resetting tty attributes - %s(%d).\n", strerror(errno), errno); close(fd); fd = kSerialError; } }
ATMO_BOOL CAtmoClassicConnection::SendData(pColorPacket data) { if(m_hComport == INVALID_HANDLE_VALUE) return ATMO_FALSE; unsigned char buffer[19]; DWORD iBytesWritten; buffer[0] = 0xFF; // Start Byte buffer[1] = 0x00; // Start channel 0 buffer[2] = 0x00; // Start channel 0 buffer[3] = 15; // int iBuffer = 4; int idx; Lock(); for(int i=0; i < 5 ; i++) { if(m_ChannelAssignment && (i < m_NumAssignedChannels)) idx = m_ChannelAssignment[i]; else idx = -1; if((idx>=0) && (idx<data->numColors)) { buffer[iBuffer++] = data->zone[idx].r; buffer[iBuffer++] = data->zone[idx].g; buffer[iBuffer++] = data->zone[idx].b; } else { buffer[iBuffer++] = 0; buffer[iBuffer++] = 0; buffer[iBuffer++] = 0; } } #if defined(WIN32) WriteFile(m_hComport, buffer, 19, &iBytesWritten, NULL); // send to COM-Port #else iBytesWritten = write(m_hComport, buffer, 19); tcdrain(m_hComport); #endif Unlock(); return (iBytesWritten == 19) ? ATMO_TRUE : ATMO_FALSE; }
void gpsd_close(struct gps_device_t *session) { if (!BAD_SOCKET(session->gpsdata.gps_fd)) { (void)ioctl(session->gpsdata.gps_fd, (unsigned long)TIOCNXCL); (void)tcdrain(session->gpsdata.gps_fd); if (isatty(session->gpsdata.gps_fd) != 0) { /* force hangup on close on systems that don't do HUPCL properly */ /*@ ignore @*/ (void)cfsetispeed(&session->ttyset, (speed_t) B0); (void)cfsetospeed(&session->ttyset, (speed_t) B0); /*@ end @*/ (void)tcsetattr(session->gpsdata.gps_fd, TCSANOW, &session->ttyset); } /* this is the clean way to do it */ session->ttyset_old.c_cflag |= HUPCL; /* * Don't revert the serial parameters if we didn't have to mess with * them the first time. Economical, and avoids tripping over an * obscure Linux 2.6 kernel bug that disables threaded * ioctl(TIOCMWAIT) on a device after tcsetattr() is called. */ if (cfgetispeed(&session->ttyset_old) != cfgetispeed(&session->ttyset) || (session->ttyset_old.c_cflag & CSTOPB) != (session->ttyset.c_cflag & CSTOPB)) { /* * If we revert, keep the most recent baud rate. * Cuts down on autobaud overhead the next time. */ /*@ ignore @*/ (void)cfsetispeed(&session->ttyset_old, (speed_t) session->gpsdata.dev.baudrate); (void)cfsetospeed(&session->ttyset_old, (speed_t) session->gpsdata.dev.baudrate); /*@ end @*/ (void)tcsetattr(session->gpsdata.gps_fd, TCSANOW, &session->ttyset_old); } gpsd_report(session->context->debug, LOG_SPIN, "close(%d) in gpsd_close(%s)\n", session->gpsdata.gps_fd, session->gpsdata.dev.path); (void)close(session->gpsdata.gps_fd); session->gpsdata.gps_fd = -1; } }
/* send through rs485 * return: bytes send */ int write_rs485 (int fd, const char *data, const int nr) { int size = 0; int state; if (nr == 0 || fd < 0) return 0; usleep(RS485_DELAY_BEFORE * 1000); /* send */ size = write(fd, data, nr); /* wait for all data send */ tcdrain(fd); usleep(RS485_DELAY_AFTER * 1000); return size; }
ATMO_BOOL CAtmoDmxSerialConnection::SendData(pColorPacket data) { if(m_hComport == INVALID_HANDLE_VALUE) return ATMO_FALSE; int iBuffer = 2; DWORD iBytesWritten; Lock(); int idx, z = 0; for(int i=0;i<getNumChannels();i++) { if(m_ChannelAssignment && (i < m_NumAssignedChannels)) idx = m_ChannelAssignment[i]; else idx = -1; if((idx>=0) && (idx<data->numColors)) { if( m_dmx_channels_base[z] >= 0 ) iBuffer = m_dmx_channels_base[z] + 2; else iBuffer += 3; DMXout[iBuffer] = data->zone[ idx ].r; DMXout[iBuffer+1] = data->zone[ idx ].g; DMXout[iBuffer+2] = data->zone[ idx ].b; } if( m_dmx_channels_base[z] >= 0 ) z++; } #if defined(WIN32) WriteFile(m_hComport, DMXout, 259, &iBytesWritten, NULL); // send to COM-Port #else iBytesWritten = write(m_hComport, DMXout, 259); tcdrain(m_hComport); #endif Unlock(); return (iBytesWritten == 259) ? ATMO_TRUE : ATMO_FALSE; }
static int oceanserver_send(int fd, const char *fmt, ...) { int status; char buf[BUFSIZ]; va_list ap; va_start(ap, fmt); (void)vsnprintf(buf, sizeof(buf) - 5, fmt, ap); va_end(ap); (void)strlcat(buf, "", BUFSIZ); status = (int)write(fd, buf, strlen(buf)); (void)tcdrain(fd); if (status == (int)strlen(buf)) { gpsd_report(LOG_IO, "=> GPS: %s\n", buf); return status; } else { gpsd_report(LOG_WARN, "=> GPS: %s FAILED\n", buf); return -1; } }
mraa_result_t mraa_uart_flush(mraa_uart_context dev) { if (!dev) { syslog(LOG_ERR, "uart: flush: context is NULL"); return MRAA_ERROR_INVALID_HANDLE; } if (IS_FUNC_DEFINED(dev, uart_flush_replace)) { return dev->advance_func->uart_flush_replace(dev); } #if !defined(PERIPHERALMAN) if (tcdrain(dev->fd) == -1) { return MRAA_ERROR_FEATURE_NOT_SUPPORTED; } #endif return MRAA_SUCCESS; }
// Given the file descriptor for a serial device, close that device. void closeSerialPort(int fileDescriptor) { // Block until all written output has been sent from the device. // Note that this call is simply passed on to the serial device driver. // See tcsendbreak(3) <x-man-page://3/tcsendbreak> for details. if (tcdrain(fileDescriptor) == -1) { printf("Error waiting for drain - %s(%d).\n", strerror(errno), errno); } // Traditionally it is good practice to reset a serial port back to // the state in which you found it. This is why the original termios struct // was saved. if (tcsetattr(fileDescriptor, TCSANOW, &gOriginalTTYAttrs) == -1) { printf("Error resetting tty attributes - %s(%d).\n", strerror(errno), errno); } close(fileDescriptor); }
int mmWrite(struct ModemStream* modem, char *buffer, int bytes){ #ifdef ARDUINO return modem->stream->write(buffer, bytes); #endif #ifdef LINUX int written = write(modem->fd, buffer, bytes); tcdrain(modem->fd); return written; #endif #ifdef _WIN32 DWORD bytesSend; if (!WriteFile(modem->hSerial, buffer, bytes, &bytesSend, 0)) { ClearCommError(modem->hSerial, modem->errors, &modem->status); return -1; } else return (int)bytesSend; #endif }
void close_serial(){ int p; int ports = 1; const char close_code[2] = "9"; if (on_serial == 0) return; if (on_serial == 3) ports = 2; printf("sending termination code to serial port(s).\n"); for (p=0; p<ports; p++){ write(serial_fd[p], close_code, 1); if(!use_tcp){ tcdrain(serial_fd[p]); tcsetattr(serial_fd[p], TCSANOW, &oldtio[p]); } close(serial_fd[p]); } }
int slow_write(int fd,char *d,int l,int preWait) { // UART is at 2Mbps, but we need to allow enough time for a whole line of // writing. 100 chars x 0.5usec = 500usec. So 1ms between chars should be ok. // printf("Writing [%s]\n",d); int i; usleep(preWait); for(i=0;i<l;i++) { int w=write(fd,&d[i],1); while (w<1) { usleep(1000); w=write(fd,&d[i],1); } // Only control characters can cause us whole line delays, if (d[i]<' ') { usleep(2000); } else usleep(0); } tcdrain(fd); return 0; }