|
情况是这样的,我从RHL9 发命令给单片机,然后单片机返回,4组256字节的数据,其中大部分数据正确,只有几个字节出错;但是在win2000下却完全正确。
代码如下:
/*
2005年6月1号。今天是儿童节,节日快乐啊。~_~
此程序参考了2002年自由软件基金出版的Serial Programming Guide for POSIX Operating Systems
Copy Right by 李伟清。
*/
#include <stdio.h> /*标准输入输出定义*/
#include <stdlib.h> /*标准函数库定义*/
#include <unistd.h> /*Unix标准函数定义*/
#include <sys/types.h> /**/
#include <sys/stat.h> /**/
#include <sys/ioctl.h> /**/
#include <fcntl.h> /*文件控制定义*/
#include <termios.h> /*PPSIX终端控制定义 具体请看<bits/termios.h>*/
#include <errno.h> /*错误号定义*/
#define FALSE -1
#define TRUE 0
/*****************定义命令***********************
#define CMD_NORM 0x30 //0-正常工作
#define CMD_T9312 0x31 //1-测试9312
#define CMD_RAD 0x32 //2-读取AD数据
#define CMD_T3517 0x33 //3-测试3517
#define CMD_T5510 0x34 //4-测试5510
#define CMD_TCLK 0x35 //5-测试时钟
#define CMD_TxPAN 0x36 //6-测试X通道
#define CMD_TyPAN 0x37 //7-测试Y通道
#define CMD_T9752 0x38 //8-测试9752
#define CMD_DxPAN 0x39 //9-调试X通道
#define CMD_DyPAN 0x3A //10-调试Y通道
unsigned char cmd[11]={0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x3A};
*******************************************/
/**
*@breif 打开串口
*/
int OpenDev(char *Dev)
{
int fd = open( Dev, O_RDWR ); //| O_NOCTTY | O_NDELAY
if (-1 == fd)
{ /*设置数据位数*/
perror("Can't Open Serial Port");
return -1;
}
else
return fd;
}
//set_serial_attr(int fd,&struct termios)
// { }
/**
*@breif main()
*/
int main(int argc, char *argv[])
{
int fd;
struct termios options;
int i,j,counter=0;
int nwrite=0,nread=0;
unsigned char lastcmd=0x00,cmd[11]={0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x3A};
unsigned char buff[2048];
unsigned char c='c',nbytes_in_buffer=0;
char *dev ="/dev/ttyS0";
fd = OpenDev(dev);
if (fd>0)
{
if ( tcgetattr( fd,&options) != 0) //get fd's attributes
{
perror("get fd's attributes error 1");
exit(0);
}
options.c_ispeed=B9600;
options.c_ospeed=B9600;
options.c_cflag |= (CLOCAL | CREAD);
options.c_cflag &= ~PARENB; /* 无奇偶校验,1位停止位,8位数据位。 */
options.c_cflag &= ~CSTOPB; // 1 stopB
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
// options.c_cflag &= ~CNEW_RTSCTS; //硬件流控制. disable hardware flow control:
//Raw input is unprocessed. Input characters are passed through exactly as they are received,
//when they are received. Generally you'll deselect the ICANON, ECHO, ECHOE, and ISIG options when using raw input:
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); //Raw input
//options.c_lflag |= (ICANON | ECHO | ECHOE | ISIG);
//options.c_lflag=0; //设置为非标准,不回应........什么是非标准,不回应? */
options.c_iflag |= ~(INPCK | ISTRIP); //输入 无奇偶校验
//Software flow control is enabled using the IXON, IXOFF, and IXANY constants:
// options.c_iflag |= (IXON | IXOFF | IXANY); //软件流控制.
//To disable software flow control simply mask those bits:
options.c_iflag &= ~(IXON | IXOFF | IXANY); //有软件流控制反而会出错!
//Processed output is selected by setting the OPOST option in the c_oflag member:
//options.c_oflag |= OPOST;
//Choosing Raw Output
//Raw output is selected by resetting the OPOST option in the c_oflag member:
//When the OPOST option is disabled, all other option bits in c_oflag are ignored.
options.c_oflag &= ~OPOST;
//Setting Read Timeouts.
// 具体请看2002年自由软件基金的Serial Programming Guide for POSIX Operating Systems。
options.c_cc[VTIME] = 1; //1*0.1s=0.1 seconds
options.c_cc[VMIN] = 255;//255; /*c_cc[]是unsigned char 类型. 具体请看<bits/termios.h>*/
if (tcsetattr(fd,TCSANOW,&options) != 0)//设置串口的属性attribute.
{
perror("tcsetattr of ttyS0 error 3");
exit(0);
}
}
else
{
printf("Can't Open Serial Port :%s!\n",dev);
exit(0);
}
// fcntl(fd,F_SETFL,FNDELAY); //去掉延时等待.
while(c!='Q')
{
/*clear(); clrscr();*/
printf("\n This is ETT5XYTEXT Programm for RedHat Linux9.\n");
printf("\n 请按照屏幕的提示,做出选择!\n");
printf("\n0. 按0键发送 \"正常工作\"命令"); printf(" 1. 按1键发送 \"测试9312\"命令\n");
printf("2. 按2键发送 \"读取AD数据\"命令"); printf(" 3. 按3键发送 \"测试3517\"命令\n");
printf("4. 按4键发送 \"测试5510\"命令"); printf(" 5. 按5键发送 \"测试时钟\"命令\n");
printf("6. 按6键发送 \"测试X通道\"命令"); printf(" 7. 按7键发送 \"测试Y通道\"命令\n");
printf("8. 按8键发送 \"测试9752\"命令"); printf(" 9. 按9键发送 \"调试X通道\"命令\n");
printf("10. 按a键发送 \"调试Y通道\"命令"); printf(" 11. 按Q键 \"离开我\"命令\n");
printf("\n 如果程序出显异常,请按Ctr+C,强制退出程序!!\n");
printf("\n请按键盘 :");
c=getchar();
getchar(); /* getchar(); 用于接收回车键 */
printf("\nYou have pressed %c! ",c);
switch(c)
{
case '3':
nwrite = write(fd,cmd+3,1);
printf("已发送\"测试3517\"命令..................\n");
/*usleep(000); */
j=1;
// nread=0;
while(j<3)
{
nread = read(fd,buff,256);
printf("\n %d.nread=%d\n",j,nread);
for(i=0;i<nread;i++)
{
if(i==0)
; //空操作 ;
else if(i%32==0) /*每行显示32个数据 */
printf("-\n");
else if(i%16==0) /*每16个数据之间用","分隔 */
printf(", ");
printf("%02X ",buff);
}
//tcflush(fd,TCIFLUSH); //CLEAR SERIAL INPUT BUFFER
j++;
}
// j=1;
//usleep(500);
//tcflush(fd,TCIFLUSH); //CLEAR SERIAL INPUT BUFFER
while(j<5)
{
nread = read(fd,buff,256);
printf("\n %d.nread=%d\n",j,nread);
for(i=0;i<nread;i++)
{
if(i==0)
; //空操作 ;
else if(i%32==0) /*每行显示32个数据 */
printf("-\n");
else if(i%16==0) /*每16个数据之间用","分隔 */
printf(", ");
printf("%02X ",buff);
}
//tcflush(fd,TCIFLUSH); //CLEAR SERIAL INPUT BUFFER
j++;
}
//tcflush(fd,TCIFLUSH); //CLEAR SERIAL INPUT BUFFER
// usleep(1000); usleep(10);
printf("\n 看完后, 按回车键返回............");
getchar();
break;
case '4':
nwrite = write(fd,cmd+4,1);
printf("已发送\"测试5510\"命令..................?\n");
j=1;
// nread=0;
while(j<3)
{
nread = read(fd,buff,256);
printf("\n %d.nread=%d\n",j,nread);
for(i=0;i<nread;i++)
{
if((0!=i)&&(i%20==0))
printf("-\n");
else if((0!=i)&&(i%10==0))
printf(", ");
printf("%02X ",buff);
}
j++;
}
default:
if(c=='Q')
printf(" 即将退出!!\n",c);
else
printf(" %c是无效按键!!\n",c);
break;
} /*end switch(c) */
} /*end while(c!='Q') */
close(fd);
exit(0);
}
返回在xterm的结果如下:
1.nread=256
00 01 02 03 04 05 06 07 08 09 0D 0B 0C 0E 0F 10 , 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F 20 -
21 22 23 24 25 26 27 28 29 2A 2B 2C 2D 2E 2F 30 , 31 32 33 34 35 36 37 38 39 3A 3B 3C 3D 3E 3F 40 -
41 42 43 44 45 46 47 48 49 4A 4B 4C 4D 4E 4F 50 , 51 52 53 54 55 56 57 58 59 5A 5B 5C 5D 5E 5F 60 -
61 62 63 64 65 66 67 68 69 6A 6B 6C 6D 6E 6F 70 , 71 72 73 74 75 76 77 78 79 7A 7B 7C 7D 7E 7F 80 -
81 82 83 84 85 86 87 88 89 8A 8B 8C 8D 8E 8F 90 , 91 92 93 94 95 96 97 98 99 9A 9B 9C 9D 9E 9F A0 -
A1 A2 A3 A4 A5 A6 A7 A8 A9 AA AB AC AD AE AF B0 , B1 B2 B3 B4 B5 B6 B7 B8 B9 BA BB BC BD BE BF C0 -
C1 C2 C3 C4 C5 C6 C7 C8 C9 CA CB CC CD CE CF D0 , D1 D2 D3 D4 D5 D6 D7 D8 D9 DA DB DC DD DE DF E0 -
E1 E2 E3 E4 E5 E6 E7 E8 E9 EA EB EC ED EE EF F0 , F1 F2 F3 F4 F5 F6 F7 F8 F9 FA FB FC FD FE FF FF
2.nread=256
00 01 02 03 04 05 06 07 08 09 0D 0B 0C 0E 0F 10 , 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F 20 -
21 22 23 24 25 26 27 28 29 2A 2B 2C 2D 2E 2F 30 , 31 32 33 34 35 36 37 38 39 3A 3B 3C 3D 3E 3F 40 -
41 42 43 44 45 46 47 48 49 4A 4B 4C 4D 4E 4F 50 , 51 52 53 54 55 56 57 58 59 5A 5B 5C 5D 5E 5F 60 -
61 62 63 64 65 66 67 68 69 6A 6B 6C 6D 6E 6F 70 , 71 72 73 74 75 76 77 78 79 7A 7B 7C 7D 7E 7F 80 -
81 82 83 84 85 86 87 88 89 8A 8B 8C 8D 8E 8F 90 , 91 92 93 94 95 96 97 98 99 9A 9B 9C 9D 9E 9F A0 -
A1 A2 A3 A4 A5 A6 A7 A8 A9 AA AB AC AD AE AF B0 , B1 B2 B3 B4 B5 B6 B7 B8 B9 BA BB BC BD BE BF C0 -
C1 C2 C3 C4 C5 C6 C7 C8 C9 CA CB CC CD CE CF D0 , D1 D2 D3 D4 D5 D6 D7 D8 D9 DA DB DC DD DE DF E0 -
E1 E2 E3 E4 E5 E6 E7 E8 E9 EA EB EC ED EE EF F0 , F1 F2 F3 F4 F5 F6 F7 F8 F9 FA FB FC FD FE FF FF
3.nread=256
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 , 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 -
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 , 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 -
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 , 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 -
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 , 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 -
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 , 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 -
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 , 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 -
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 , 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 -
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 , 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
4.nread=256
FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF , FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF -
FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF , FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF -
FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF , FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF -
FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF , FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF -
FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF , FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF -
FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF , FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF -
FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF , FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF -
FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF , FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF
本来应该是:
1.nread=256
00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F, 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F 20 -
21 22 23 24 25 26 27 28 29 2A 2B 2C 2D 2E 2F ,30, 31 32 33 34 35 36 37 38 39 3A 3B 3C 3D 3E 3F 40 -
41 42 43 44 45 46 47 48 49 4A 4B 4C 4D 4E 4F, 50, 51 52 53 54 55 56 57 58 59 5A 5B 5C 5D 5E 5F 60 -
61 62 63 64 65 66 67 68 69 6A 6B 6C 6D 6E 6F, 70 71 72 73 74 75 76 77 78 79 7A 7B 7C 7D 7E 7F 80 -
81 82 83 84 85 86 87 88 89 8A 8B 8C 8D 8E 8F ,90, 91 92 93 94 95 96 97 98 99 9A 9B 9C 9D 9E 9F A0 -
A1 A2 A3 A4 A5 A6 A7 A8 A9 AA AB AC AD AE AF, B0 B1 B2 B3 B4 B5 B6 B7 B8 B9 BA BB BC BD BE BF C0 -
C1 C2 C3 C4 C5 C6 C7 C8 C9 CA CB CC CD CE CF, D0 D1 D2 D3 D4 D5 D6 D7 D8 D9 DA DB DC DD DE DF E0 -
E1 E2 E3 E4 E5 E6 E7 E8 E9 EA EB EC ED EE EF ,F0 F1 F2 F3 F4 F5 F6 F7 F8 F9 FA FB FC FD FE FF FF
2.nread=256
00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F ,10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F 20 -
21 22 23 24 25 26 27 28 29 2A 2B 2C 2D 2E 2F 30 , 31 32 33 34 35 36 37 38 39 3A 3B 3C 3D 3E 3F 40 -
41 42 43 44 45 46 47 48 49 4A 4B 4C 4D 4E 4F 50 , 51 52 53 54 55 56 57 58 59 5A 5B 5C 5D 5E 5F 60 -
61 62 63 64 65 66 67 68 69 6A 6B 6C 6D 6E 6F 70 , 71 72 73 74 75 76 77 78 79 7A 7B 7C 7D 7E 7F 80 -
81 82 83 84 85 86 87 88 89 8A 8B 8C 8D 8E 8F 90 , 91 92 93 94 95 96 97 98 99 9A 9B 9C 9D 9E 9F A0 -
A1 A2 A3 A4 A5 A6 A7 A8 A9 AA AB AC AD AE AF B0 , B1 B2 B3 B4 B5 B6 B7 B8 B9 BA BB BC BD BE BF C0 -
C1 C2 C3 C4 C5 C6 C7 C8 C9 CA CB CC CD CE CF D0 , D1 D2 D3 D4 D5 D6 D7 D8 D9 DA DB DC DD DE DF E0 -
E1 E2 E3 E4 E5 E6 E7 E8 E9 EA EB EC ED EE EF F0 , F1 F2 F3 F4 F5 F6 F7 F8 F9 FA FB FC FD FE FF FF |
|