Programming 版 (精华区)

发信人: Mice (鼠儿), 信区: Programming
标  题: DOS下的串口通信编程----(5)
发信站: 紫丁香 (Mon Sep 15 16:04:28 1997)


//plc.cpp
#define _PLC_CPP
#include "plc.h"
#include "serial.h"
#include <stdlib.h>
#include <process.h>
#include <stdio.h>
#include <string.h>

void PLC::NumHex(unsigned char *buf,unsigned char num)
{
        unsigned char temp;
        temp=(num&0xf0)>>4;
        if(temp<10) temp+='0';
        else temp+='A'-10;
        buf[0]=temp;
        temp=num&0x0f;
       if(temp<10) temp+='0';
        else temp+='A'-10;
        buf[1]=temp;
}

unsigned char* PLC::read(char *str)
{
        static unsigned char buf[MAXBUF];   /*max buffer*/
        unsigned char *p;
        unsigned char ack,rcv;
        unsigned char BccCount=0,BCC1[2];
        int wait1,wait2,wcount=100;
        if(!str||strlen(str)!=7||(*str!='g'&&*str!='G'&&*str!='h'&&*str!='H')) r
turn NULL;
        p=str;
        if(*str=='g'||*str=='h') {
                while(*p) BccCount+=*p++;
                BccCount+=EOT;
                NumHex(BCC1,BccCount);
        }
        for(wait1=0;wait1<wcount;wait1++) {
                for(wait2=0;wait2<wcount;wait2++) {
                        ComPort::write(ENQ);
                        if(errcode==TIMEOUT) continue;
                        ack=ComPort::read();
                        if(ack==ACK) break;
                }
                if(wait2==wcount) return NULL;
                ComPort::write(STX);
                if(errcode==TIMEOUT) continue;
                ComPort::write(str);
                if(errcode==TIMEOUT) continue;
                ComPort::write(EOT);
                if(errcode==TIMEOUT) continue;
                rcv=ComPort::read();
                if(*str=='g'||*str=='h') {
                        ComPort::write(BCC1[0]);
                        if(errcode==TIMEOUT) continue;
                        ComPort::write(BCC1[1]);
                        if(errcode==TIMEOUT) continue;
                }
                ack=ComPort::read();
                if(ack==ACK) break;
       }
        if(wait1==wcount) return NULL;
        p=buf;
        while((rcv=ComPort::read())!=STX){
                if(rcv==NAK||rcv==0xff) return NULL;
        };
        do {
                *p++=rcv;
        }while((rcv=ComPort::read())!=EOT&&rcv!=0xff&&p<buf+MAXBUF-3);
        if(rcv==0xff||rcv!=EOT) return NULL;
        *p++=rcv;                       /*EOT*/
        if(*str=='h'||*str=='g') {
                *p++=ComPort::read();      /*BCC2 first byte*/
                if(errcode==TIMEOUT) return NULL;
                *p++=ComPort::read();   /*BCC2 second byte*/
                if(errcode==TIMEOUT) return NULL;
                p=buf+1;
                BccCount=0;
                while(*p!=EOT) BccCount+=*p++;
                BccCount+=*p++;
                NumHex(BCC1,BccCount);
                if(p[0]!=BCC1[0]||p[1]!=BCC1[1]) return NULL;
        }
       return buf;
}

void PLC::write(char *format,char *data)
{
        unsigned char msg2[5];
        unsigned char *mes;
        unsigned char BccCount=0,BCC3[2];
        int i;
        if(*format=='h'){
                mes=data;
                while(*mes) BccCount+=*mes++;
                BccCount+=EOT;
                NumHex(BCC3,BccCount);
        }
        mes=read(format);
        if(mes==NULL||mes[1]!=ACK) {
                errcode=TIMEOUT;
                return;
        }
        ComPort::write(ENQ);
        if(errcode==TIMEOUT) return;
       if(ComPort::read()!=ACK) return;
        ComPort::write(STX);
        if(errcode==TIMEOUT) return;
        ComPort::write(data);
        if(errcode==TIMEOUT) return;
        ComPort::write(EOT);
        if(errcode==TIMEOUT) return;
        if(*format=='h') {
                ComPort::write(BCC3[0]);
                if(errcode==TIMEOUT) return;
                ComPort::write(BCC3[1]);
                if(errcode==TIMEOUT) return;
        }
        if(ComPort::read()==ACK) {
                for(i=0;i<5;i++) {
                        msg2[i]=ComPort::read();
                }
                if(msg2[0]==STX&&msg2[1]==ACK) errcode=0;
                else errcode=TIMEOUT;
        } else {
                errcode=TIMEOUT;
        }
 
}




       我是一只好老鼠

※ 来源:.紫丁香 pclinux.hit.edu.cn.[FROM: 202.118.226.74]
[百宝箱] [返回首页] [上级目录] [根目录] [返回顶部] [刷新] [返回]
Powered by KBS BBS 2.0 (http://dev.kcn.cn)
页面执行时间:2.500毫秒