📄 ulibnt.c
字号:
printmsg(2,"sread: User aborted processing");
recurse = TRUE;
}
return 0;
}
/*--------------------------------------------------------------------*/
/* Compute a new timeout for the read, if needed */
/*--------------------------------------------------------------------*/
if (stop_time > now )
{
port_timeout = (USHORT) (stop_time - now) / needed * 100;
if (port_timeout < 100)
port_timeout = 100;
}
else
port_timeout = 0;
if (!console)
{
port_timeout *= 10; /* OS/2 is in hundredths; NT in msec */
CommTimeout.ReadTotalTimeoutConstant = 0;
CommTimeout.WriteTotalTimeoutConstant = 0;
CommTimeout.ReadIntervalTimeout = port_timeout;
CommTimeout.ReadTotalTimeoutMultiplier = 1;
CommTimeout.WriteTotalTimeoutMultiplier = 0;
rc = SetCommTimeouts(hCom, &CommTimeout);
if ( !rc )
{
dwError = GetLastError();
printmsg(0, "sread: unable to set timeout for comm port");
printNTerror("sread", dwError);
panic();
}
}
#ifdef UDEBUG
printmsg(15,"sread: Port time out is %ud seconds/100",
port_timeout);
#endif
/*--------------------------------------------------------------------*/
/* Read the data from the serial port */
/*--------------------------------------------------------------------*/
rc = ReadFile (hCom, &save[bufsize], needed, &received, NULL);
if (!rc) {
printmsg(0,
"sread: Read from comm port for %d bytes failed, received = %d.",
needed, received);
bufsize = 0;
return 0;
}
#ifdef UDEBUG
printmsg(15,"sread: Want %d characters, received %d, total %d in buffer",
(int) wanted, (int) received, (int) bufsize + received);
#endif
/*--------------------------------------------------------------------*/
/* Log the newly received data */
/*--------------------------------------------------------------------*/
traceData( &save[bufsize], received, FALSE );
/*--------------------------------------------------------------------*/
/* If we got the data, return it to the caller */
/*--------------------------------------------------------------------*/
bufsize += received;
if ( bufsize == wanted )
{
memmove( output, save, bufsize);
bufsize = 0;
if (debuglevel > 14)
fwrite(output,1,bufsize,stdout);
return wanted;
} /* if */
/*--------------------------------------------------------------------*/
/* Update the clock for the next pass */
/*--------------------------------------------------------------------*/
if (stop_time > 0)
time( &now );
} while (stop_time > now);
/*--------------------------------------------------------------------*/
/* We don't have enough data; report what we do have */
/*--------------------------------------------------------------------*/
return bufsize;
} /*nsread*/
/*--------------------------------------------------------------------*/
/* n s w r i t e */
/* */
/* Write to the serial port */
/*--------------------------------------------------------------------*/
int nswrite(const char *input, unsigned int len)
{
char *data = (char *) input;
DWORD bytes;
BOOL rc;
hangupNeeded = TRUE; /* Flag that the port is now dirty */
/*--------------------------------------------------------------------*/
/* Write the data out as the queue becomes available */
/*--------------------------------------------------------------------*/
rc = WriteFile (hCom, data, len, &bytes, NULL);
if (!rc) {
printmsg(0,"swrite: Write to communications port failed.");
return bytes;
}
/*--------------------------------------------------------------------*/
/* Log the data written */
/*--------------------------------------------------------------------*/
traceData( data, len, TRUE);
/*--------------------------------------------------------------------*/
/* Return bytes written to the port to the caller */
/*--------------------------------------------------------------------*/
return len;
} /*nswrite*/
/*--------------------------------------------------------------------*/
/* n s s e n d b r k */
/* */
/* send a break signal out the serial port */
/*--------------------------------------------------------------------*/
void nssendbrk(unsigned int duration)
{
#ifdef UDEBUG
printmsg(12, "ssendbrk: %d", duration);
#endif
SetCommBreak(hCom);
ddelay( duration == 0 ? 200 : duration);
ClearCommBreak(hCom);
} /*nssendbrk*/
/*--------------------------------------------------------------------*/
/* n c l o s e l i n e */
/* */
/* Close the serial port down */
/*--------------------------------------------------------------------*/
void ncloseline(void)
{
DWORD dwError;
if ( ! portActive )
panic();
portActive = FALSE; /* flag port closed for error handler */
hangupNeeded = FALSE; /* Don't fiddle with port any more */
/*--------------------------------------------------------------------*/
/* Lower DTR */
/*--------------------------------------------------------------------*/
if (!EscapeCommFunction(hCom, CLRDTR | CLRRTS))
{
printmsg(0,"ncloseline: Unable to lower DTR/RTS");
}
/*--------------------------------------------------------------------*/
/* Actually close the port */
/*--------------------------------------------------------------------*/
if(!CloseHandle(hCom))
{
dwError = GetLastError();
printmsg(0, "ncloseline: close of serial port failed");
printNTerror("ncloseline", dwError);
}
/*--------------------------------------------------------------------*/
/* Stop logging the data to disk */
/*--------------------------------------------------------------------*/
traceStop();
} /* ncloseline */
/*--------------------------------------------------------------------*/
/* n h a n g u p */
/* */
/* Hangup the telephone by dropping DTR. Works with HAYES and */
/* many compatibles. */
/* 14 May 89 Drew Derbyshire */
/*--------------------------------------------------------------------*/
void nhangup( void )
{
if (!hangupNeeded)
return;
hangupNeeded = FALSE;
if ( console )
return;
/*--------------------------------------------------------------------*/
/* Drop DTR */
/*--------------------------------------------------------------------*/
if (!EscapeCommFunction(hCom, CLRDTR))
{
printmsg(0, "hangup: Unable to lower DTR for comm port");
panic();
}
/*--------------------------------------------------------------------*/
/* Wait for the telephone to hangup */
/*--------------------------------------------------------------------*/
printmsg(3,"hangup: Dropped DTR");
carrierdetect = FALSE; /* Modem is not connected */
ddelay(500); /* Really only need 250 milliseconds */
/*--------------------------------------------------------------------*/
/* Bring DTR back up */
/*--------------------------------------------------------------------*/
if (!EscapeCommFunction(hCom, SETDTR))
{
printmsg(0, "hangup: Unable to raise DTR for comm port");
panic();
}
ddelay(2000); /* Now wait for the poor thing to recover */
} /* nhangup */
/*--------------------------------------------------------------------*/
/* n S I O S p e e d */
/* */
/* Re-specify the speed of an opened serial port */
/* */
/* Dropped the DTR off/on calls because this makes a Hayes drop */
/* the line if configured properly, and we don't want the modem */
/* to drop the phone on the floor if we are performing */
/* autobaud. */
/* */
/* (Configured properly = standard method of making a Hayes */
/* hang up the telephone, especially when you can't get it into */
/* command state because it is at the wrong speed or whatever.) */
/*--------------------------------------------------------------------*/
void nSIOSpeed(BPS baud)
{
USHORT rc;
#ifdef UDEBUG
printmsg(15,"SIOSpeed: Setting baud rate to %lu",
(unsigned long) baud);
#endif
GetCommState (hCom, &dcb);
dcb.BaudRate = baud;
rc = SetCommState (hCom, &dcb);
if (!rc && !console) {
printmsg(0,"SIOSpeed: Unable to set baud rate for port to %lu",
(unsigned long) baud);
panic();
}
currentSpeed = baud;
} /* nSIOSpeed */
/*--------------------------------------------------------------------*/
/* n f l o w c o n t r o l */
/* */
/* Enable/Disable in band (XON/XOFF) flow control */
/*--------------------------------------------------------------------*/
void nflowcontrol( boolean flow )
{
USHORT rc;
DCB dcb;
DWORD dwError;
if ( console )
return;
GetCommState(hCom, &dcb);
if (flow)
{
dcb.fOutX = TRUE;
dcb.fInX = TRUE;
dcb.fRtsControl = RTS_CONTROL_ENABLE;
dcb.fOutxCtsFlow = FALSE;
} else {
dcb.fOutX = FALSE;
dcb.fInX = FALSE;
dcb.fRtsControl = RTS_CONTROL_ENABLE;
dcb.fOutxCtsFlow = TRUE;
}
rc = SetCommState(hCom, &dcb);
if ( !rc )
{
dwError = GetLastError();
printmsg(0,"flowcontrol: Unable to set flow control");
printNTerror("nflowcontrol", dwError);
panic();
} /*if */
} /* nflowcontrol */
/*--------------------------------------------------------------------*/
/* n G e t S p e e d */
/* */
/* Report current speed of communications connection */
/*--------------------------------------------------------------------*/
BPS nGetSpeed( void )
{
return currentSpeed;
} /* nGetSpeed */
/*--------------------------------------------------------------------*/
/* n C D */
/* */
/* Return status of carrier detect */
/*--------------------------------------------------------------------*/
boolean nCD( void )
{
boolean previous_carrierdetect = carrierdetect;
USHORT rc;
DWORD status;
static DWORD oldstatus = (DWORD) 0xDEADBEEF;
DWORD dwError;
if ( console )
return feof( stdin ) == 0;
rc = GetCommModemStatus(hCom, &status);
if ( !rc )
{
dwError = GetLastError();
printmsg(0,"nCD: Unable to get modem status");
printNTerror("nCD", dwError);
panic();
} /*if */
if ( status != oldstatus )
{
ShowModem( status );
oldstatus = status;
}
/*--------------------------------------------------------------------*/
/* If we previously had carrier detect but have lost it, we */
/* report it was lost. If we do not yet have carrier detect, */
/* we return success because we may not have connected yet. */
/*--------------------------------------------------------------------*/
carrierdetect = status && MS_RLSD_ON;
if (previous_carrierdetect)
return (status && (MS_RLSD_ON || MS_DSR_ON)) ==
(MS_RLSD_ON || MS_DSR_ON);
else
return (status && MS_DSR_ON);
} /* nCD */
/*--------------------------------------------------------------------*/
/* S h o w M o d e m */
/* */
/* Report current modem status */
/*--------------------------------------------------------------------*/
#define mannounce(flag, bits, text ) ((flag & bits) ? text : "" )
static void ShowModem( const DWORD status )
{
if ( debuglevel < 4 )
return;
printmsg(0, "ShowModem: %#02x%s%s%s%s",
status,
mannounce(MS_RLSD_ON, status, " Carrier Detect"),
mannounce(MS_RING_ON, status, " Ring Indicator"),
mannounce(MS_DSR_ON, status, " Data Set Ready"),
mannounce(MS_CTS_ON, status, " Clear to Send"));
} /* ShowModem */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -