CRC16 小查表法

#include
“stdafx.h”

 

static
unsigned
short crcTlb[] = { 0x0000, 0xCC01, 0xD801, 0x1400, 0xF001, 0x3C00, 0x2800, 0xE401,

0xA001, 0x6C00, 0x7800, 0xB401, 0x5000, 0x9C01, 0x8801, 0x4400 };

 

static
unsigned
short CalcCRC16(unsigned
short * pBuf, unsigned
int
length)

{

    unsigned
int i = 0, ch = 0;

    unsigned
int crc = 0xFFFF;

    for (i = 0; i < length; i++)

    {

        ch = pBuf[i];

        crc = crcTlb[(ch ^ crc) & 0x000F] ^ (crc >> 4);

        crc = crcTlb[((ch >> 4) ^ crc) & 0x000F] ^ (crc >> 4);

    }

    crc = (crc & 0x00FF) << 8 | (crc >> 8);

    return crc;

}

 

 

int main()

{

    unsigned
short ar[] = {0x01,0x03,0x00,0x56,0x00,0x09};

    unsigned
short crc16 = CalcCRC16(ar,6);

    printf(“%X\n”, crc16);


return 0;

}

 

运算符

意义

示例

对于每个位位置的结果(1=设定,0=清除)

    &    

位 AND

 x&y 

如果 x 和 y 都为 1,则得到 1;如果 x 或 y 任何一个为 0,或都为0,则得到 0

    |    

位 OR

 x|y 

如果 x 或 y 为 1,或都为 1,则得到 1;如果 x 和 y 都为 0,则得到 0

    ^    

位 XOR

 x^y 

如果 x 或 y 的值不同,则得到 1;如果两个值相同,则得到 0

    ~    

位 NOT(I的补码)

 ~x 

如果 x 为 0,则得到 1,如果 x 是 1,则得到 0

 

运算符

意义

示例

结果

<<

向左移位

x<<y

x 的每个位向左移动 y 个位

>>

向右移位

x>>y

x 的每个位向右移动 y 个位

Codesys或TwinCAT采用指针获取输入输出M存储区或其它连续存储区变量给数组赋值

VAR

a0 AT%M*:REAL:=0.0;

a1 AT%M*:REAL:=0.1;

a2 AT%M*:REAL:=0.2;

a3 AT%M*:REAL:=0.3;

a4 AT%M*:REAL:=0.4;

i:int:=0;

ar:array[0..4] of real;

b:bool;

pr:pointer to real;

r_trig1:R_TRIG;

END_VAR

r_trig1(clk:=b);

if r_trig1.q  then

b:=0;

for i:=0 to 4 do

pr:=adr(a0)+sizeof(a0)*i;

ar[i]:=pr^;

end_for

end_if;

 

Invoke实现跨线程访问控件

不带参数的跨线程


private
void button1_Click(object sender, EventArgs e)

{


Thread t = new
Thread(() =>

{


Random R = new
Random();


this.Invoke(new
Action(()=> { this.Text = R.NextDouble().ToString(); }));

});

t.Start();

}

注意:Invoke应用在其他线程内部;

带参数的跨线程


private
void button1_Click(object sender, EventArgs e)

{


Action<object> a = new
Action<object>(printf);


this.Invoke(a,“Caption”);

}


public
void printf(object s)

{


this.Text = s.ToString();

}

内置函数

数学函数

Abs() 求绝对值

Sgn() 返回符号

 

字符串函数

Len(string) 获取字符串长度

Right(string,length) 截取字符串

Mid(string,start,length)截取字符串

Ltrim(string)前导去空格

RTrim(string)去后缀空格

Trim(string)去前导尾随空格

 

类型转换函数

Asc

Chr

Val

Str

 

判断函数

IsNull

IsNumeric

IsArray

 

日期和时间函数

Date

Now

Time

 

随机函数

Randomize初始化随机数生成器

Rnd

 

格式化函数

Format

参考文献

《单片机编程魔法师之高级裸编程思想》 余灿基主编 张玮 张志柏 苏永刚编著 电子工业出版社。

《深入理解计算机网络》 王达著 机械工业出版社 2013年

《.NET 4.0面向对象编程漫谈 基础篇及应用篇》 金旭亮 电子工业出版社 2010年

C# 运行环境问题处理

同事的电脑运行程序,莫名其妙的闪退。

增加以下代码查找原因。

using System;

using System.Collections.Generic;

using System.Linq;

using System.Threading.Tasks;

using System.Windows.Forms;

 

namespace WindowsFormsApplication6

{


static
class
Program

{


///
<summary>


///
应用程序的主入口点。


///
</summary>

[STAThread]


static
void Main()

{


AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;


Application.EnableVisualStyles();


Application.SetCompatibleTextRenderingDefault(false);


Application.Run(new
Form1());

}

 


private
static
void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)

{


MessageBox.Show(e.ExceptionObject.ToString());

}

}

}

 

 

 

画面之间采用委托传递参数

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

 

namespace WindowsFormsApplication2

{


public
delegate
void
delegateShow(int counter);


public
partial
class
Form1 : Form

{


public Form1()

{

InitializeComponent();

 


Form2 f2 = new
Form2();

f2.ShowCounter = new
delegateShow(showcounter);

f2.Show();

}

 


public
void showcounter(int counter)

{


this.Text = counter.ToString();

}

}

}

 

 

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

 

namespace WindowsFormsApplication2

{


public
partial
class
Form2 : Form

{


public Form2()

{

InitializeComponent();

}

 


public
delegateShow ShowCounter;


int counter=0;

 


private
void button1_Click(object sender, EventArgs e)

{

counter++;

ShowCounter?.Invoke(counter);

}

}

}

 

 

二维图形绘制

绘制图形的基本函数Plot

绘制图形步骤

1、首先定义自变量X的取值向量(横坐标)。

2、再定义函数Y的取值向量(纵坐标)。

3、用plot(x,y)命令给出平面曲线图。

 

单条曲线绘制

x=-2:0.01:2;

y=x.^2;

plot(x,y);

单条曲线绘制

x=linspace(-2*pi,2*pi,30);

y=sin(x);

plot(x,y,“r*”);

多条曲线绘制

x1=linspace(-2*pi,2*pi,30);

y1=40*sin(x1);

plot(x1,y1)

hold on %不清除图形

x2=linspace(-2*pi,2*pi,30);

y2=x.^2;

plot(x2,y2);

 

多条曲线绘制

x=linspace(0,50,30);

y=x+20;

z=2*x.^2+3*x+1;

w=100*cos(x);

plot(x,y,x,z,x,w);

XML文件读写

xml文件内容

<?xml version=”1.0″ encoding=”utf-8″ standalone=”yes”?>

<library>

<book>

<name>笑傲江湖</name>

<name1>罪与罚</name1>

</book>

</library>

文件的读取


private
void button1_Click(object sender, EventArgs e)

{


OpenFileDialog op = new
OpenFileDialog();

op.Filter = “xml文件|*.xml”;

op.ShowDialog();

 


XDocument document = XDocument.Load( op.FileName );


XElement root = document.Root;


XElement book = root.Element(“book”);


XElement name1 = book.Element(“name”);


XElement name2 = book.Element(“name1”);


MessageBox.Show(name1.Value);


MessageBox.Show(name2.Value);

}

文件的写入


private
void button2_Click(object sender, EventArgs e)

{


OpenFileDialog op = new
OpenFileDialog();

op.Filter = “xml文件|*.xml”;

op.ShowDialog();

 


XDocument document = XDocument.Load(op.FileName);


XElement root = document.Root;


XElement book = root.Element(“book”);


XElement name1 = book.Element(“name”);


XElement name2 = book.Element(“name1”);

name1.Value = 笑傲江湖;

name2.Value = 罪与罚;

document.Save(op.FileName);

}

 

 

和泉PLC的子程序、宏、脚本的使用

子程序

这里的子程序和我们常规理解的子程序不一样,这里的子程序更像是一个大程序被分成几个小块而已。

使用方式如下:

首先编写子程序,注意子程序的编号

这里调用指令需要填写子程序的编号

 

这里的宏反而更像子程序,但也缺少了临时变量的概念。

调用宏

 

脚本

这里的脚本支持类C语言

编写脚本

调用脚本

注意:这是使用的第二天,不能保证我的练习是正确的。

网络

下载文件

TextWindow.WriteLine(“开始下载……”)

path=Network.DownloadFile(“https://smallbasic-publicwebsite.azurewebsites.net/assets/tutorial-downloads/CodingClub_Practice01.pdf”)

TextWindow.WriteLine(path)

TextWindow.WriteLine(“下载完成!”)

 

这里下载的是个PDF文件,找到下载的文件,修改文件名为pdf即可打开。

 

下载网页文件

TextWindow.WriteLine(“开始下载……”)

path=Network.GetWebPageContents(“https://www.baidu.com”)

TextWindow.WriteLine(path)

TextWindow.WriteLine(“下载完成!”)

 

 

海龟绘图

这是引进的Logo语言的模式。

 

显示乌龟

Turtle.Show()

乌龟移动100个像素

Turtle.Show()

Turtle.Move(100)

 

绘制正方形

Turtle.Show()

Turtle.Move(100)

Turtle.TurnLeft()

Turtle.Move(100)

Turtle.TurnLeft()

Turtle.Move(100)

Turtle.TurnLeft()

Turtle.Move(100)

我们把程序简化一下

Turtle.Show()

For i=1 To 4

Turtle.Move(100)

Turtle.TurnLeft()

EndFor

把直线随机修改一下颜色

Turtle.Show()

For i=1 To 4

GraphicsWindow.PenColor=GraphicsWindow.GetRandomColor()

Turtle.Move(100)

Turtle.TurnLeft()

EndFor

 

 

图形

图形界面

GraphicsWindow.Show()

 

 

设置一下窗体的外观

GraphicsWindow.BackgroundColor=”Pink”

GraphicsWindow.Title=”画板”

GraphicsWindow.Width=400

GraphicsWindow.Height=400

GraphicsWindow.Show()

 

 

画直线

GraphicsWindow.Title=”画板”

GraphicsWindow.BackgroundColor=”LightGray”

GraphicsWindow.Width=400

GraphicsWindow.Height=400

GraphicsWindow.Show()

GraphicsWindow.DrawLine(0,0,400,400)

GraphicsWindow.DrawLine(0,400,400,0)

 

 

 

修改笔的属性

GraphicsWindow.Title=”画板”

GraphicsWindow.BackgroundColor=”LightGray”

GraphicsWindow.Width=400

GraphicsWindow.Height=400

GraphicsWindow.Show()

GraphicsWindow.PenColor=”Red”

GraphicsWindow.PenWidth=5

GraphicsWindow.DrawLine(0,0,400,400)

GraphicsWindow.PenColor=”Blue”

GraphicsWindow.PenWidth=10

GraphicsWindow.DrawLine(0,400,400,0)

 

 

 

画图和填充图形

GraphicsWindow.Title=”画板”

GraphicsWindow.BackgroundColor=”LightGray”

GraphicsWindow.Width=600

GraphicsWindow.Height=600

GraphicsWindow.Show()

GraphicsWindow.PenColor=”Red”

GraphicsWindow.DrawRectangle(20,20,200,100)

GraphicsWindow.BrushColor=”Blue”

GraphicsWindow.FillRectangle(20,300,200,100)

 

 

循环

循环有两种语句,For和While

For实现的循环输出1到10

For i=1 To 10

TextWindow.WriteLine(i)

EndFor

 

 

 

While实现的循环输出1到10

i=1

While i<11

TextWindow.WriteLine(i)

i=i+1

EndWhile