|
楼主 |
发表于 2005-5-19 09:50:23
|
显示全部楼层
-------------MyCom.h---------------------------
/*This program under GPL
*MyCom.h
*operate serial port function
*2005-5-6
*write by huangguixiang
*/
#include <stdio.h>
//serial port struct
typedef struct{
char prompt; //prompt after reciving data
int baudrate; //baudrate
char databit; //data bits, 5,6,7,8
char debug; //debug mode, 0:none,1:debug
char echo; //echo mode, 0:none,1:hardware,2:software
char fctl; //flow control, 0:none,1:hardware,2:software
char tty; //tty:0,1,2,3,4,5,6,7
char parity; //parity 0:none,1dd,2:even
char stopbit; //stop bits, 1,2
const int reserved; //reserved, must be zero
}portinfo_t;
typedef portinfo_t *pportinfo_t;
/*open port
*pportinfo: infomations to set
*/
int PortOpen(pportinfo_t pportinfo);
/*set port
*fdcom: port descrip
*pportinfo: infomations to set
*/
int PortSet(int fdcom, const pportinfo_t pportinfo);
/*close port
*fdcom: port descrip
*/
void PortClose(int fdcom);
/*send data
*fdcom: port descrip
*data: data to send
*datalen: data length
*return length of really send
*/
int PortSend(int fdcom, char *data, int datalen);
/*recieve data
*fdcom: port descrip
*data: recieve buf
*datalen: recieve lenght
*baudrate: baudrate
*return length of really read
*/
int PortRecv(int fdcom, char *data, int datalen, int baudrate);
--------------------------MyCom.c-------------------------------------------
/*MyCom.c
*
*write by huangguixiang
*2005-5-6
*/
#include <stdio.h> //printf
#include <fcntl.h> //open
#include <string.h> //bzero
#include <stdlib.h> //exit
#include <sys/times.h> //times
#include <sys/types.h> //pid_t
#include <termios.h> //termios, tcgetattr(), tcsetattr()
#include <unistd.h>
#include <sys/ioctl.h> //ioctl
#include "MyCom.h"
#define TTY_DEV "/dev/ttyS" //port path
#define TIMEOUT_SEC(buflen, baud) (buflen*20/baud+2) //recieve timeout
#define TIMEOUT_USEC 0
/*************************************
*get port name
*************************************/
char *get_ptty(pportinfo_t pportinfo)
{
char *ptty;
switch(pportinfo->tty)
{
case '0':{ptty= TTY_DEV"0"; }break;
case '1':{ptty= TTY_DEV"1"; }break;
case '2':{ptty= TTY_DEV"2"; }break;
}
return(ptty);
}
/********************************
*baudrate charge function
*******************************/
int convbaud(unsigned long int baudrate)
{
switch(baudrate)
{
case 2400:
return B2400;
case 4800:
return B4800;
case 9600:
return B9600;
case 19200:
return B19200;
case 38400:
return B38400;
case 57600:
return B57600;
case 115200:
return B115200;
default:
return B9600;
}
}
/**********************************
*Setup comm attr
*fdcom: port file descrip,
*pportinfo: informations to set
**********************************/
int PortSet(int fdcom, const pportinfo_t pportinfo)
{
struct termios termios_old, termios_new;
int baudrate, tmp;
char databit, stopbit, parity, fctl;
bzero(&termios_old, sizeof(termios_old));
bzero(&termios_new, sizeof(termios_new));
cfmakeraw(&termios_new);
tcgetattr(fdcom, &termios_old); //get the serial port attributions
/*set port attributions*/
//baudrates
baudrate= convbaud(pportinfo->baudrate);
cfsetispeed(&termios_new, baudrate); //fill serial port in baudrate
cfsetospeed(&termios_new, baudrate); //fill serial port out baudrate
termios_new.c_cflag|=CLOCAL; //control pattern, about terminal
termios_new.c_cflag|=CREAD; //control pattern, about read data
//flow contol
fctl= pportinfo->fctl;
switch(fctl)
{
case '0':{termios_new.c_cflag &= ~CRTSCTS;}break; //no flow control
case '1':{termios_new.c_cflag |= CRTSCTS;}break; //hardware flow control
case '2':{termios_new.c_cflag |= IXON|IXOFF|IXANY;}break; //software flow control
}
//data bits
termios_new.c_cflag &= ~CSIZE; //control pattern, about char size
databit= pportinfo->databit;
switch(databit)
{
case '5':
termios_new.c_cflag |= CS5;
case '6':
termios_new.c_cflag |= CS6;
case '7':
termios_new.c_cflag |= CS7;
default:
termios_new.c_cflag |= CS8;
}
//parity check
parity- pportinfo->parity;
switch(parity)
{
case '0':{
termios_new.c_cflag &= ~PARENB; //no parity check
}break;
case '1':{
termios_new.c_cflag !=~PARENB; //odd check
termios_new.c_cflag &= ~PARODD;
}break;
case '2':{
termios_new.c_cflag |= PARENB; //even check
termios_new.c_cflag |= PARODD;
}break;
}
//stop bits
stopbit= pportinfo->stopbit;
if(stopbit == '2')
termios_new.c_cflag |=CSTOPB; //2 stop bits
else
termios_new.c_cflag &= ~CSTOPB; //1 stop bits
//other attributions default
termios_new.c_oflag &= ~OPOST; //out pattern, oragial data
termios_new.c_cc[VMIN]= 1; //control char, need read minsize
termios_new.c_cc[VTIME]= 1; //control char, need read first char waiting times unit1/10)second
tcflush(fdcom, TCIFLUSH); //overflow can recieve but not read
tmp= tcsetattr(fdcom, TCSANOW, &termios_new); //set new attributions, TCSANOW: now active effert
tcgetattr(fdcom, &termios_old);
return(tmp);
}
/********************************************
*Open serial port
*tty: port no, ttyS0, ttyS1,........
*return serial port descrip char
********************************************/
int PortOpen(pportinfo_t pportinfo)
{
int fdcom;
char *ptty;
ptty= get_ptty(pportinfo);
//fdcom= open(ptty, O_RDWR|O_NOCTTY|O_NONBLOCK|O_NDELAY);
fdcom= open(ptty, O_RDWR|O_NOCTTY|O_NONBLOCK);
return (fdcom);
}
/*******************************************
*Close serial port
********************************************/
void PortClose(int fdcom)
{
close(fdcom);
}
/***************************************
*Send data
*fdcom:
*data:
*datalen:
*return
***************************************/
int PortSend(int fdcom, char *data, int datalen)
{
int len= 0;
len= write(fdcom, data, datalen); //really write length
if(len == datalen)
return(len);
else
{
tcflush(fdcom, TCOFLUSH);
return -1;
}
}
/**********************************
*Receive data
*return really read bytes
*********************************/
int PortRecv(int fdcom, char *data, int datalen, int baudrate)
{
int readlen, fs_sel;
fd_set fs_read;
struct timeval tv_timeout;
FD_ZERO(&fs_read);
FD_SET(fdcom, &fs_read);
tv_timeout.tv_sec= TIMEOUT_SEC(datalen, baudrate);
tv_timeout.tv_usec= TIMEOUT_USEC;
fs_sel= select(fdcom+1, &fs_read, NULL, NULL, &tv_timeout);
if(fs_sel)
{
readlen= read(fdcom, data, datalen);
return(readlen);
}
else
return(-1);
return(readlen);
}
----------------------------testMyCom.c----------------------------
//************************test MyCom ************************//
#include "MyCom.h"
#include <stdio.h>
#include <termios.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
int fdcom, i, SendLen, RecvLen;
struct termios termios_cur;
char RecvBuf[10];
portinfo_t portinfo={
'0', //print prompt after receiving
9600, //baudrate:9600
'8', //databit:8
'0', //debugff
'0', //echoff
'0', //flow control:software
'0', //default tty:COM1=ttyS0
'0', //parity:none
'1', //stopbit:1
0 //reserved
};
if(argc != 2)
{
printf("Usage:<type 0 --send 1--receive>\n>");
printf("eg:");
printf("MyPort 0");
exit(-1);
}
fdcom= PortOpen(&portinfo);
if(fdcom<0)
{
printf("Errorpen serial prot error.\n");
exit(1);
}
PortSet(fdcom, &portinfo);
if(atoi(argv[1])==0)
{
//send data
for(i=0; i<100; i++)
{
SendLen= PortSend(fdcom, "1234567890", 10);
if(SendLen>0)
printf("No %d send %d data 1234567890.\n", i, SendLen);
else
printf("Error:send failed.\n");
sleep(1);
}
PortClose(fdcom);
}
else
{
for(;;)
{
RecvLen= PortRecv(fdcom, RecvBuf, 10, portinfo.baudrate);
if(RecvLen>0)
{
for(i=0; i<RecvLen; i++)
printf("Receive data No %d is %x.\n", i, RecvBuf);
printf("Total frame length is %d.\n", RecvLen);
}
else
printf("Error:receive error.\n");
sleep(2);
}
}
return(0);
}
--------------------------------------end----------------------------
上面就是我测试通信的源代码了,主要程序是网上下得代码。
在PC上LINUX下能正常通信,到2410的板子上,就不行了。
请帮帮我吧!!!!!!!!! |
|