日期:2014-05-16  浏览次数:20723 次

各位仁兄有没有ttytest.c的源程序阿
各位仁兄有没有ttytest.c的源程序阿?
能不能发我一份
谢谢了
tiplinky2002@yahoo.com.cn

------解决方案--------------------
/**************ttytest.c*******************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "rs232.h "
main(int argc, char *argv[])
{

int ret,portno,nWritten,nRead;
char buf[256];
portno=0;
while(1)
{
ret=OpenCom(portno, "/dev/ttyS1 ",115200);
if(ret==-1)
{
perror( "The /dev/ttyS1 open error. ");
exit(1);
}
nWritten=ComWrt(portno, "abc ",3);
printf( "\n/dev/ttyS1 has send %d chars!\n ",nWritten);
printf( "\nRecieving data!***\n ");
fflush(stdout);
nRead=ComRd(0,buf,256,3000);
if(nRead> 0)
{
printf( "*****OK\n ");
}
else
printf( "Timeout\n ");
if((ret=CloseCom(portno)==-1))
{
perror( "Close com ");
exit(1);
}
printf( "\n\n ");
}
printf( "Exit now.\n ");
return;
}

/******************************rs232********************************/

int ComRd(int portNo, char buf[], int maxCnt,int Timeout)
{
int actualRead = 0;
fd_set rfds;
struct timeval tv;
int retval;

if (!ports[portNo].busy)
{
assert(0);
}

/* camp on the port until data appears or 5 seconds have passed */
FD_ZERO(&rfds);
FD_SET(ports[portNo].handle, &rfds);
tv.tv_sec = Timeout/1000;
tv.tv_usec = (Timeout%1000)*1000;
retval = select(16, &rfds, NULL, NULL, &tv);

if (retval)
{
actualRead = read(ports[portNo].handle, buf, maxCnt);
}