📄 serdpcpl.c
字号:
if ((outputmode != STREAM) || (rxchar == RXTIMEOUT))
{
clear_rx();
return(RXERRORS);
}
/*
If an Error occured and we are in STREAM mode, resynch
*/
if (outputmode == STREAM)
{
/*
Resynch
...and keep track of the phase error
*/
phaseerror_count++;
resynch = TRUE;
continue;
}
}
/*
Check to make sure the the phase bit is a '1'
If not, then if STREAM mode, resynch
else, return with error
*/
if (!(rxchar & 0x80))
{
if (outputmode == STREAM)
{
/*
Resynch
...and keep track of the phase error
*/
phaseerror_count++;
resynch = TRUE;
continue;
}
else
{
clear_rx();
return(RXPHASEERROR);
}
}
}
else /* rxcount > 0 */
{
/*
Get remainder of Block of data from the serial port, recsize characters
and store them in rxbuf
*/
if ((rxchar = waitforchar()) >= 0) /* no errors */
{
/*
Check Phase bit
*/
if (rxchar & 0x80) /* check to see that phase bit = '0' */
{
if (outputmode == STREAM)
{
phaseerror_count++; /* keep track of phase errors */
resynch = TRUE; /* loop again flag */
continue;
}
else
{
clear_rx();
return(RXPHASEERROR); /* return phase error */
}
}
}
else
{
if (outputmode == STREAM)
{
phaseerror_count++; /* keep track of phase errors */
resynch = TRUE; /* loop again flag */
continue;
}
else
{
clear_rx();
return(RXERRORS);
}
}
}
/*
Store the received character
*/
*rxbufptr++ = rxchar; /* store and adjust pointer */
rxcount++; /* increment */
}
while ((resynch) || (rxcount < recsize));
/*
Return the number of characters received
*/
return(rxcount);
}
/*
send_serial_cmd - Send Serial Command to the Bird port
Prototype in: serial.h
Parameters Passed: cmd - string to send to the serial point
cmdsize - size of the cmd string (cmd is NOT
NULL terminated, since the data can
be NULL)
Return Value: number of characters transmitted
Remarks: Routine will send a string of characters to the serial
port. The string is pointed to by cmd and all
characters will be sent upto but NOT including
the NULL
*/
int send_serial_cmd(cmd,cmdsize)
unsigned char * cmd;
short cmdsize;
{
short txcount = 0;
unsigned char rs232tofbbcmd;
/*
Send the RS232 to FBB Prefice Character if non-zero
*/
if (rs232tofbbaddr > 0)
{
if (rs232tofbbaddr <= 15)
/* pass through command 0-15 */
rs232tofbbcmd = (unsigned char)(0xF0 | rs232tofbbaddr);
else
/* pass through command 16-31 */
rs232tofbbcmd = (unsigned char)(0xE0 | rs232tofbbaddr-16);
while (send_serial_char(rs232tofbbcmd) == TXNOTEMPTY);
}
while (txcount < cmdsize)
{
/*
Wait until the character goes out OK
*/
while ((send_serial_char(*cmd)) == TXNOTEMPTY);
/*
point to the next character
*/
cmd++;
txcount++;
}
return(txcount++);
}
/*
get_serial_char - Get 1 Character from the serial port if one is available
Prototype in: serial.h
Parameters Passed: void
Return Value: returns the
Remarks: returns the receive character if successful,
RXERRORS if recieve errors
NODATAAVAIL if no characer available
*/
int get_serial_char()
{
short linestatus;
/*
Get line status and check if character is available
else return
*/
if ((linestatus = INPORTB(com_base + LINESTATUS)) & DATARDY)
{
/*
check for errors and return
*/
if ((rxerrors = (linestatus & RXERRORMSK)) != 0)
return(RXERRORS);
/*
Send back the RX data
*/
return(INPORTB(com_base + RXBUF));
}
else
{
/*
check for errors and return
*/
if ((rxerrors = (linestatus & RXERRORMSK)) != 0)
return(RXERRORS);
return(NODATAAVAIL);
}
}
/*
send_serial_char - Send one serial char to the serial port
Prototype in: serial.h
Parameters Passed: chr - character to send to the serial port
Return Value: returns TRUE if successful, or TXNOTEMPTY if
cannot send because the holding register is not
empty
Remarks:
*/
int send_serial_char(chr)
unsigned char chr;
{
/*
Get line status and check if transmit holding register is empty
else return TXNOTEMPTY
*/
if (!(INPORTB(com_base + LINESTATUS) & TXHOLDEMPTY))
return(TXNOTEMPTY);
/*
Else, Transmit the Character
*/
OUTPORTB(com_base + TXBUF, (char) chr);
return(TRUE);
}
/*
waitforchar - Wait for a Character from the Serial Port
Prototype in: serial.h
Parameters Passed: void
Return Value: returns the receive character if successful,
RXERRORS if recieve errors,
RXTIMEOUT if a time out occurs before a
character is ready
Remarks: Routine waits for the TIMEOUTINTICKS period
for a character
*/
int waitforchar()
{
short rxchar;
long starttime;
/*
Get the time now in ticks
*/
starttime = GETTICKS;
/*
Wait until a character is available
....leave loop if errors or character available
*/
while ((rxchar = get_serial_char()) == NODATAAVAIL)
{
/*
Check to see if a timeout occured
*/
if ((GETTICKS - starttime) > (long)((RXTIMEOUTINSECS * 1000) / TICK_MSECS))
{
printf("\n** ERROR ** receiver timed out\n");
return(RXTIMEOUT);
}
}
/*
return if RX errors
*/
if (rxchar < 0)
return(RXERRORS);
/*
Everything is OK...return the character
*/
return(rxchar);
}
/*
waitforphase - Wait for a Character with phase bit set
Prototype in: serial.h
Parameters Passed: void
Return Value: returns the received character if successful,
or RXERRORS if an error occurs
Remarks: waits for a character to be received with the
most significant bit (bit 7) set to a '1'. Characters
received with bit 7 = '0' are thrown away.
Routine waits for the TIMEOUTINTICKS period.
*/
int waitforphase()
{
short rxchar;
/*
Wait until waitforchar returns a character or error
*/
while (((rxchar = waitforchar()) & 0x80) == 0)
{
/*
return if errors
*/
if (rxchar < 0)
return(RXERRORS);
}
/*
Everything is OK...return the character
*/
return(rxchar);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -