the read thread :
/* SerialRead
*
- Description: This function does the setup of the Serial port 0 and then uses it to
- receive data which were sent into the serial port 0.
-
- Called by: root
-
*/
void SerialRead(ULONG thread_input)
{
int zeroFd, result, status, baudrate;
int bytetorecv = 0;
int numBytesRcvd= 0;
int numBytesSent= 0;
#ifdef LOOP_FOREVER_IN_THIS_TEST
unsigned int iteration = 0;
#endif
#ifdef OLD_SERIAL_API
int iOptions;
#else
static int first_open = 1;
struct ioflags_t iOptions;
struct termios tios;
struct serial_cntr_t prev_counters;
struct serial_cntr_t curr_counters;
memset( &prev_counters, 0, sizeof(prev_counters));
#endif
printf ("serial recv thread created.
");
// char sendBuffer=setSendBuffer(BufferSizeForSendAndReceive);
while (1/SWserlTests[currentop].szData/)
{
/ Check status. /
// status = tx_semaphore_get(&semaphore_0, TX_WAIT_FOREVER);
if (status != TX_SUCCESS)
{
printf("Unable to get semaphore for com 0.
");
goto err;
}
else
{
zeroready = 0; / com zero not ready */
zeroFd = open (TEST_COM0, SWserlTests[currentop].mode); /* because hardware handshake is needed */
if (zeroFd <0)
{
printf ("Can't open %s [%d].
", TEST_COM0, getErrno());
goto err;
}
else
{
#if IS_DTR_DSR_PRESENT == TRUE
#ifdef OLD_SERIAL_API
do
{
/* get the DCD state */
result = ioctl(zeroFd, SIO_GET_STATUS_DCD, &iOptions);
if (result < 0)
{
printf ("ioctl(SIO_GET_STATUS_DCD) errno[%d]
", getErrno());
goto err;
}
/* get the DSR state */
result = ioctl(zeroFd, SIO_GET_STATUS_DSR, &status);
if (result < 0)
{
printf ("ioctl(SIO_GET_STATUS_DSR) errno[%d]
", getErrno());
goto err;
}
if ((status == 0) && (iOptions == 0))
{
printf ("Connect Serial cable!
" );
// tx_thread_sleep(100);
}
}
while((status == 0) && (iOptions == 0));
#else
do
{
tcgetstats( zeroFd, &ser_status );
if ( (ser_status.mstat & (MS_DCD | MS_DSR)) == 0 )
{
printf ("Connect Serial cable!
");
// tx_thread_sleep(100);
}
}
while( (ser_status.mstat & (MS_DCD | MS_DSR)) == 0 );
#endif /* OLD_SERIAL_API /
#endif / IS_DTR_DSR_PRESENT */
/* set the baud rate */
baudrate = SWserlTests[currentop].baudrate;
#ifdef OLD_SERIAL_API
result = ioctl(zeroFd, SIO_BAUD_SET, &baudrate);
if (result < 0)
{
printf ("ioctl(Baud set) errno[%d]
", getErrno());
goto err;
}
#else
tcgetattr(zeroFd, &tios);
cfsetospeed(&tios, baudrate);
#endif
/* set other parameters */
iOptions = SWserlTests[currentop].ioflags;
#ifdef OLD_SERIAL_API
result = ioctl(zeroFd, SIO_HW_OPTS_SET, &iOptions);
if (result < 0)
{
printf ("ioctl(Line options) failure: errno[%d]
", getErrno());
goto err;
}
#else
tios.c_cflag = iOptions.cflags;
tios.c_iflag = iOptions.iflags;
result = tcsetattr( zeroFd, TCSANOW, &tios );
if (result < 0) {
printf ("ioctl(Set attributes) failure: errno[%d]
", getErrno());
goto err;
}
if ( cfgetospeed(&tios) != baudrate ) {
printf ("ioctl(Baud set) errno[%d]
", getErrno());
return;
}
if ( (tios.c_cflag != iOptions.cflags) ||
(tios.c_iflag != iOptions.iflags) )
{
printf ("ioctl(Flag settings) failure errno[%d]
", getErrno());
goto err;
}
#endif
bytetorecv = BufferSizeForSendAndReceive;
// bytetorecv = SWserlTests[currentop].szData;
#ifdef OLD_SERIAL_API
result = ioctl(zeroFd, SIO_GET_STATUS_ERROR, &iOptions);
if (iOptions != 0)
{
printf ("Error condition [0x%x].
", iOptions);
ioctl(zeroFd, SIO_CLEAR_STATUS_ERROR, &iOptions);
}
#else
tcgetcounters(zeroFd, &curr_counters);
if (first_open) {
prev_counters = curr_counters;
first_open = 0;
}
if (prev_counters.norun != curr_counters.norun)
{
printf ("Error condition [Overrun].
");
}
if (prev_counters.noflow != curr_counters.noflow)
{
printf ("Error condition [Overflow].
");
}
if (prev_counters.nframe != curr_counters.nframe)
{
printf ("Error condition [Framing Error].
");
}
if (prev_counters.nparity != curr_counters.nparity)
{
printf ("Error condition [Parity Error].
");
}
if (prev_counters.nbreak != curr_counters.nbreak)
{
printf ("Error condition [Break].
");
}
prev_counters = curr_counters;
#endif
zeroready = 1;
// status = tx_semaphore_put(&semaphore_0);
numBytesRcvd = 0;
// com0NotFinish = 1;
do
{
if (numBytesRcvd < bytetorecv)
{
bufstat("R1 ",zeroFd);
result = read(zeroFd, (rcvBuffer0 + numBytesRcvd), (bytetorecv - numBytesRcvd));
bufstat("R2 ",zeroFd);
if (result < 0)
{
printf ("read failed [%d].
", getErrno() );
}
else
{
numBytesRcvd += result;
printf ("serial num bytes recvd [%d]=%c.
", numBytesRcvd, (char )(rcvBuffer0 + numBytesRcvd - result) );
}
INT32 var_yellow,var_green;
char cmd[100];
char rc=(char *)*(rcvBuffer0 + numBytesRcvd - result);
manGetINT32(EXAMPLE_greenLedOn, &var_green, NULL, MAN_TIMEOUT_FOREVER);
manGetINT32(EXAMPLE_yellowLedOn, &var_yellow, NULL, MAN_TIMEOUT_FOREVER);
switch (rc)
{
case 'D':// tasto down
if(var_green==2)
sendTrap();
manSetINT32(EXAMPLE_greenLedOn, 1, NULL, MAN_TIMEOUT_FOREVER);
break;
case 'U':// tasto up
if(var_green==1)
sendTrap();
manSetINT32(EXAMPLE_greenLedOn, 2, NULL, MAN_TIMEOUT_FOREVER);
break;
case 'O':// led on
manSetINT32(EXAMPLE_yellowLedOn, 1, NULL, MAN_TIMEOUT_FOREVER);
break;
case 'o':// led off
manSetINT32(EXAMPLE_yellowLedOn, 2, NULL, MAN_TIMEOUT_FOREVER);
break;
case 'Q':// richiesta comandi
if(g_delta)
{
// while (bytetosend) // attendo invio ;
sendBuffer[0]=g_delta;// ultimo set da snmp manager
bytetosend=1;
// tx_thread_sleep(1); /* wait for send to finish completely */
SerialWrite1(zeroFd,sendBuffer,1);
// while (bytetosend) // attendo invio ;
g_delta=0;
break;
}
default:
/*
if (result > 0)
{
while (bytetosend) // attendo invio
;
sendBuffer[0]=rc;// ultimo set da snmp manager
bytetosend=1;
}
*/
break;
}
// SerialWrite(zeroFd,&rc,1);
}
}
while (numBytesRcvd < bytetorecv);
numBytesRcvd = 0;
numBytesSent = 0;
result = close (zeroFd);
if (result<0)
{
printf ("Close failed [%d].
", getErrno() );
}
currentop++;
#ifdef LOOP_FOREVER_IN_THIS_TEST
if (SWserlTests[currentop].szData == 0)
{
currentop = 0;
printf("
Iteration %d
", ++iteration);
}
#endif
}
} /* end if /
} / end while /
// tx_thread_sleep(1000); / wait for send to finish completely */
printf ("Thread Serial One complete.
");
return;
err:
return;
}
the write thread will write in this way but it seems to me it is flushing the input buffer, and also getting serial port stats seems to flush it
void SerialWrite1(int oneFd, char *psendBuffer, int pbytetosend)
{
int result;
bufstat("W1 ",oneFd);
result = write (oneFd, psendBuffer, pbytetosend);
bufstat("W2 ",oneFd);
if (result < 0)
{
printf ("write failed [%d].
", getErrno());
}
else
{
if (result)
printf("serial char sent ‘%c’
",psendBuffer[0]);
}
}