\Arduino与VUP通讯代码

/*
 * @Author: lavida
 * @version: 2.0
 * @Date: 2020-05-20 10:05:53
 * @LastEditTime: 2020-05-20 11:31:45
 * @Description: 规范了代码分区
 */

#define NINPUT 64
#define NOUTPUT 64
#define NBIT 512
#define NWORD 400
#define NDWORD 100
#define NFLOAT 100
#define TIMOUT 100
#define MAXDATA 256

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */

/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

unsigned char In[NINPUT];
unsigned char Out[NOUTPUT];
unsigned char Bit[NBIT];
unsigned short Word[NWORD];
unsigned long Dword[NDWORD];
float Real[NFLOAT];

unsigned long lasttimetimout;
unsigned ltramein;
unsigned char tramein[1200];
unsigned lasttime;
unsigned cycleps;

unsigned curtime;
unsigned etime;
unsigned atime;
unsigned long dsltime;
unsigned dstime;
unsigned rtime;

int run = 1;
int Step;

void inittimout(void);
int istimout(void);
void comintputcom(unsigned char c);
int comintischar(void);
int comintgetcom(void);
void rdebugsendtrame(unsigned char *data, unsigned len);
void rdebugprocesstrame(void);
void rdebuginit(void);
void rdebugprocess(void);
void prog(void);

/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
double add1, add2, result0;
/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

/**
 * @name: getinputs
 * @func: 获取 VUP 传递过来的数据
 */
void getinputs(void)
{
    /* USER CODE BEGIN 1 */
    add1 = Real[1];
    add2 = Real[2];
    /* USER CODE END 1 */
}

/**
 * @name: myFunctions
 * @func: 在此函数中编写主要业务功能
 */
void myFunctions(void)
{
    /* USER CODE BEGIN 2 */
    result0 = add1 + add2;
    /* USER CODE END 2 */
}

/**
 * @name: setoutputs
 * @func: 将开发板中的数据传递给 VUP
 */
void setoutputs(void)
{
    /* USER CODE BEGIN 3 */
    Real[3] = result0;
    /* USER CODE END 3 */
}

/**
 * @name: setup
 * @func: Arduino 初始化函数
 */
void setup()
{
    /* USER CODE BEGIN 4 */

    /* USER CODE END 4 */
    curtime = millis();
    rdebuginit();
}

/**
 * @name: loop
 * @func: Arduino 主循环函数
 */
void loop(void)
{
    /* USER CODE BEGIN 5 */

    /* USER CODE END 5 */
    rdebugprocess();
    getinputs();
    prog();
    setoutputs();
}

/* Connect Functions ---------------------------------------------------------*/

/*
 * @Tips: 下面是与 Arduino 通讯有关的函数实现,请勿修改!
 */

void prog(void)
{
    myFunctions();
    etime = millis();
    atime = (etime - curtime) + rtime;
    curtime = etime;
    dsltime = atime;
    dstime = dsltime;
    atime -= (unsigned)dsltime;
    rtime = atime;
}

void inittimout(void)
{
    lasttimetimout = millis();
}

int istimout(void)
{
    return (millis() - lasttimetimout >= TIMOUT) ? 1 : 0;
}

void comintputcom(unsigned char c)
{
    Serial.write(c);
}

int comintischar(void)
{
//Serial.available() 的意思是:返回串口缓冲区中当前剩余的字符个数。
//一般用这个函数来判断zd串口的缓冲区有无数据,当Serial.available()>0时,
//说明串口接收到了数据,可以读取;Serial.read()指从串口的缓冲区取出并读取一个Byte的数据,
//比如有设备通过串口向Arduino发送数据了,我们就可以用Serial.read()来读取发送的数据。
    if (Serial.available() > 0)
    {
        return 1;
    }
    return 0;
}

int comintgetcom(void)
{
    unsigned char c;
    c = Serial.read();
    return (int)c;
}

void rdebugsendtrame(unsigned char *data, unsigned len)
{
    unsigned char crc;
    unsigned count;
    crc = 'Z' + (unsigned char)(len & 0xff) + (unsigned char)(len >> 8);
    comintputcom('Z');
    comintputcom((unsigned char)(len & 0xff));
    comintputcom((unsigned char)(len >> 8));
    for (count = 0; count < len; count++)
    {
        comintputcom(data[count]);
        crc += data[count];
    }
    comintputcom((unsigned char)(~crc));
}

void rdebugprocesstrame(void)
{
    unsigned char data[MAXDATA + 1];
    if (ltramein > 0)
    {
        switch (tramein[0])
        {
        case 0:
        {
            if (ltramein == 1)
            {
                rdebugsendtrame((unsigned char *)"\x00", 1);
            }
            else
            {
                rdebugsendtrame((unsigned char *)"\xFF", 1);
            }
        }
        break;
        case 1:
        {
            if (ltramein != 2)
            {
                rdebugsendtrame((unsigned char *)"\xFF", 1);
            }
            else
            {
                switch (tramein[1])
                {
                case 0:
                {
                    if (run)
                    {
                        run = 0;
                        rdebugsendtrame((unsigned char *)"\x00", 1);
                    }
                    else
                    {
                        rdebugsendtrame((unsigned char *)"\xFE", 1);
                    }
                }
                break;
                case 1:
                {
                    if (!run)
                    {
                        run = 1;
                        rdebugsendtrame((unsigned char *)"\x00", 1);
                    }
                    else
                    {
                        rdebugsendtrame((unsigned char *)"\xFE", 1);
                    }
                }
                break;
                case 2:
                {
                    if (!run)
                    {
                        Step = 1;
                        rdebugsendtrame((unsigned char *)"\x00", 1);
                    }
                    else
                    {
                        rdebugsendtrame((unsigned char *)"\xFE", 1);
                    }
                }
                break;
                case 3:
                {
                    run = 0;
                    rdebugsendtrame((unsigned char *)"\x00", 1);
                }
                break;
                case 0x80:
                {
                    unsigned char srun[2];
                    srun[0] = 0;
                    if (run == 1)
                        srun[1] = 1;
                    else
                        srun[1] = 0;
                    rdebugsendtrame(srun, 2);
                }
                break;
                default:
                {
                    rdebugsendtrame((unsigned char *)"\xFF", 1);
                }
                }
            }
        }
        break;
        case 2:
        {
            unsigned first, len;
            unsigned count;
            if (ltramein != 6)
            {
                rdebugsendtrame((unsigned char *)"\xFF", 1);
                break;
            }
            for (count = 0; count < MAXDATA; count++)
            {
                data[count] = 0;
            }
            first = tramein[2] + (tramein[3] << 8);
            len = tramein[4] + (tramein[5] << 8);
            switch (tramein[1])
            {
            case 0:
            {
                if (len + first > NINPUT || (len + 7) / 8 > MAXDATA)
                {
                    rdebugsendtrame((unsigned char *)"\xFF", 1);
                    break;
                }
                for (count = 0; count < len; count++)
                {
                    if (In[count + first])
                        data[1 + count / 8] |= 1 << (count & 7);
                }
                rdebugsendtrame(data, 1 + (len + 7) / 8);
            }
            break;
            case 1:
            {
                if (len + first > NOUTPUT || (len + 7) / 8 > MAXDATA)
                {
                    rdebugsendtrame((unsigned char *)"\xFF", 1);
                    break;
                }
                for (count = 0; count < len; count++)
                {
                    if (Out[count + first])
                        data[1 + count / 8] |= 1 << (count & 7);
                }
                rdebugsendtrame(data, 1 + (len + 7) / 8);
            }
            break;
            case 2:
            {
                if (len + first > NBIT || (len + 7) / 8 > MAXDATA)
                {
                    rdebugsendtrame((unsigned char *)"\xFF", 1);
                    break;
                }
                for (count = 0; count < len; count++)
                {
                    if (Bit[count + first])
                        data[1 + count / 8] |= 1 << (count & 7);
                }
                rdebugsendtrame(data, 1 + (len + 7) / 8);
            }
            break;
            case 3:
            {
                if (len + first > NWORD || len * sizeof(unsigned long) > MAXDATA)
                {
                    rdebugsendtrame((unsigned char *)"\xFF", 1);
                    break;
                }
                data[0] = 0;
                memcpy(&data[1], &Word[first], len * sizeof(unsigned));
                rdebugsendtrame(data, 1 + len * sizeof(unsigned));
            }
            break;
            case 4:
            {
                if (len + first > NDWORD || len * sizeof(unsigned long) > MAXDATA)
                {
                    rdebugsendtrame((unsigned char *)"\xFF", 1);
                    break;
                }
                data[0] = 0;
                memcpy(&data[1], &Dword[first], len * sizeof(unsigned long));
                rdebugsendtrame(data, 1 + len * sizeof(unsigned long));
            }
            break;
            case 5:
            {
                if (len + first > NFLOAT || len * sizeof(float) > MAXDATA)
                {
                    rdebugsendtrame((unsigned char *)"\xFF", 1);
                    break;
                }
                data[0] = 0;
                memcpy(&data[1], &Real[first], len * sizeof(float));
                rdebugsendtrame(data, 1 + len * sizeof(float));
            }
            break;
            default:
            {
                rdebugsendtrame((unsigned char *)"\xFF", 1);
            }
            }
        }
        break;
        case 3:
        {
            unsigned num;
            num = tramein[2] + (tramein[3] << 8);
            switch (tramein[1])
            {
            case 0:
            {
                if (ltramein != 5 || num >= NINPUT)
                {
                    rdebugsendtrame((unsigned char *)"\xFF", 1);
                    break;
                }
                In[num] = tramein[4] ? 1 : 0;
                rdebugsendtrame((unsigned char *)"\x00", 1);
            }
            break;
            case 1:
            {
                if (ltramein != 5 || num >= NOUTPUT)
                {
                    rdebugsendtrame((unsigned char *)"\xFF", 1);
                    break;
                }
                Out[num] = tramein[4] ? 1 : 0;
                rdebugsendtrame((unsigned char *)"\x00", 1);
            }
            break;
            case 2:
            {
                if (ltramein != 5 || num >= NBIT)
                {
                    rdebugsendtrame((unsigned char *)"\xFF", 1);
                    break;
                }
                Bit[num] = tramein[4] ? 1 : 0;
                rdebugsendtrame((unsigned char *)"\x00", 1);
            }
            break;
            case 3:
            {
                if (ltramein != 6 || num >= NWORD)
                {
                    rdebugsendtrame((unsigned char *)"\xFF", 1);
                    break;
                }
                memcpy(&Word[num], &tramein[4], 2);
                rdebugsendtrame((unsigned char *)"\x00", 1);
            }
            break;
            case 4:
            {
                if (ltramein != 8 || num >= NDWORD)
                {
                    rdebugsendtrame((unsigned char *)"\xFF", 1);
                    break;
                }
                memcpy(&Dword[num], &tramein[4], 4);
                rdebugsendtrame((unsigned char *)"\x00", 1);
            }
            break;
            case 5:
            {
                if (ltramein != 8 || num >= NDWORD)
                {
                    rdebugsendtrame((unsigned char *)"\xFF", 1);
                    break;
                }
                memcpy(&Real[num], &tramein[4], 4);
                rdebugsendtrame((unsigned char *)"\x00", 1);
            }
            break;
            default:
            {
                rdebugsendtrame((unsigned char *)"\xFF", 1);
            }
            }
        }
        break;
        case 10:
        {
            if (ltramein != 1)
            {
                rdebugsendtrame((unsigned char *)"\xFF", 1);
            }
            else
            {
                tramein[0] = 0;
                memcpy(&tramein[1], &cycleps, 2);
                rdebugsendtrame(tramein, 3);
            }
        }
        break;
        default:
        {
            rdebugsendtrame((unsigned char *)"\xFF", 1);
        }
        }
    }
}

void rdebuginit(void)
{
    Serial.begin(38400);
    lasttime = millis();
    rdebugsendtrame((unsigned char *)"\x00", 1);
}

void rdebugprocess(void)  //rdebugprocess程序
{
    static unsigned phase;  //步骤的标签
    static unsigned len;  //长度
    static unsigned char crc;
    if (!run)//运行的标记
        cycleps = 0;
    else
    {
        extern unsigned word[];
        unsigned ctime;
        unsigned long calc;
        ctime = millis();//计算得到当前的时间
        calc = ctime - lasttime;//当前的时间减去上一次的时间,得到时间差
        if (calc == 0)//如果时间差=0,那么时间差设置10000
            calc = 10000lu;
        else
        {
            calc = 10000lu / calc;//如果时间差不等于0,那么将
        }
        cycleps = calc;
        lasttime = ctime;
    }
    do
    {
        switch (phase)
        {//第0步
        case 0:
        {
            if (comintischar())//判断是不是z,是z,进行后面的程序
            {
                if (comintgetcom() == 'Z')
                {
                    phase++;
                }
                else
                {
                    return;
                }
            }
            else
            {
                return;
            }
            crc = 'Z';
            inittimout();//得到当前的时间
        }
        case 1:
        {
            if (comintischar())//如果有串口数据
            {
                len = comintgetcom();
                crc += len;
                phase++;
            }
            else
            {
                if (istimout())//如果超出设定的等待时间
                {
                    phase = 0;
                }
                return;
            }
            inittimout();//计算得到当前的时间
        }
        case 2:
        {
            if (comintischar())//如果有串口数据
            {
                len |= (unsigned)comintgetcom() << 8;
                crc += (len >> 8);
                ltramein = 0;
                phase++;
            }
            else
            {
                if (istimout())
                {
                    phase = 0;
                }
                return;
            }
            inittimout();
        }
        case 3:
        {
            if (comintischar())
            {
                tramein[ltramein] = comintgetcom();
                crc += tramein[ltramein++];
            }
            else
            {
                if (istimout())
                {
                    phase = 0;
                }
                return;
            }
            inittimout();
            if (ltramein == len)
                phase++;
            else
                break;
        }
        case 4:
        {
            if (comintischar())
            {
                int charin;
                charin = comintgetcom();
                if ((unsigned char)~crc == (unsigned char)charin)
                {
                    rdebugprocesstrame();
                }
                else
                {
                }
                phase = 0;
            }
            else
            {
                if (istimout())
                {
                    phase = 0;
                }
                return;
            }
        }
        }
    } while (1);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值