#include <SoftwareSerial.h>
SoftwareSerial mySerial(,);
byte ZERO=0x00;
byte Addr=0x03;
byte Status=0x00;
int buffLen=;
char HexTable[] = "0123456789ABCDEF";
int pinTrigger=;
int pinDirection=;
void setup() {
// put your setup code here, to run once:
pinMode(pinDirection,OUTPUT);
digitalWrite(pinDirection,LOW);
pinMode(pinTrigger,OUTPUT);
Off(); mySerial.begin();
Serial.begin();
Serial.println("ok");
} void On(){
digitalWrite(pinTrigger,HIGH);
Status=0x01;
}
void Off(){
digitalWrite(pinTrigger,LOW);
Status=0x00;
} void loop() { byte crc[];
int count=;
byte rBytes[buffLen];
while(mySerial.available()>){
byte b=mySerial.read();
if(count<buffLen){
rBytes[count]=b;
}
count++;
delayMicroseconds();
}
if(count>){
String v= toHex(rBytes,count > buffLen? buffLen : count);
Serial.println("r:" + v);
} if(count<)return;
if(rBytes[] !=Addr)return;
if(count>buffLen)return;
//Send To Me //check crc
calculateCRC(rBytes,count-,crc);
if(crc[]!=rBytes[count-] || crc[] !=rBytes[count-]){
Serial.println("crc error!");
return;
} digitalWrite(pinDirection,HIGH);
byte resp[];
byte respLen=;
//Read status
if(rBytes[]==0x03){
resp[]=Addr;
resp[]=0x03;
resp[]=Status;
respLen=;
}else if(rBytes[]==0x06){ //Write Status
//power on : 03 06 01
//power off: 03 06 00
if(rBytes[]==0x01){
On();
}else{
Off();
}
resp[]=Addr;
resp[]=0x06;
resp[]=Status;
respLen=;
}else{
resp[]=Addr;
resp[]=0x83;
respLen=;
} sendData(resp,respLen);
digitalWrite(pinDirection,LOW); } //================CRC 16==========================
//CRC校验函数
//参数1:待校验数组的起始地址
//参数2:待校验数组的长度
//返回值CRC校验结果,16位,低字节在前
unsigned int calculateCRC(unsigned char* _regs,unsigned char arraySize,byte* crc)
{
unsigned int temp, temp2, flag;
temp = 0xFFFF;
for (unsigned char i = ; i < arraySize; i++)
{
temp = temp ^ *(_regs+i);
for (unsigned char j = ; j <= ; j++)
{
flag = temp & 0x0001;
temp >>= ;
if (flag)
temp ^= 0xA001;
}
}
temp2 = temp >> ; // Serial.print("b:");
// Serial.println(temp,HEX);
// Serial.println((byte)(temp & 0x00FF),HEX);
// Serial.println( (byte)(temp >> 8) ,HEX); temp = (temp << ) | temp2;
temp &= 0xFFFF; crc[]=(byte)(temp >> ) ;
crc[]=(byte)(temp & 0x00FF); return temp;
}
void sendData(byte data[],int count)
{
byte crc[];
calculateCRC(data,count,crc);
for(int i=;i<count;i++){
mySerial.write(data[i]);
}
mySerial.write(crc[]);
mySerial.write(crc[]);
}
//=================================================
//==============byte to hex string===============
String toHex(byte bs[],int count){
String bundle="";
for(int i=;i<count;i++){
bundle += (String)( HexTable[ bs[i] / ]) + (String)( HexTable[bs[i] % ]); }
return bundle;
}
//==============End byte to hex string=============

中间2脚短后连到单片机数字口上,低电位收,高电位发数据

注意左边4脚 是指MUC的,所以串口收发线不需要互换,直接按标识连好。

服务端代码参考:华为网盘中的ZNJM2-20150612.rar

ardunio 实现RS485通讯-下位机的更多相关文章

  1. C# WPF上位机实现和下位机TCP通讯

    下位机使用北京大华程控电源DH1766-1,上位机使用WPF.实现了电压电流实时采集,曲线显示.上午在公司调试成功,手头没有程控电源,使用TCP服务端模拟.昨天写的TCP服务端正好排上用场. 界面如下 ...

  2. STM32菜鸟成长记录---RS485通讯协议的应用

    写作原因:近来蛋闲?非也  !  昨天一同事合作的项目代码出现的bug-----他的上位机每200ms给我发送命令向我这边下位机读取一些数据,在此过程会按下按键做一些另外操作并给他返回数据:(通信是通 ...

  3. vb配置下位机CAN寄存器小结

    2011-12-14 18:44:32 效果图 1,完成设计(由于没有eeprom等存储设备,所以每次上电后需要通过串口配置某些寄存器).在设计中,列出技术评估难度,并进行尝试,参看<我的设计& ...

  4. "废物利用"也抄袭——“完全”DIY"绘图仪"<二、下位机程序设计>

    就不说怎么组装了吧,一把辛酸泪.说程序,因为这有两把辛酸泪……一把给下位机的C代码一把为了VB.NET的图像处理……不过就上上一篇说的,它们可以正确运行了,并且今天克服了Arduino上电过程中步进电 ...

  5. C#使用struct直接转换下位机数据

    编写上位机与下位机通信的时候,涉及到协议的转换,比较多会使用到二进制.传统的方法,是将数据整体获取到byte数组中,然后逐字节对数据进行解析.这样操作工作量比较大,对于较长数据段更容易计算位置出错. ...

  6. C#做上位机软件——绘图并传输给下位机

    拿到任务之后首先分成了几个部分: 1.绘图.学习了GDI+ 2.图片保存. 3.将图片转换成byte[].由于使用Socket通信,只能传输byte[]数据,所以这一步是向下位机传输的关键. 相应地, ...

  7. 下位机多个".c, .h"文件的相互包含及排版

    一.背景: 自从接触单片机编程以来,由于工作上的需要,不可避免的时常会接手别人的代码,但常常由于上一位同事的编码随意性有点大,导致可读性非常的差,有时候不得不完全舍弃原有代码,推倒重来,无形中增加了工 ...

  8. MSP430G2333下位机乘法运算需要注意的一个问题

    背景: 最近负责为主板管理电源的电源管理模块编写软体,使用的MCU为MSP430G2333.功能上很简单,即通过板子上的硬件拨码设定,或者通过IIC与主板通信,由主板的BIOS决定开机及关机的延时供电 ...

  9. ROS主题发布订阅控制真实的机器人下位机

    先模拟控制小乌龟 新建cmd_node.ccpp文件: #include"ros/ros.h" #include"geometry_msgs/Twist.h" ...

随机推荐

  1. 机器学习入门-随机森林温度预测-增加样本数据 1.sns.pairplot(画出两个关系的散点图) 2.MAE(平均绝对误差) 3.MAPE(准确率指标)

    在上一个博客中,我们构建了随机森林温度预测的基础模型,并且研究了特征重要性. 在这个博客中,我们将从两方面来研究数据对预测结果的影响 第一方面:特征不变,只增加样本的数据 第二方面:增加特征数,增加样 ...

  2. Oracle+Mybatis批量插入,更新和删除

    1.插入 (1)第一种方式:利用<foreach>标签,将入参的list集合通过UNION ALL生成虚拟数据,从而实现批量插入(验证过) <insert id="inse ...

  3. Java HashMap两种遍历方式

    第一种: Map map = new HashMap(); Iterator iter = map.entrySet().iterator(); while (iter.hasNext()) { Ma ...

  4. kafka相关资料

    先来说一下Kafka与RabbitMQ的对比: RabbitMQ,遵循AMQP协议,由内在高并发的erlanng语言开发,用在实时的对可靠性要求比较高的消息传递上. kafka是Linkedin于20 ...

  5. liblas 1.8.1编译安装

    liblas https://github.com/libLAS/libLAS/issues/102 https://liblas.org/start.html 源码 https://github.c ...

  6. Haskell语言学习笔记(63)Dicidable

    Dicidable class Divisible f => Decidable f where lose :: (a -> Void) -> f a choose :: (a -& ...

  7. LitJson JavaScriptSerializer

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...

  8. 如何打开Windows Server 2008 R2的域安全策略

    今天安装了Windows Server 2008 R2系统,并且建了域环境,在添加新用户的时候,发现用简单的密码时域安全策略提示密码复杂度不够,于是我就想在域安全策略里面把密码复杂度降低一点,但是很快 ...

  9. python中迭代器(转)

    一.迭代器与for语句 网上许多文章说Python的for语句中,in关键字后面的对象是一个集合.例如 for i in [1,2,3] print i 上面代码中in关键字后面的对象[1,2,3]是 ...

  10. too few PGs per OSD (20 < min 30)

    ceph osd pool set replicapool pg_num 150 ceph osd pool set replicapool pgp_num 150