#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. as3 优化

    1 代码写法 1 定义局部变量 定义局部变量的时候,一定要用关键字var来定义,因为在Flash播放器中,局部变量的运行速度更快,而且在他们的作用域外是不耗占系统资源的.当一个函数调用结束的时候,相应 ...

  2. css 学习网址

    http://www.divcss5.com/ http://www.divcss5.com/css3/  css3手册 http://www.divcss5.com/shouce/ css手册 ht ...

  3. python远程调试及celery调试

    部分来自 from: https://www.xncoding.com/2016/05/26/python/pycharm-remote.html 你是否经常要在Windows 7或MAC OS X上 ...

  4. Haskell语言学习笔记(61)Distributive

    Distributive class Functor g => Distributive g where distribute :: Functor f => f (g a) -> ...

  5. mongodb first

    use [database] 使用数据库,新增文档后,数据库被自动创建 show dbs 显示所有数据库 db.[document].insert() 插入数据库 例:db.persons.inser ...

  6. Hibernate 的update语句性能详解

    Hibernate 中如果直接使用 Session.update(Object o); 会把这个表中的所有字段更新一遍. 比如: view plaincopy to clipboardprint? p ...

  7. PR合并回写

    ) as LGORT ,'SAPRFC' as ERNAM,out_pr.due_datetime,out_pr.so_id,out_pr.so_lineid,out_pr.sobsl from V_ ...

  8. Python3 exec 函数

    Python3 exec 函数  Python3 内置函数 描述 exec 执行储存在字符串或文件中的 Python 语句,相比于 eval,exec可以执行更复杂的 Python 代码. 语法 以下 ...

  9. 38. Count and Say (String; DP)

    The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...

  10. Pie(浮点数二分)

    Pie http://poj.org/problem?id=3122 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2454 ...