This is my first posting on this site so please forgive me as I am new to this support process.
I have used teensy for years (I used to coach a high school robotics team). Currently I am an electronics technician attempting to utilize a teensy 4.1 (I bought several of them, along with the magjack ethernet kits) for calibration of a quad encoder with a valve positioner. I need the teensy to feed encoder count values over modbus (ideally through tcp/ip ethernet) to an HMI with an embedded version of windows 7. Currently I pieced together the following code from the examples. It does work to feed encoder values to an internet explorer webpage on the HMI. I am not finding a good example for modbus over tcp/ip for the teensy4.1 that works to get the data I need into the right location.
Here is the code that works as a webserver for the encoder values:
#include "QuadEncoder.h"
#include <SPI.h>
#include <NativeEthernet.h>
byte mac[] = {
0x04, 0xe9, 0xe5, 0x0f, 0xb9, 0xcd
};
IPAddress ip(192, 168, 111, 44);
EthernetServer server(80);
uint32_t mCurPosValue;
uint32_t old_position = 0;
uint32_t mCurPosValue1;
uint32_t old_position1 = 0;
QuadEncoder myEnc1(1, 0, 1, 0);
QuadEncoder myEnc2(2, 2, 3, 0);
void setup()
{
while(!Serial && millis() < 4000);
/* Initialize the ENC module. */
myEnc1.setInitConfig(); //
myEnc1.EncConfig.revolutionCountCondition = ENABLE;
myEnc1.EncConfig.enableModuloCountMode = ENABLE;
myEnc1.EncConfig.positionModulusValue = 20;
// with above settings count rev every 20 ticks
// if myEnc1.EncConfig.revolutionCountCondition = ENABLE;
// is not defined or set to DISABLE, the position is zeroed every
// 20 counts, if enabled revolution counter is incremented when
// phaseA ahead of phaseB, and decrements from 65535 when reversed.
myEnc1.init();
myEnc2.setInitConfig(); //
myEnc2.EncConfig.positionInitialValue = 160;
myEnc2.EncConfig.positionMatchMode = ENABLE;
myEnc2.EncConfig.positionCompareValue = 200;
myEnc2.EncConfig.filterCount = 5;
myEnc2.EncConfig.filterSamplePeriod = 255;
myEnc2.init();
Serial.begin(9600);
Serial.println("Ethernet WebServer Example");
Ethernet.begin(mac, ip);
// Check for Ethernet hardware present
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. ");
while (true) {
delay(1); // do nothing, no point running without Ethernet hardware
}
}
if (Ethernet.linkStatus() == LinkOFF) {
Serial.println("Ethernet cable is not connected.");
}
// start the server
server.begin();
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
}
void loop(){
EthernetClient client = server.available();
if (client) {
Serial.println("new client");
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.write(c);
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
if (c == '\n' && currentLineIsBlank) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close"); // the connection will be closed after completion of the response
client.println("Refresh: 5"); // refresh the page automatically every 5 sec
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<html>");
// output the value of each analog input pin
client.print(" Encoder count is ");
client.print(mCurPosValue1);
client.println("<br />");
client.println("</html>");
break;
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
} else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
// give the web browser time to receive the data
delay(1);
// close the connection:
client.stop();
Serial.println("client disconnected");
}
/* This read operation would capture all the position counter to responding hold registers. */
mCurPosValue = myEnc1.read();
if(mCurPosValue != old_position){
/* Read the position values. */
Serial.printf("Current position value1: %ld\r\n", mCurPosValue);
Serial.printf("Position differential value1: %d\r\n", (int16_t)myEnc1.getHoldDifference());
Serial.printf("Position HOLD revolution value1: %d\r\n", myEnc1.getHoldRevolution());
Serial.println();
}
old_position = mCurPosValue;
mCurPosValue1 = myEnc2.read();
if(myEnc2.compareValueFlag == 1) {
//myEnc2.init();
//resets counter to positionInitialValue so compare
//will hit every 200
myEnc2.write(myEnc2.EncConfig.positionInitialValue);
Serial.print("Compare Value Hit for Encoder 2: ");
Serial.println(myEnc2.compareValueFlag);
Serial.println();
myEnc2.compareValueFlag = 0;
}
if(mCurPosValue1 != old_position1){
/* Read the position values. */
Serial.printf("Current position value2: %ld\r\n", mCurPosValue1);
Serial.printf("Position differential value2: %d\r\n", (int16_t)myEnc2.getHoldDifference());
Serial.printf("Position revolution value2: %d\r\n", myEnc2.getHoldRevolution());
Serial.println();
}
old_position1 = mCurPosValue1;
}
What I need is the Modbus portion instead of the webserver. I also need a way to divine the appropriate memory address for the data tag (encoder count integer value represented by "mCurPosValue1" in the above program) to interface with the HMI visualization program.
Any help would be greatly appreciated!
I have used teensy for years (I used to coach a high school robotics team). Currently I am an electronics technician attempting to utilize a teensy 4.1 (I bought several of them, along with the magjack ethernet kits) for calibration of a quad encoder with a valve positioner. I need the teensy to feed encoder count values over modbus (ideally through tcp/ip ethernet) to an HMI with an embedded version of windows 7. Currently I pieced together the following code from the examples. It does work to feed encoder values to an internet explorer webpage on the HMI. I am not finding a good example for modbus over tcp/ip for the teensy4.1 that works to get the data I need into the right location.
Here is the code that works as a webserver for the encoder values:
#include "QuadEncoder.h"
#include <SPI.h>
#include <NativeEthernet.h>
byte mac[] = {
0x04, 0xe9, 0xe5, 0x0f, 0xb9, 0xcd
};
IPAddress ip(192, 168, 111, 44);
EthernetServer server(80);
uint32_t mCurPosValue;
uint32_t old_position = 0;
uint32_t mCurPosValue1;
uint32_t old_position1 = 0;
QuadEncoder myEnc1(1, 0, 1, 0);
QuadEncoder myEnc2(2, 2, 3, 0);
void setup()
{
while(!Serial && millis() < 4000);
/* Initialize the ENC module. */
myEnc1.setInitConfig(); //
myEnc1.EncConfig.revolutionCountCondition = ENABLE;
myEnc1.EncConfig.enableModuloCountMode = ENABLE;
myEnc1.EncConfig.positionModulusValue = 20;
// with above settings count rev every 20 ticks
// if myEnc1.EncConfig.revolutionCountCondition = ENABLE;
// is not defined or set to DISABLE, the position is zeroed every
// 20 counts, if enabled revolution counter is incremented when
// phaseA ahead of phaseB, and decrements from 65535 when reversed.
myEnc1.init();
myEnc2.setInitConfig(); //
myEnc2.EncConfig.positionInitialValue = 160;
myEnc2.EncConfig.positionMatchMode = ENABLE;
myEnc2.EncConfig.positionCompareValue = 200;
myEnc2.EncConfig.filterCount = 5;
myEnc2.EncConfig.filterSamplePeriod = 255;
myEnc2.init();
Serial.begin(9600);
Serial.println("Ethernet WebServer Example");
Ethernet.begin(mac, ip);
// Check for Ethernet hardware present
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. ");
while (true) {
delay(1); // do nothing, no point running without Ethernet hardware
}
}
if (Ethernet.linkStatus() == LinkOFF) {
Serial.println("Ethernet cable is not connected.");
}
// start the server
server.begin();
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
}
void loop(){
EthernetClient client = server.available();
if (client) {
Serial.println("new client");
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.write(c);
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
if (c == '\n' && currentLineIsBlank) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close"); // the connection will be closed after completion of the response
client.println("Refresh: 5"); // refresh the page automatically every 5 sec
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<html>");
// output the value of each analog input pin
client.print(" Encoder count is ");
client.print(mCurPosValue1);
client.println("<br />");
client.println("</html>");
break;
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
} else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
// give the web browser time to receive the data
delay(1);
// close the connection:
client.stop();
Serial.println("client disconnected");
}
/* This read operation would capture all the position counter to responding hold registers. */
mCurPosValue = myEnc1.read();
if(mCurPosValue != old_position){
/* Read the position values. */
Serial.printf("Current position value1: %ld\r\n", mCurPosValue);
Serial.printf("Position differential value1: %d\r\n", (int16_t)myEnc1.getHoldDifference());
Serial.printf("Position HOLD revolution value1: %d\r\n", myEnc1.getHoldRevolution());
Serial.println();
}
old_position = mCurPosValue;
mCurPosValue1 = myEnc2.read();
if(myEnc2.compareValueFlag == 1) {
//myEnc2.init();
//resets counter to positionInitialValue so compare
//will hit every 200
myEnc2.write(myEnc2.EncConfig.positionInitialValue);
Serial.print("Compare Value Hit for Encoder 2: ");
Serial.println(myEnc2.compareValueFlag);
Serial.println();
myEnc2.compareValueFlag = 0;
}
if(mCurPosValue1 != old_position1){
/* Read the position values. */
Serial.printf("Current position value2: %ld\r\n", mCurPosValue1);
Serial.printf("Position differential value2: %d\r\n", (int16_t)myEnc2.getHoldDifference());
Serial.printf("Position revolution value2: %d\r\n", myEnc2.getHoldRevolution());
Serial.println();
}
old_position1 = mCurPosValue1;
}
What I need is the Modbus portion instead of the webserver. I also need a way to divine the appropriate memory address for the data tag (encoder count integer value represented by "mCurPosValue1" in the above program) to interface with the HMI visualization program.
Any help would be greatly appreciated!