我使用PC104总线的主板,挂一个数据采集卡,作为下位机采集模块,采集到结果通过RS485送给上位机,采用的是中断方式的串行通讯。一开始通讯正常,但是一端时间,大概发送1000次左右,下位机就会死机,反复调试都是这样,不是很明白。现在把代码贴出来,就高手指教!
void interrupt(*oldport1isr)();
void interrupt ComInt()/*中断服务程序*/
{
unsigned char ReceiveFlag;
unsigned int cnt;
unsigned char panelid = 0;
OverTimeFlag = 0;
CheckSumReceive = 0;
CountByte = 0;
awresult = 0;
tempresult = 0;
ppmresult = 0;
closeint();
for(cnt = 0; cnt < 8; cnt++)
{
CommandReceive[cnt] = 0;
}
overtimejudge();
panelid = (unsigned char)(para[0]);
if(OverTimeFlag == 0)
{
CommandReceive[0] = inport(PORT); /*First byte receive,if don't match panelid,return interrupt funtion*/
if(panelid == CommandReceive[0])
{
panelright();
}
else
{
;
}
}
delay(DELAYTIME);
outportb(PORT + 2, 0x07);
outport(0x20, 0x20);
outport(0x20, 0x20);
}
void readparameter()
{
FILE *fp;
int i;
fp = fopen("config.ini", "r");
if(fp == NULL)
{
printf("Can not open file\n");
}
for(i = 0; i < 3; i++)
{
fscanf(fp, "%f", para + i);
}
fclose(fp);
}
void senddata(char data)
{
unsigned int i;
for(i = 0; i < SendDelayTime; i++)
{
;
}
outportb(PORT + 1,0x00); /*Turn Off Interrupt */
outportb(PORT + 4, 0x0B); /*Turn on DTR,RTS,OUT2,send mode*/
outportb(PORT, data);
}
void initcom()/*串口初始化*/
{
outportb(PORT + 1, 0); /*Turn off interrupt*/
oldport1isr = getvect(INTVECT); /*Save old interrupt vector for recovery*/
setvect(INTVECT, ComInt);
outportb(PORT + 3, 0x80); /*Set DLAB on*/
outportb(PORT,0x0C); /*Set 9600 baud rate*/
outportb(PORT + 1, 0x00);
outportb(PORT + 3, 0x3B); /*8 bits,1 parity,1 stop bit */
outportb(PORT + 2, 0x07);
outportb(PORT + 4,0x0A); /*Turn off DTR,Turn on RTS,OUT2,receive mode*/
}
void openint()/*开中断*/
{
outportb(PORT + 4,0x0A); /*Turn off DTR,Turn on RTS,OUT2,receive mode*/
outportb(0x21, (inportb(0x21) & 0xF7)); /*Set programmable interrupt controller*/
outportb(PORT + 1, 0x01); /*Interrupt when data received*/
}
void closeint()/*关中断*/
{
oldport1isr = getvect(INTVECT);
outportb(PORT + 1,0); /*Turn off Com2 interrupt*/
outport(0x21, (inportb(0x21) | 0x08));
}
void panelright()/*下位机判断是否命令是自己的板号*/
{
unsigned char i;
for(i = 1; i< 7; i++)
{
receive6commandbyte(i);
if(OverTimeFlag != 0)
{
break;
}
}
if(CountByte == 6)
{
receive6bytecomplete();
}
}
void receive6commandbyte(unsigned char order)
{
overtimejudge();
if(OverTimeFlag == 0)
{
CommandReceive[order] = inport(PORT);
CheckSumReceive += CommandReceive[order];
CountByte++;
}
}
void receive6bytecomplete()
{
overtimejudge();
if(OverTimeFlag == 0)
{
CommandReceive[7] = inport(PORT);
if(CommandReceive[7] == CheckSumReceive)
{
if(BeginJudge == 0)
{
beginmeasure();
}
else
{
sendresult();
}
}
else
{
checksumwrong();
}
}
}
void sendresult()
{
/*发送测量结果*/
}
void beginmeasure()
{
/*开始测量*/
}
void beginsucess()
{
/*返回启动测试成功的信息*/
}
void checksumwrong()
{
/*上位机命令校验和出错后下位机返回出错信息*/
}
void overtimejudge()/*上位机命令是8个字节,判断有没有接收超时*/
{
unsigned char ReceiveFlag;
unsigned long cnt = 0;
do
{
ReceiveFlag = inport(PORT + 5);
cnt++;
if(cnt > TimeExceed) /*Overtime judge,wait for 300ms*/
{
OverTimeFlag = 1;
break;
}
}
while(!(ReceiveFlag&1));
}
unsigned int sample(int chx)
{
/*开始采样*/
}
void main()
{
clrscr();
arrayinit();
readparameter();
enable_gpio();
initcom();
openint();
while(1) /*一直等待上位机命令*/
{
initcom();
openint();
delay(DELAYTIME);
feedDog();
setvect(INTVECT, oldport1isr);
}
}
抱歉我不懂这些,但我是专业做PC104的,我们公司有精通的工程师哦,有兴趣的话,可以联系我:MSN:dongyan98@hotmail.com QQ:15144652