锐单电子商城 , 一站式电子元器件采购平台!
  • 电话:400-990-0325

耀华YHL-5屏幕开发教程

时间:2022-08-11 10:30:02 拉线传感器yhl

耀华YHL-5屏幕开发教程

        • 耀华LED通讯协议
        • 数据处理和函数包装
        • 发送重量数据函数封装

耀华LED通讯协议

波特率1200,数据位8位(只能显示6个数字或符号)

当发送端串口连续发送时,数据是ASCII码方式输出,每帧数据共有9组组成(包括小
数点)。数据传输先低后高。每帧数据之间有一组分隔符=,发送的数据是毛重,如
当前毛重70.15,连续发送51.07000=51.07000=……。
30 30 30 30 30 30 30 30 3D(16进制,HEX码)
00 0 0 0 0 0 0=
黄色字节为符号位,负数为2D(其他正数30)
如当前重量70.15,连续发送(ASCII码) 51.07000= <–> 35 31 2E 30 37 30 30 30 3D(hex码)
如当前重量-70.15,连续发送(ASCII码) 51.0700-= <–> 35 31 2E 30 37 30 30 2D 3D(hex码)
如当前重量123456,连续发送(ASCII码) 65432100= <–> 36 35 34 33 32 31 30 30 3D(hex码)
如当前重量-023456,连续发送(ASCII码) 6543200-= <–> 36 35 34 33 32 30 30 2D 3D(hex码)

数据处理和函数包装

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;  namespace avs { 
             public static class DataTransform     { 
                 ///          /// 字符串逆序         /// Jonsonzhu         ///          /// 字符串需要转换         /// 转换后的字符串         public static string reverseString(string text)         { 
                     return new string(text.ToCharArray().Reverse().ToArray());         }          ///          /// 字符串复制         ///          /// Jonsonzhu         /// 要复制的字符串         /// 复制的次数         ///          public static string copyString(string str, int times)         { 
        
            string res = "";
            for (int i = 0; i < times; i++)
            { 
        
                res += str + " ";
            }
            return res;
        }

        /// 
        /// 将字符串转换成字节码发送
        /// 
        /// 
        /// 
        public static byte[] getBytes(string hexcommand)
        { 
        
            //串口未打开,则不能执行发送功能
            if (string.IsNullOrEmpty(hexcommand))
                return null;
            string sendBuf = hexcommand;
            string sendnoNull = sendBuf.Trim();
            string sendNOComma = sendnoNull.Replace(',', ' ');    //去掉英文逗号
            string sendNOComma1 = sendNOComma.Replace(',', ' '); //去掉中文逗号
            string strSendNoComma2 = sendNOComma1.Replace("0x", "");   //去掉0x
            strSendNoComma2.Replace("0X", "");   //去掉0X
            string[] strArray = strSendNoComma2.Split(' ');

            int byteBufferLength = strArray.Length;
            for (int i = 0; i < strArray.Length; i++)
            { 
        
                if (strArray[i] == "")
                { 
        
                    byteBufferLength--;
                }
            }

            byte[] byteBuffer = new byte[byteBufferLength];
            int j = 0;
            for (int i = 0; i < strArray.Length; i++)        //对获取的字符做相加运算
            { 
        

                Byte[] bytesOfStr = Encoding.Default.GetBytes(strArray[i]);
                int decNum = 0;
                if (strArray[i] == "")
                { 
        
                    continue;
                }
                else
                { 
        
                    decNum = Convert.ToInt32(strArray[i], 16); //atrArray[i] == 12时,temp == 18 
                }
                try    //防止输错,使其只能输入一个字节的字符
                { 
        
                    byteBuffer[j] = Convert.ToByte(decNum);
                }
                catch (System.Exception ex)
                { 
        
                    Console.WriteLine("字节越界,请逐个字节输入!", "Error");
                    return null;
                }
                j++;
            }
            return byteBuffer;
        }

        /// 
        /// 将称重数据转换成指定格式的字符串
        /// 
        /// 重量数据
        /// 9字节的字符串
        public static string EncodingWeightStr(string weightstr)
        { 
        
            string tempValue = "";
            string res = "";
            string input = reverseString(weightstr);
            char[] values = input.ToCharArray();
            foreach (char letter in values)
            { 
        
                int value = Convert.ToInt32(letter);
                // 将十进制转化为16进制的字符形式
                string hexOutput = String.Format("{0:X}", value);
                tempValue += hexOutput + " ";
            }
            res = tempValue + copyString("30", 8 - input.Length) + "3D";

            return res.Trim();

        }

    }
}

发送重量数据函数封装

using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace avs
{ 
        
    public class avsFuc
    { 
        
        /// 
        /// 发送称重数据
        /// 
        /// 要发送的重量数据
        /// 实例化的串口
        public void sendText(string text, SerialPort serialPort)
        { 
        
            byte[] by = new byte[] { 
         };
            string res = DataTransform.EncodingWeightStr(text);
            by = DataTransform.getBytes(res);
            serialPort.Write(by, 0, by.Length);
        }
    }
}
锐单商城拥有海量元器件数据手册IC替代型号,打造电子元器件IC百科大全!

相关文章