他的回复:
你好: 你们工程师刚告诉我一个办法,就是在现有代码中加入波特率设置的代码,我已加了,试了下,还是不行,以下是运行后状态和代码;//int test_huawei(void);int test_ar502h_serial(void);//INT_F _tmain(INT_F argc, _TCHAR* argv[])INT_F main(void){ test_ar502h_serial(); return 0;}#include #include #include #include #include #include inet.h>#include #include #include ioctl.h>#include #include #include //#include "appBase.h"//argv[1]://设备名;//argv[2]://操作;//argv[3]://写入数据缓冲区;//argv[4]://发送数据后的等待时间;//int test_ar502h_serial(int argc, char *argv[])int test_ar502h_serial(void){ char* pFilePath = NULL; char* pOp = NULL; char* pUartWrite = NULL; unsigned char aucUartRcvBuf[256] = {0}; int fd; //int ret = 0; int iDataLen = 0; int i = 0; unsigned int ulPresent = 0; struct termios uart_termios; int ret=0, err=0,test_ret=0; int nSpeed = 9600; //----------------------------test--------------------------// char devfile[100]={0}; char dev_operator[100]={0}; char dev_databuffer[100]={0}; int opera_sleeptime = 1;//us strcpy(devfile,"/dev/ttyRS2"); strcpy(dev_operator,"write"); strcpy(dev_databuffer,"0123456789"); opera_sleeptime = 1000*500;//500ms //----------------------------test--------------------------// pFilePath = devfile;//argv[1];//设备; pOp = dev_operator;//argv[2];//操作; //"/dev/rs485Ctrl1"使用到了485控制角,切换; //argv[3]; 要发送的数据; //argv[4]: 发送完成后,切换的时间;us //"/dev/ttyRS"是个发送缓冲区;先要将数据放到"/dev/ttyRS"区里来; //发送数据完成后,要将缓冲区的数据发完,再切换到接收状态; int crlfd; char ctrlvalue = 0; int sleeptime = 1; printf("start-v1.2.\n"); //pFilePath == /dev/ttyRS //必须是这个才能操作;/dev/ttyRS1 if (0 == strncmp(pFilePath, "/dev/ttyRS", strlen("/dev/ttyRS")) || 0 == strncmp(pFilePath, "/dev/plugin/ttyRS", strlen("/dev/plugin/ttyRS"))) { printf("flag1\n"); /* 打开文件 */ fd = open(pFilePath, O_RDWR);//存储发送数据的缓冲区; if (fd 0) { printf("open file[%s] fail \r\n", pFilePath); return -1; } //-------------设置波特率及相关参数------------------------- fcntl(fd, F_SETFL, 0); //get serial configuration if ((err = tcgetattr(fd, &uart_termios)) != 0) { printf("tcgetattr, err = %s\n", strerror(errno)); close(fd); return err; } uart_termios.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON); uart_termios.c_oflag &= ~OPOST; uart_termios.c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN); uart_termios.c_cflag &= ~(CSIZE|PARENB); uart_termios.c_cflag |= CS8; uart_termios.c_cflag &= ~CRTSCTS;//no flow control switch(nSpeed) { case 2400: cfsetispeed(&uart_termios,B2400);//设置输入速度 cfsetospeed(&uart_termios,B2400);//设置输出速度 break; case 4800: cfsetispeed(&uart_termios,B4800); cfsetospeed(&uart_termios,B4800); break; case 9600: cfsetispeed(&uart_termios,B9600); cfsetospeed(&uart_termios,B9600); break; case 115200: cfsetispeed(&uart_termios,B115200); cfsetospeed(&uart_termios,B115200); break; case 19200: cfsetispeed(&uart_termios,B19200); cfsetospeed(&uart_termios,B19200); break; case 38400: cfsetispeed(&uart_termios,B38400); cfsetospeed(&uart_termios,B38400); break; case 57600: cfsetispeed(&uart_termios,B57600); cfsetospeed(&uart_termios,B57600); break; default: cfsetispeed(&uart_termios,B9600); cfsetospeed(&uart_termios,B9600); break; } //set serial configuration if (tcsetattr(fd, TCSANOW, &uart_termios)0) { printf("tcsetattr fail\n"); } tcflush(fd, TCIOFLUSH); printf("In speed: %d\n", (int)cfgetispeed(&uart_termios)); printf("Out speed: %d\n", (int)cfgetospeed(&uart_termios)); //-----------------------配置波特率及相关参数-------------------// /* 操作文件 */ if (0 == strncmp(pOp, "write", strlen("write"))) { printf("flag2\n"); pUartWrite = dev_databuffer;//argv[3]; sleeptime = opera_sleeptime;//lib_atoi(argv[4]); if( 0 == strncmp(pFilePath, "/dev/ttyRS1", strlen("/dev/ttyRS1"))) { /* 切换到发送*/ crlfd = open("/dev/rs485Ctrl1", O_RDWR | O_NOCTTY | O_NONBLOCK);//切换发送角; if (crlfd 0) { printf("open file rs485Ctrl1 fail \r\n"); return -1; } ctrlvalue = 0; iDataLen = write(crlfd, &ctrlvalue, sizeof(ctrlvalue));//切换到发送; if (0 > iDataLen) { printf("write file rs485Ctrl1 fail \r\n"); return -1; } printf("flag3\n"); /* 发送报文*/ iDataLen = write(fd, pUartWrite, strlen(pUartWrite));//把数据发到缓冲区里; if (0 > iDataLen) { printf("write file[%s] fail \r\n", pFilePath); } sleep(sleeptime);//等待一段时间后; printf("flag4\n"); /*切换到接收模式*/ ctrlvalue = 1; iDataLen = write(crlfd, &ctrlvalue, sizeof(ctrlvalue));//切换到接收; if (0 > iDataLen) { printf("write file rs485Ctrl1 fail \r\n"); return -1; } } else if(0 == strncmp(pFilePath, "/dev/ttyRS2", strlen("/dev/ttyRS2"))) { printf("flag5\n"); /* 切换到发送*/ crlfd = open("/dev/rs485Ctrl2", O_RDWR | O_NOCTTY | O_NONBLOCK); if (crlfd 0) { printf("open file rs485Ctrl fail \r\n"); return -1; } ctrlvalue = 0; iDataLen = write(crlfd, &ctrlvalue, sizeof(ctrlvalue)); if (0 > iDataLen) { printf("write file rs485Ctrl2 fail \r\n"); return -1; } printf("flag6\n"); /* 发送报文*/ iDataLen = write(fd, pUartWrite, strlen(pUartWrite)); if (0 > iDataLen) { printf("write file[%s] fail \r\n", pFilePath); } printf("flag7\n"); /*切换到接收模式*/ ctrlvalue = 1; iDataLen = write(crlfd, &ctrlvalue, sizeof(ctrlvalue)); if (0 > iDataLen) { printf("write file rs485Ctrl2 fail \r\n"); return -1; } } else { printf("flag8\n"); iDataLen = write(fd, pUartWrite, strlen(pUartWrite)); if (0 > iDataLen) { printf("write file[%s] fail \r\n", pFilePath); } } } else if (0 == strncmp(pOp, "read", strlen("read")))//读操作; { printf("flag9\n"); if( 0 == strncmp(pFilePath, "/dev/ttyRS1", strlen("/dev/ttyRS1"))) { printf("flag10\n"); crlfd = open("/dev/rs485Ctrl1", O_RDWR | O_NOCTTY | O_NDELAY); if (crlfd 0) { printf("open file rs485Ctrl1 fail \r\n"); return -1; } ctrlvalue = 1; iDataLen = write(crlfd, &ctrlvalue, sizeof(ctrlvalue)); if (0 > iDataLen) { printf("write file rs485Ctrl1 fail \r\n"); return -1; } printf("flag11\n"); } else if ( 0 == strncmp(pFilePath, "/dev/ttyRS2", strlen("/dev/ttyRS2")) ) { printf("flag12\n"); crlfd = open("/dev/rs485Ctrl2", O_RDWR | O_NOCTTY | O_NONBLOCK); if (crlfd 0) { printf("open file rs485Ctrl2 fail \r\n"); return -1; } ctrlvalue = 1; iDataLen = write(crlfd, &ctrlvalue, sizeof(ctrlvalue)); if (0 > iDataLen) { printf("write file rs485Ctrl2 fail \r\n"); return -1; } printf("flag13\n"); } else { ; } printf("flag14\n"); iDataLen = read(fd, aucUartRcvBuf, sizeof(aucUartRcvBuf)); if (0 > iDataLen) { printf("read file[%s] fail \r\n", pFilePath); } printf("read data, len[%d]: \r\n", iDataLen); printf("flag15\n"); for (i = 0; i iDataLen; i++) { if ((0 != i) && (0 == (i % 16))) { printf("\r\n"); } printf("%02X ", aucUartRcvBuf[i]); } printf("\r\n"); printf("flag16\n"); } else { printf("flag17\n"); printf("not support operation[%s]\r\n", pOp); } /* 关闭文件 */ printf("flag18\n"); close(fd ); } else { printf("flag19\n"); printf("no device.\n"); } printf("flag20\n"); return 0;}