Good evening,
I have problems reading analog values from an arduin one connected in modbus tcp.
Virtually I can control the status of inputs and outputs but no value comes from analog pins.
I attach the arduino code and the codesys screen.
Where am I wrong?
Thank you, Alberto
\#include<SPI.h>\#include<Ethernet.h>\#include"MgsModbus.h"MgsModbusMb;intinByte=0;//incomingserialbyte//Ethernetsettings(dependingonMACandLocalnetwork)bytemac[]={0x90,0xA2,0xDA,0x0E,0x94,0xB6};IPAddressip(10,0,0,46);//Unwrapper192,168,0,5IPAddressgateway(10,0,0,1);IPAddresssubnet(255,255,255,0);//ConfigurationDigitalINintIN1=2;intIN2=3;intIN3=5;intIN4=6;//ConfigurationDigitalOUTintOU1=7;intOU2=8;intOU3=9;//ConfigurationAnalogINintAN0=A0;intAN1=A1;intAN2=A2;intAN3=A3;intAN4=A4;intAN5=A5;voidsetup(){io_setup();//I/Osettings//serialsetupSerial.begin(9600);Serial.println("Serial interface started");//initializetheethernetdeviceEthernet.begin(mac,ip,gateway,subnet);//startetehrnetinterfaceSerial.println("Ethernet interface started");//printyourlocalIPaddress:Serial.print("My IP address: ");for(bytethisByte=0;thisByte<4;thisByte++){//printthevalueofeachbyteoftheIPaddress:Serial.print(Ethernet.localIP()[thisByte],DEC);Serial.print(".");}Serial.println();}voidloop(){io_poll();//I/Osettingsunsignedintj;//Inmonitorserialeinserire10pervederelostatodeibitif(Serial.available()>0){//getincomingbyte:inByte=Serial.read();if(inByte=='0'){//printMbDatainti=0;intj=0;Serial.println();for(i=0;i<5;i++){//NumberofchannelSerial.print("Mb.MbData[");Serial.print(i);Serial.print("]=");Serial.print(Mb.MbData[i]);Serial.print(" BIN: ");for(j=0;j<16;j++){//NumberofbitSerial.print(bitRead(Mb.MbData[i],j));}Serial.println();}Serial.println();}}Mb.MbsRun();}voidio_poll(){//INPUTDIGITALbitWrite(Mb.MbData[0],0,digitalRead(IN1));bitWrite(Mb.MbData[0],1,digitalRead(IN2));bitWrite(Mb.MbData[0],2,digitalRead(IN3));bitWrite(Mb.MbData[0],3,digitalRead(IN4));//OUTPUTDIGITALdigitalWrite(OU1,bitRead(Mb.MbData[1],0));digitalWrite(OU2,bitRead(Mb.MbData[1],1));digitalWrite(OU3,bitRead(Mb.MbData[1],2));//INPUTANALOGSerial.println(Mb.MbData[2]);bitWrite(Mb.MbData[2],0,analogRead(AN0*0.48875));bitWrite(Mb.MbData[2],1,analogRead(AN1));bitWrite(Mb.MbData[2],2,analogRead(AN2));bitWrite(Mb.MbData[2],3,analogRead(AN3));bitWrite(Mb.MbData[2],4,analogRead(AN4));bitWrite(Mb.MbData[2],5,analogRead(AN5));}voidio_setup(){//ConfigurationDigitalOUTpinMode(OU1,OUTPUT);pinMode(OU2,OUTPUT);pinMode(OU3,OUTPUT);//ConfigurationDigitalINpinMode(IN1,INPUT);pinMode(IN2,INPUT);pinMode(IN3,INPUT);pinMode(IN4,INPUT);//ConfigurationAnalogINpinMode(AN0,INPUT);pinMode(AN1,INPUT);pinMode(AN2,INPUT);pinMode(AN3,INPUT);pinMode(AN4,INPUT);pinMode(AN5,INPUT);}
Good morning,
I did a simple program to figure out how to read an analog value from an arduino one connected to modbus tcp.
Using a test program (jbus) to see the values that pass on the bus I can see the value sent but in codesys i do not see other than 0.
Can you help me understand what's wrong?
Thank you, Alberto
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
From Alberto's screenshot, it looks like he had "always update variables" set to the default of disabled, and didn't use the values anywhere in the code. The hint is that the values appear grey in the Io mapping tab.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Good evening,
I have problems reading analog values from an arduin one connected in modbus tcp.
Virtually I can control the status of inputs and outputs but no value comes from analog pins.
I attach the arduino code and the codesys screen.
Where am I wrong?
Thank you, Alberto
Related
Talk.ru: 1
Talk.ru: 2
Nothing help ?
Good morning,
I did a simple program to figure out how to read an analog value from an arduino one connected to modbus tcp.
Using a test program (jbus) to see the values that pass on the bus I can see the value sent but in codesys i do not see other than 0.
Can you help me understand what's wrong?
Thank you, Alberto
Solved
What was the problem? I am having similar issues...
From Alberto's screenshot, it looks like he had "always update variables" set to the default of disabled, and didn't use the values anywhere in the code. The hint is that the values appear grey in the Io mapping tab.
This is such a common issue that it rivals IT's "Have you tried turning it off and on again?"
Ahhh.. I feel like a fool! Yes that solved it! Thank you for your help!