极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 13525|回复: 0

arduino串口数据接收

[复制链接]
发表于 2016-2-17 17:31:38 | 显示全部楼层 |阅读模式
#include <SimpleModbusMaster.h>

/*
   The example will use packet1 to read a register from address 0 (the adc ch0 value)
   from the arduino slave (id=1). It will then use this value to adjust the brightness
   of an led on pin 9 using PWM.
   It will then use packet2 to write a register (its own adc ch0 value) to address 1
   on the arduino slave (id=1) adjusting the brightness of an led on pin 9 using PWM.
*/

//////////////////// Port information ///////////////////
#define baud 9600
#define timeout 1000
#define polling 200 // the scan rate
#define retry_count 10

// used to toggle the receive/transmit pin on the driver
#define TxEnablePin 2

#define LED 9

// The total amount of available memory on the master to store data
#define TOTAL_NO_OF_REGISTERS 2

// This is the easiest way to create new packets
// Add as many as you want. TOTAL_NO_OF_PACKETS
// is automatically updated.
enum
{
  PACKET1,
  PACKET2,
  TOTAL_NO_OF_PACKETS // leave this last entry
};

// Create an array of Packets to be configured
Packet packets[TOTAL_NO_OF_PACKETS];

// Masters register array
unsigned int regs[TOTAL_NO_OF_REGISTERS];

void setup()
{
  // Initialize each packet
  modbus_construct(&packets[PACKET1], 01, READ_INPUT_REGISTERS, 257, 03, 00);
  
  
  // Initialize the Modbus Finite State Machine
  modbus_configure(&Serial, baud, SERIAL_8N2, timeout, polling, retry_count, TxEnablePin, packets, TOTAL_NO_OF_PACKETS, regs);
  
  pinMode(LED, OUTPUT);
}

void loop()
{
  modbus_update();
  
   //regs[0] = analogRead(0);
   // update data to be written to arduino slave
Serial.println(regs[0]);


  //analogWrite(LED, regs[0]>>2);
  // constrain adc value from the arduino slave to 255
  Serial.println("-------");
  delay(2000);
}



求大神看一下 我这个程序 要实现的功能是用arduino采集rs485的电压值  载入程序之后 tx闪 ttl的tx也闪 rs485也闪 只有arduino的rx不闪  串口监视器显示的数据是300-400的数值  求指教 哪里 不对啊
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-15 02:27 , Processed in 0.035054 second(s), 17 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表