Hello Guys,
I've managed to communicate over a MODBUS protocol (modbus.h library) and it works fine over the USB connection to COMport x on my PC.
However i want to communicate over the serial port pin 0 and 1 on TTL. (use TTL to 485 converter, bit of cable, then back from 485 to USB) on comport y.
When i use the USB port on the arduino and have comms, and put a scope meter on the serial communications i read all the information so i know comms via pin 0 and 1 works. also all my converters work fine when i write a test program to send a string to the PC over 485 (not in modbus mode).
My problem is that i can't establish communications without the USB. my goal is a stand alone arduino with power supply and comms over the TTL lines to 485. do i need to include specific code to get the channel open on the pins 0 and 1?
#include <modbus.h>
#include <modbusDevice.h>
#include <modbusRegbank.h>
#include <modbusSlave.h>
#include <SoftwareSerial.h>
/* PINS
Add more registers if needed
Digital input pins 2
Digital output pins 8
Analog output pins 9 (PWM)
Analog input pins 0
*/
modbusDevice SCADA;
modbusSlave PLC;
char string1[] = "4354";
//Defining PIN inputs and outputs
int DigitalInput_1 = 2,
DigitalOutput_1 = 8,
AnalogInput_1 = A0,
PWMOutput_1 = 9;
//Defining storing variable
int DO1, DI1;
word AI1;
byte AO1;
int value;
void setup()
{
SCADA.setId(10); ///Set PLC ID
//Add Digital Input registers
SCADA.add(10001);
// Add Digital Output registers
SCADA.add(00001);
//Analog input registers
SCADA.add(30001);
SCADA.add(30002);
//Analog Output registers
SCADA.add(40001);
PLC._device = &SCADA;
PLC.setBaud(19200);
pinMode(DigitalInput_1,INPUT);
pinMode(DigitalOutput_1,OUTPUT);
pinMode(AnalogInput_1, INPUT);
pinMode(PWMOutput_1, OUTPUT);
}
void loop(){
//Digital Input
delay(10);
DI1 = digitalRead(DigitalInput_1); // was een byte
if (DI1 >= 1)SCADA.set(10001,1);
if (DI1 <= 0)SCADA.set(10001,0);
delay(10);
//Digital output
DO1 = SCADA.get(00001);
if (DO1 <= 0 && digitalRead(DigitalOutput_1) == HIGH)digitalWrite(DigitalOutput_1,LOW);
if (DO1 >= 1 && digitalRead(DigitalOutput_1) == LOW)digitalWrite(DigitalOutput_1,HIGH);
//Analog input
AI1 = analogRead(AnalogInput_1);
AI1 = map (AI1, 0, 1024, 0, 100);
SCADA.set(30001, (word) AI1);
SCADA.set(30002, (int) value);
//Analog output
AO1 = SCADA.get(40001);
AO1 = map (AO1, 0, 20, 0, 255);
analogWrite(PWMOutput_1,AO1);
PLC.run();
}