int serialPort::portMode(setType setting, char *set) { switch (setting) { case CONFIG: strcpy(config, set); break; case EOSMODE: eosMode = *set; break; case CLEAR: #ifdef __WIN32__ SetCommBreak( handle ); Sleep ( 300 ); // sleep 300 ms ClearCommBreak( handle ); Sleep ( 50 ); // sleep 50 ms #endif #ifdef __linux__ printf("serialPort::portMode break 300\n"); tcsendbreak( handle, 300); // should send 300 ms break usleep(100000); // a bit of a guard after tcdrain( handle ); #endif break; case TIMEOUT: timeout = strtoul(set, NULL, 10) * 1000L; break; case PACING: pace = strtoul(set, NULL, 10) * 1000L; break; } return 0; }
int serialCommunicator::SerialBreak() { #if defined WIN32 // Asio library doesn't support serial breaks on windows, so use win api boost::asio::deadline_timer timer(IO_service); // Set serial break SetCommBreak(newSerial->native_handle()); // Wait for defined period timer.expires_from_now(boost::posix_time::milliseconds(serialBreakms)); timer.wait(); // Blocks till the timer expires // Clear serial break ClearCommBreak(newSerial->native_handle()); #else // Otherwise send using the asio interface -NOTE this hasn't been tested yet. newSerial->send_break(); #endif return 1; }
int RS232Interface::SetSerialBreak(int state) { int result = E2ERR_OPENFAILED; #ifdef _WINDOWS if (hCom != INVALID_HANDLE_VALUE) { if (state) SetCommBreak(hCom); else ClearCommBreak(hCom); result = OK; } #else #ifdef _LINUX_ #if defined(TIOCSBRK) && defined(TIOCCBRK) //check if available for compilation if (state) result = ioctl(fd,TIOCSBRK,0); else result = ioctl(fd,TIOCCBRK,0); #else UserDebug(UserApp1, "RS232Interface::SetSerialBreak Can't get IOCTL\n"); #endif #endif #endif return result; }
int diag_tty_break(struct diag_l0_device *dl0d, const int ms) { HANDLE hd; long h; /* * I'm going through this convoluted two-step conversion * to avoid compiler warnings: */ h = get_osfhandle(dl0d->fd); hd = (HANDLE)h; if (tcdrain(dl0d->fd)) { fprintf(stderr, FLFMT "tcdrain returned %s.\n", FL, strerror(errno)); return diag_iseterr(DIAG_ERR_GENERAL); } SetCommBreak(hd); diag_os_millisleep(ms); ClearCommBreak(hd); return 0; }
// diag_tty_break #1 : use Set / ClearCommBreak // and return as soon as break is cleared. // ret 0 if ok int diag_tty_break(struct diag_l0_device *dl0d, const unsigned int ms) { LARGE_INTEGER qpc1, qpc2; //for timing verification long real_t; //"real" duration struct tty_int *wti = (struct tty_int *)dl0d->tty_int; int errval=0; if (wti->fd == INVALID_HANDLE_VALUE) { fprintf(stderr, FLFMT "Error. Is the port open ?\n", FL); return diag_iseterr(DIAG_ERR_GENERAL); } if (ms <= 1) return diag_iseterr(DIAG_ERR_GENERAL); QueryPerformanceCounter(&qpc1); errval = !SetCommBreak(wti->fd); QueryPerformanceCounter(&qpc2); //that call can take quite a while (6ms !!) on some setups (win7 + CH340 USB-Serial). //It's still impossible to know (from here) when exactly TXD goes low (beginning or end of the call) real_t=(long) (pf_conv * (qpc2.QuadPart-qpc1.QuadPart)) / 1000L; real_t = (long) ms - real_t; //time remaining if (real_t <= 0) real_t = 0; diag_os_millisleep((unsigned int ) real_t); errval |= !ClearCommBreak(wti->fd); if (errval) { //if either of the calls failed fprintf(stderr, FLFMT "tty_break could not set/clear break!\n", FL); return diag_iseterr(DIAG_ERR_GENERAL); } return 0; }
int xbee_ser_break( xbee_serial_t *serial, int enabled) { BOOL success; XBEE_SER_CHECK( serial); if (enabled) { success = SetCommBreak( serial->hCom); } else { success = ClearCommBreak( serial->hCom); } #ifdef XBEE_SERIAL_VERBOSE if (success == 0) { printf( "%s: {Set|Clear}CommBreak error %lu\n", __FUNCTION__, GetLastError()); } #endif return success ? 0 : -EIO; }
int far pascal zSetCommBreak( int pp1 ) { int r; SaveRegs(); /* ** Log IN Parameters (No Create/Destroy Checking Yet!) */ LogIn( (LPSTR)"APICALL:SetCommBreak int+", pp1 ); /* ** Call the API! */ RestoreRegs(); GrovelDS(); r = SetCommBreak(pp1); UnGrovelDS(); SaveRegs(); /* ** Log Return Code & OUT Parameters (No Create/Destroy Checking Yet!) */ LogOut( (LPSTR)"APIRET:SetCommBreak int++", r, (short)0 ); RestoreRegs(); return( r ); }
void com_break(int on) { COMMHANDLE h=ComGetHandle(hcModem); if (on) SetCommBreak(h); else ClearCommBreak(h); }
//уст. в указ. знач. линию TXD // bool CComPort::SetTXD(bool state) { //TODO: maybe use of EscapeCommFunction() will be more suitable? if (state) return SetCommBreak(m_hCom); else return ClearCommBreak(m_hCom); }
int wxSerialPort::SendBreak(int duration) { if(duration <= 0) duration = 1; if(!SetCommBreak(fd)) return -1; // win32 Sleep parameter is ms Sleep(duration * 250); if(!ClearCommBreak(fd)) return -1; // no error return 0; };
void SerialPort::sendBreak() { #ifdef __WIN32__ SetCommBreak(handle); ClearCommBreak(handle); #endif #if defined(__gnu_linux__) || (defined(__APPLE__) && defined(__MACH__)) tcsendbreak(handle, 0); #endif }
void CSerialPort::SetBreak() { ASSERT(IsOpen()); if (!SetCommBreak(m_hComm)) { TRACE(_T("Failed in call to SetCommBreak\n")); AfxThrowSerialException(); } }
// Transmit a continuous stream of zero bits for the given duration. If the // argument is 0, the transmission will last between 0.25 and 0.5 seconds. // Otherwise, the duration specfies the number of seconds to send the zero bit // stream. void SerialPortImplWin32::sendBreak(const vpr::Int32 duration) { DWORD flags; //Send a break for .5 seconds SetCommBreak(mHandle); Sleep(500); ClearCommBreak(mHandle); Sleep(35); ClearCommError(mHandle,&flags,NULL); //Clear the break error }
void serial_raw::set_break(int mode) { BX_DEBUG (("set break %s", mode?"on":"off")); #ifdef WIN32 if (mode) { SetCommBreak(hCOM); } else { ClearCommBreak(hCOM); } #endif }
int ndiSerialBreak(HANDLE serial_port) { DWORD dumb; ClearCommError(serial_port,&dumb,NULL); /* clear error */ PurgeComm(serial_port,PURGE_TXCLEAR|PURGE_RXCLEAR); /* clear buffers */ SetCommBreak(serial_port); Sleep(300); /* hold break for 0.3 seconds */ ClearCommBreak(serial_port); return 0; }
//------------------------------------------------------------------- // Description: // Send a break on the com port for "len" msec // // portnum - number 0 to MAX_PORTNUM-1. This number is provided to // indicate the symbolic port number. // void BreakCOM(int portnum) { // start the reset pulse SetCommBreak(ComID[portnum]); // delay msDelay(2); // clear the reset pulse and return // PROGRAMMER'S NOTE: ClearCommBreak is returning 24:an undefined code ClearCommBreak(ComID[portnum]); // Win3.1 bug, close and then open port CloseCOM(portnum); OpenCOM(portnum,&port[0]); }
static int ser_windows_send_break (struct serial *scb) { HANDLE h = (HANDLE) _get_osfhandle (scb->fd); if (SetCommBreak (h) == 0) return -1; /* Delay for 250 milliseconds. */ Sleep (250); if (ClearCommBreak (h)) return -1; return 0; }
/***************************************************************** Routine: SerialBreak() Inputs: None. Returns: int COMM_OK if successful Description: This routine resets the COM PORT by sending a Serial Break to it. *****************************************************************/ int Comm32Port::SerialBreak() { if( m_hCommPort == INVALID_HANDLE_VALUE ) return COMM_NOT_INITIALIZED; if (!SetCommBreak( m_hCommPort )) return COMM_ERROR_SETBREAK; Sleep( m_ulSerBreakDelay ); if (!ClearCommBreak( m_hCommPort )) return COMM_ERROR_CLEARBREAK; return COMM_OK; } /* SerialBreak */
// diag_tty_break #1 : use Set / ClearCommBreak // and return as soon as break is cleared. // ret 0 if ok int diag_tty_break(struct diag_l0_device *dl0d, const unsigned int ms) { LARGE_INTEGER qpc1, qpc2; //for timing verification static long correction=0; //running average offset (us) to add to the timeout long real_t; //"real" duration measured in us struct tty_int *wti = (struct tty_int *)dl0d->tty_int; int errval=0; if (wti->fd == INVALID_HANDLE_VALUE) { fprintf(stderr, FLFMT "Error. Is the port open ?\n", FL); return diag_iseterr(DIAG_ERR_GENERAL); } if ( (ms + correction/1000)<1) return diag_iseterr(DIAG_ERR_GENERAL); QueryPerformanceCounter(&qpc1); errval=!SetCommBreak(wti->fd); diag_os_millisleep(ms + correction/1000); QueryPerformanceCounter(&qpc2); errval += !ClearCommBreak(wti->fd); if (errval) { //if either of the calls failed fprintf(stderr, FLFMT "tty_break could not set/clear break!\n", FL); return diag_iseterr(DIAG_ERR_GENERAL); } real_t=(long) (pf_conv * (qpc2.QuadPart-qpc1.QuadPart)); //now verify if it's within 1ms of the requested delay. real_t = real_t - (ms*1000); if (real_t < -3000) { diag_os_millisleep((unsigned int)(real_t / -1000)); } else if ((real_t > -1000) && (real_t < 1000)) { //good enough: return 0; } //we're here if we were off by more than -3ms or +1ms. //correct by a fraction of the error. //diag_os_millisleep also does some self-correcting; we don't want to overdo it. correction = correction - (real_t / 3); //fprintf(stderr, FLFMT "tty_break off by %ldus, new correction=%ldus.\n", // FL, real_t, correction); return 0; }
JNIEXPORT void JNICALL Java_gurux_io_NativeCode_setBreakState(JNIEnv* env, jclass clazz, jlong hComPort, jboolean value) { #if defined(_WIN32) || defined(_WIN64) if (value) { if (!SetCommBreak((HANDLE) hComPort)) { ReportError(env, GetLastError()); } } else { if (!ClearCommBreak((HANDLE) hComPort)) { ReportError(env, GetLastError()); } } #else int status = 0; if (value) { int ret = ioctl(hComPort, TIOCSBRK, &status); if (ret < 0) { ReportError(env, "setBreakState failed."); } } else { int ret = ioctl(hComPort, TIOCCBRK, &status); if (ret < 0) { ReportError(env, "setBreakState failed."); } } #endif }
void CommSendBreak(PComVar cv, int msec) /* for only serial ports */ { MSG DummyMsg; if ( ! cv->Ready ) { return; } switch (cv->PortType) { case IdSerial: /* Set com port into a break state */ SetCommBreak(cv->ComID); /* pause for 1 sec */ if (SetTimer(cv->HWin, IdBreakTimer, msec, NULL) != 0) { GetMessage(&DummyMsg,cv->HWin,WM_TIMER,WM_TIMER); } /* Set com port into the nonbreak state */ ClearCommBreak(cv->ComID); break; } }
/* * Send serial special codes. */ static void serial_special(Backend *be, SessionSpecialCode code, int arg) { Serial *serial = container_of(be, Serial, backend); if (serial->port && code == SS_BRK) { logevent(serial->logctx, "Starting serial break at user request"); SetCommBreak(serial->port); /* * To send a serial break on Windows, we call SetCommBreak * to begin the break, then wait a bit, and then call * ClearCommBreak to finish it. Hence, I must use timing.c * to arrange a callback when it's time to do the latter. * * SUS says that a default break length must be between 1/4 * and 1/2 second. FreeBSD apparently goes with 2/5 second, * and so will I. */ serial->clearbreak_time = schedule_timer(TICKSPERSEC * 2 / 5, serbreak_timer, serial); serial->break_in_progress = true; } return; }
/* * Send serial special codes. */ static void serial_special(void *handle, Telnet_Special code) { Serial serial = (Serial) handle; if (serial->port && code == TS_BRK) { logevent(serial->frontend, "Starting serial break at user request"); SetCommBreak(serial->port); /* * To send a serial break on Windows, we call SetCommBreak * to begin the break, then wait a bit, and then call * ClearCommBreak to finish it. Hence, I must use timing.c * to arrange a callback when it's time to do the latter. * * SUS says that a default break length must be between 1/4 * and 1/2 second. FreeBSD apparently goes with 2/5 second, * and so will I. */ serial->clearbreak_time = schedule_timer(TICKSPERSEC * 2 / 5, serbreak_timer, serial); serial->break_in_progress = TRUE; } return; }
// // write on ctl file. modify the settings for // the underlying port. // static void wrctl(int port, char *cmd) { DCB dcb; int nf, n, i; char *f[16]; HANDLE comfh = eia[port].comfh; DWORD flag, opt; BOOL rslt; int chg; // get the current settings for the port if(!GetCommState(comfh, &dcb)) oserror(); chg = 0; nf = tokenize(cmd, f, nelem(f)); for(i = 0; i < nf; i++){ if(strcmp(f[i], "break") == 0){ if(!SetCommBreak(comfh)) oserror(); SleepEx((DWORD)300, FALSE); if(!ClearCommBreak(comfh)) oserror(); continue; } n = atoi(f[i]+1); switch(*f[i]) { case 'B': case 'b': // set the baud rate if(n < 110) error(Ebadarg); dcb.BaudRate = n; chg = 1; break; case 'C': case 'c': /* dcd */ break; case 'D': case 'd': // set DTR opt = n ? SETDTR : CLRDTR; if(!EscapeCommFunction(comfh, opt)) oserror(); break; case 'E': case 'e': /* dsr */ break; case 'F': case 'f': // flush any untransmitted data if(!PurgeComm(comfh, PURGE_TXCLEAR)) oserror(); break; case 'H': case 'h': /* hangup */ /* TO DO: close handle */ break; case 'I': case 'i': /* fifo: nothing to do */ break; case 'K': case 'k': /* send a break */ if(!SetCommBreak(comfh)) oserror(); SleepEx((DWORD)300, FALSE); if(!ClearCommBreak(comfh)) oserror(); break; case 'L': case 'l': // set bits per byte flag = stof(size, f[0]+1); if(flag == BAD) error(Ebadarg); dcb.ByteSize = (BYTE)flag; chg = 1; break; case 'M': case 'm': // set CTS (modem control) dcb.fOutxCtsFlow = (n!=0); chg = 1; break; case 'N': case 'n': /* don't block on output */ break; case 'P': case 'p': // set parity -- even or odd flag = stof(parity, f[0]+1); if(flag==BAD) error(Ebadarg); dcb.Parity = (BYTE)flag; chg = 1; break; case 'Q': case 'q': /* set i/o queue limits */ break; case 'R': case 'r': // set RTS opt = n ? SETRTS : CLRRTS; if(!EscapeCommFunction(comfh, opt)) oserror(); break; case 'S': case 's': // set stop bits -- valid: 1 or 2 (win32 allows 1.5??) flag = stof(stopbits, f[0]+1); if(flag==BAD) error(Ebadarg); dcb.StopBits = flag; chg = 1; break; case 'T': case 't': break; case 'W': case 'w': /* set uart timer */ break; case 'X': case 'x': // xon/xoff opt = n ? SETXON : SETXOFF; if(!EscapeCommFunction(comfh, opt)) oserror(); break; default: /* ignore */ break; } } if(!chg) return; // make the changes on the underlying port, but flush // outgoing chars down the port before osenter(); rslt = FlushFileBuffers(comfh); if(rslt) rslt = SetCommState(comfh, &dcb); osleave(); if(!rslt) oserror(); eia[port].restore = 1; eia[port].dcb = dcb; }
void CDirectSerial::setBreak (bool value) { if (value) SetCommBreak (hCom); else ClearCommBreak (hCom); }
MX_EXPORT mx_status_type mxi_win32com_send_break( MX_RS232 *rs232 ) { static const char fname[] = "mxi_win32com_send_break()"; MX_WIN32COM *win32com; BOOL win32_status; mx_status_type status; MX_DEBUG( 2,("%s invoked.",fname)); status = mxi_win32com_get_pointers( rs232, &win32com, fname ); if ( status.code != MXE_SUCCESS ) return status; if ( win32com->handle == INVALID_HANDLE_VALUE ) { return mx_error( MXE_ILLEGAL_ARGUMENT, fname, "Win32 COM port device '%s' is not open.", rs232->record->name ); } /* Turn the break signal on. */ win32_status = SetCommBreak( win32com->handle ); if ( win32_status == 0 ) { DWORD last_error_code; TCHAR message_buffer[MXU_ERROR_MESSAGE_LENGTH - 120]; last_error_code = GetLastError(); mx_win32_error_message( last_error_code, message_buffer, sizeof(message_buffer) ); return mx_error( MXE_INTERFACE_IO_ERROR, fname, "Attempt to send a Break signal for '%s' failed. " "Win32 error code = %ld, error message = '%s'", win32com->filename, last_error_code, message_buffer ); } /* Leave the break signal on for about 0.5 seconds. */ Sleep(500); /* Turn the break signal off. */ win32_status = ClearCommBreak( win32com->handle ); if ( win32_status == 0 ) { DWORD last_error_code; TCHAR message_buffer[MXU_ERROR_MESSAGE_LENGTH - 120]; last_error_code = GetLastError(); mx_win32_error_message( last_error_code, message_buffer, sizeof(message_buffer) ); return mx_error( MXE_INTERFACE_IO_ERROR, fname, "Attempt to stop a Break signal for '%s' failed. " "Win32 error code = %ld, error message = '%s'", win32com->filename, last_error_code, message_buffer ); } return MX_SUCCESSFUL_RESULT; }