📄 serknr.c
字号:
ioctl(comhandle,TCSETA,&oldcom_termio); /* restore config */
#endif
close(comhandle); /* close the handle */
comhandle = -1; /* make the comhandle 'look' closed */
}
#endif /* DEBUG_SKIPSERIAL */
}
/*
clearrx - Read the characters out of the Rx buffer if available
Prototype in: serial.h
Parameters Passed: void
Return Value: void
Remarks: clears the rx buffer and rx errors if any
*/
void clear_rx()
{
#ifdef UNIX
short charsrdy;
char temprxbuf[255]; /* should never be more than 255 chars in buffer ?? */
#endif
phaseerror_count = 0; /* clear the phase counter */
#ifdef UNIX
/*
Flush Pending Buffers
*/
ioctl(comhandle,TCFLSH,TIOCFLUSH); /* flush pending I/O chars */
/*
Determine if there are chars in the Buffer
*/
if (ioctl(comhandle,TIONREAD,&charsrdy))
{
printf("\n\r** ERROR ** IOCTL returned an error\n\r");
return;
}
/*
Get the number of chars ready out of the read buffer
*/
if (charsrdy)
{
if (get_record(charsrdy,temprxbuf,FALSE) != charsrdy)
printf("\n\r** ERROR ** could not clear the RX buffer\n\r");
}
#endif
}
/*
get_serial_record - Get a record from the serial IO Buffer
Prototype in: serial.h
Parameters Passed: rxbuf - pointer to a buffer to store the
received characters
recsize - number of characters to receive
outputmode - POINT, CONTINUOUS or STREAM
Return Value: If successful, returns recsize
else, RXERRORS if Rx errors were detected while
receiving data
Remarks: A record of data has the MSB of the first
character set to a 1. The routine verifies that
the first character received is in PHASE.
NOTE that this routine returns the MOST RECENT
record from the Buffer...if more than one record
is present, old records are discarded.
*/
int get_serial_record(rxbuf, recsize, outputmode)
unsigned char * rxbuf;
short recsize;
short outputmode;
{
short charsread;
#ifdef DEBUG_SKIPSERIAL
/*
Fill in the Record with the Data Read
*/
for (charsread = 0; charsread < recsize; charsread++)
{
rxbuf[charsread] = charsread;
}
/*
Set the Phasing Bit
*/
rxbuf[0] |= 0x80;
return(recsize);
#else
char * rxbufinptr;
short tempcharsneeded;
int charsrdy = 0;
short numrecordsrdy;
int totalcharsrdy;
/*
STREAM mode is NOT allowed if using DOS
..DOS does not support interrupt driven serial I/O
*/
#ifdef DOS
if (outputmode == STREAM)
{
printf("\n\r** ERROR ** STREAM mode is not supported by DOS\n\r");
return(RXERRORS);
}
#endif
/*
Setup some variables
*/
totalcharsrdy = 0;
/*
Get the Number of Chars Ready
Note that this approach reads the MOST RECENT record received by the
host computer by manually emptying the operating system's serial
character buffer until the last valid record
*/
#ifdef UNIX
if (ioctl(comhandle,TIONREAD,&charsrdy))
{
printf("\n\r** ERROR ** IOCTL returned an error\n\r");
return(RXERRORS);
}
#endif
/*
Add the current amount of chars ready to the total
*/
totalcharsrdy += charsrdy;
/*
Now take care of 3 cases...
1) charsrdy = recsize
2) charsrdy > recsize
3) charsrdy < recsize
*/
if (totalcharsrdy >= recsize)
{
/*
Determine the number of records available
If the number is greater than 1 then throw away old records
*/
if (recsize)
numrecordsrdy = totalcharsrdy/recsize;
else
{
printf("\n\r** ERROR ** illegal parameter in call to get_serial_record\n\r");
return(RXERRORS);
}
/*
Read the most recent complete Block from the Port
*/
while (numrecordsrdy--)
{
if (get_record(recsize,(char *) rxbuf,TRUE) == RXERRORS)
return(RXERRORS);
}
}
else
{
return(get_record(recsize,(char *) rxbuf,TRUE));
}
/*
Everything is OK, so return the recsize
*/
return(recsize);
#endif /* DEBUG_SKIPSERIAL */
}
/*
get_record - Get a record from the serial port
Prototype in: serknr.c
Parameters Passed:
short charsneeded - number of chars needed
char * rxbufinptr - place to store the chars
short checkphasebit - check phase on/off
Return Value: If successful, returns recsize
else, RXERRORS if Rx errors were detected while
receiving data
Remarks: A record of data has the MSB of the first
character set to a 1. The routine verifies that
the first character received is in PHASE.
*/
int get_record(charsneeded, rxbufinptr, checkphasebit)
short charsneeded;
char * rxbufinptr;
short checkphasebit;
{
short tempcharsneeded;
char * temprxbufinptr = rxbufinptr;
short charsread;
/*
Now Try to Read the required amount of characters...
Note that we do NOT assume that the read will block until
required number of characters are available because the
read can return with less than the number of characters
needed
*/
tempcharsneeded = charsneeded;
while(tempcharsneeded)
{
charsread = read(comhandle,temprxbufinptr,tempcharsneeded);
if (charsread <= 0) /* check to make sure we got a least 1 */
{
printf("\n\r** ERROR ** serial receive timeout \n\r");
return(RXERRORS);
}
/*
Keep track of the number of characters read
*/
if (charsread != -1)
{
temprxbufinptr += charsread;
tempcharsneeded -= charsread;
}
/*
Verify that the first character has the Phasing Bit Set
and all the remaining characters do NOT have the Phasing Bit
Set ..only if enabled
*/
if (checkphasebit && !(*rxbufinptr & 0x80))
{
printf ("\n\r** ERROR ** phasing bit error\n\r");
return(RXERRORS);
}
}
return(charsneeded);
}
/*
send_serial_cmd - Send Serial Command to the Bird port
Prototype in: serial.h
Parameters Passed: cmd - string to send to the serial port
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;
{
char cmdbytessent;
#ifdef DEBUG_SKIPSERIAL
#ifdef DEBUG_VIEWSERIAL
printf ("\n\rCOMMAND SENT: ");
for (cmdbytessent = 0; cmdbytessent < cmdsize; cmdbytessent++)
printf(" 0x%X",*cmd++);
printf ("\n\r");
#endif /* DEBUG_VIEWSERIAL */
return(cmdbytessent);
#else /* DEBUG_SKIPSERIAL */
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);
#ifdef DEBUG_VIEWSERIAL
printf ("\n\rRS232TOFBB COMMAND: 0x%X\n\r",rs232tofbbcmd);
#endif
write(comhandle,&rs232tofbbcmd,1);
}
#ifdef DEBUG_VIEWSERIAL
printf ("\n\rCOMMAND SENT: ");
for(cmdbytessent = 0;cmdbytessent < cmdsize; cmdbytessent++)
printf (" 0x%X",cmd[cmdbytessent]);
printf("\n\r");
#endif
cmdbytessent = write(comhandle,cmd,cmdsize);
#ifdef UNIX
ioctl(comhandle,TCFLSH,TIOCFLUSH); /* flush pending I/O chars */
#endif
return(cmdbytessent);
#endif /* DEBUG_SKIPSERIAL */
}
/*
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 receive errors
*/
int get_serial_char()
{
#ifdef DEBUG_SKIPSERIAL
static chr = 0;
#ifdef DEBUG_VIEWSERIAL
printf("\n\rCHAR RECEIVED: 0x%X\n\r",chr);
#endif
return(chr++);
#else
unsigned char chr;
if ((read(comhandle, &chr,1)) != -1)
{
#ifdef DEBUG_VIEWSERIAL
printf("\n\rCHAR RECEIVED: 0x%X\n\r",chr);
#endif
return(chr);
}
else
return(RXERRORS);
#endif /* DEBUG_SKIPSERIAL */
}
/*
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)
short chr;
{
#ifdef DEBUG_SKIPSERIAL
#ifdef DEBUG_VIEWSERIAL
printf("\n\rCHAR SENT: 0x%X\n\r",chr);
#endif
return(TRUE);
#else
#ifdef UNIX
int zeroint=0;
#endif
if ((write(comhandle,(char *) &chr,1)) == 1)
{
#ifdef UNIX
ioctl(comhandle,TCFLSH,TIOCFLUSH); /* flush pending I/O chars */
#endif
#ifdef DEBUG_VIEWSERIAL
printf("\n\rCHAR SENT: 0x%X\n\r",chr);
#endif
return(TRUE);
}
else
return(TXNOTEMPTY);
#endif /* DEBUG_SKIPSERIAL */
}
/*
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;
/*
Wait until a character is available
....leave loop if errors or character available
*/
while ((rxchar = get_serial_char()) == NODATAAVAIL);
/*
return if RX errors
*/
if (rxchar < 0)
return(RXERRORS);
/*
Everything is OK...return the character
*/
return(rxchar);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -