no message

master
zcy 2023-10-06 00:38:32 +08:00
parent b0a2fcc7ce
commit 3edb55948a
3 changed files with 91 additions and 57 deletions

36
arduino/arduino.ino Normal file
View File

@ -0,0 +1,36 @@
int incomedate = 0;
void setup() {
// put your setup code here, to run once:
pinMode(13, OUTPUT);
Serial.begin(9600); //设置串口波特率9600
Serial.println(78, BIN);// "1001110"
Serial.println(78, OCT);// "116"
Serial.println(78, DEC);// "78"
Serial.println(78, HEX);// "4E"
Serial.println(1.23456, 0);// "1"
Serial.println(1.23456, 2);// "1.23"
Serial.println(1.23456, 4);// "1.2346"
Serial.println('N');// "N"
Serial.println("Hello world.");// "Hello world."
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(13, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
if (Serial.available() > 0)//串口接收到数据
{
incomedate = Serial.read();//获取串口接收到的数据
if (incomedate == 'H')
{
Serial.println("Good Job!");
}
}
}

View File

@ -1,57 +0,0 @@
/*
Stepper Motor Control - one revolution
This program drives a unipolar or bipolar stepper motor.
The motor is attached to digital pins 8 - 11 of the Arduino.
The motor should revolve one revolution in one direction, then
one revolution in the other direction.
Created 11 Mar. 2007
Modified 30 Nov. 2009
by Tom Igoe
*/
#include <Stepper.h>
#include <Servo.h>
const int stepsPerRevolution = 20; // change this to fit the number of steps per revolution
// for your motor
Servo myservo; // create servo object to control a servo
// initialize the stepper library on pins 8 through 11:
#define CLK 10
#define CW 9
#define EN 8
void setup() {
// set the speed at 60 rpm:
pinMode(CLK,OUTPUT);
pinMode(CW,OUTPUT);
pinMode(EN,OUTPUT);
// initialize the serial port:
Serial.begin(115200);
digitalWrite(EN,LOW);
myservo.attach(12); // attaches the servo on pin 9 to the servo object
}
void loop() {
char data[20] = {0};
digitalWrite(EN,LOW);
// step one revolution in one direction:
if(Serial.available()){ //if number of bytes (characters) available for reading from {
Serial.readBytes(data,20);
Serial.print("I received:"); //print I received
Serial.write(data); //send what you read
String strInt = data; // string to hold input
int ints = strInt.toInt();
Serial.print(ints); //print I received
myservo.write(ints); // sets the servo position according to the scaled value
for(int cnt = 0;cnt < ints;cnt++){
delay(100);
digitalWrite(EN,LOW);
delay(100);
digitalWrite(EN,HIGH);
}
ints = 0;
}
}

55
arduino/modbus_slave.ino Normal file
View File

@ -0,0 +1,55 @@
#include "ModBusSlave0.h"
#define TRANSMIT_PIN 6
#define MODBUS_ADDRESS 100
ModBusSlave0 mod(false);
ISR(USART0_RX_vect)
{
mod.receiveHandler();
}
ISR(USART0_TX_vect)
{
mod.transmitHandler();
}
uint16_t get_addr(uint16_t addr){
switch(addr){
case 1:
return 1;
default:
return addr;
}
}
uint8_t prepareResponse03(uint8_t *frame, uint8_t* data) {
if(frame[1] == 0x03){
uint16_t addr = uint16_t(frame[2])*256 + uint16_t(frame[3]);
uint16_t len = uint16_t(frame[4])*256 + uint16_t(frame[5]);
int i = 0;
while(len --){
data[i] = (get_addr(addr + i/2)&0xff00)>>8;
data[i + 1] = get_addr(addr + i/2)&0x00ff;
i +=2 ;
}
return i;
}
if(frame[1] == 0x06){
}
}
void setup() {
mod.begin(57600, TRANSMIT_PIN, MODBUS_ADDRESS);
mod.prepareResponse03 = *prepareResponse03;
}
void loop() {
mod.process();
}