I have so far been using this https://github.com/kroimon/Arduino-SerialCommand for my comms with success.
Now I need to add ModBus to my project so that I can pass messages to Mach3.
I found the sketch (plain modbus)at the end of this post online. It reads the message from Mach and sends response
which is read by Mach giving expected results.
However I want to keep the ability to receive serial (ASCII) messages already implemented so I modified the
SerialCommand library to also respond to modbus message.
I changed line 118 to line 119 and added lines 131,132 and 133 as per snippet below.
I added the first 3 lines and the Reply_Inputs() function from plain modbus to my sketch
My Setup is win7 with Roger's core 1.6.9IDE and 2 bluepills connected through a USB hub.
The plain modbus sketch works on both pills.
Using this: https://www.hhdsoftware.com/device-monitoring-studio
I can see both the request "0x01 0x04 0x00 0x00 0x00 0x31 0xca" from Mach and the response
The response is the same also when I use realterm to send the request (in hex)
https://realterm.sourceforge.io/
Now my actual issue/problem:
Using realterm my skecth sends out the correct response verifiable by both realterm and devicemonitoringstudio
but using Mach my skecth does not respond anything??
There is still 2 differences that I can see:
1 buffer data type int vs char
2 if ( Serial.available() == 8 ) vs while (Serial.available() > 0)
but can they be the cause, as the request is Hex and triggers response depending on which application sends it?
I know that its sort of unfair to post above question but I'm all out of ideas at the moment.
Rgds
Tapio
Code: Select all
118 // else if (isprint(inChar)) { // Only printable characters into the buffer <- Changed this
119 else if (inChar) { // All characters into the buffer <- to this
120 if (bufPos < SERIALCOMMAND_BUFFER) {
121 buffer[bufPos++] = inChar; // Put character into buffer
122 buffer[bufPos] = '\0'; // Null terminate
123 } else {
124 #ifdef SERIALCOMMAND_DEBUG
125 Serial.println("Line buffer is full - increase SERIALCOMMAND_BUFFER");
126 #endif
127 }
128 }
129 }
130
131 if (buffer[0] == 0x01 & buffer[1] == 0x04) { // Added this if statement
132 (*commandList[0].function)();
133 clearBuffer();
134 }
Code: Select all
//*****start plain modbus*****
unsigned int CRC16, SEED, GP; //for CRC16
#define SEED 0xFFFF //initialization for CRC16
#define GP 0xA001 //generating polynomial
int in_buffer[8]; // receiving buffer
#define CycleStart 2; //MOD:0-D0
#define FeedHold 3; //MOD:0-D1
#define StopFile 4; //MOD:0-D2
#define SingleStep 5; //MOD:0-D3
#define ResumeFile 6; //MOD:0-D4
#define ZeroX 7; //MOD:0-D5
#define ZeroY 8; //MOD:0-D6
#define ZeroZ 9; //MOD:0-D7
void setup()
{
Serial.begin(115200); // Init serial communication
pinMode(PA0, INPUT_PULLUP); // Set pin to input
pinMode(PA1, INPUT_PULLUP); // Set pin to input
pinMode(PA2, INPUT_PULLUP); // Set pin to input
pinMode(PA3, INPUT_PULLUP); // Set pin to input
pinMode(PA4, INPUT_PULLUP); // Set pin to input
pinMode(PA5, INPUT_PULLUP); // Set pin to input
pinMode(PA6, INPUT_PULLUP); // Set pin to input
pinMode(PA7, INPUT_PULLUP); // Set pin to input
}
void loop()
{
if ( Serial.available() == 8 ) {
// read ModBus command from Mach3
for (int i=0; i < 8; i++) in_buffer[ i ] = Serial.read();
if ((in_buffer[0]== 0x01)&(in_buffer[1]== 0x04))
Reply_Inputs();
else
Reply_Error();
Serial.flush();
}
}
/// ----------------------- Functions -----------------------
void Reply_Inputs()
{
CRC16 = SEED;
int Inputs[] ={0x01, 0x04, 0x0A, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00};
Inputs[4] = (
((!digitalRead(PA7))<<7) |
((!digitalRead(PA6))<<6) |
((!digitalRead(PA5))<<5) |
((!digitalRead(PA4))<<4) |
((!digitalRead(PA3))<<3) |
((!digitalRead(PA2))<<2) |
((!digitalRead(PA1))<<1) |
((!digitalRead(PA0)) ) );
for (int i = 0; i < 13; i++)
{
Calc_CRC(Inputs[i], &CRC16);
}
Inputs[13]=CRC16;
CRC16=CRC16>>8;
Inputs[14]=CRC16;
for (int i=0; i < 15; i++) Serial.write(Inputs[i]);
}
/// ---------------------------------------------------------
void Reply_Error()
{
int Error[] ={0x01, 0x84, 0x00, 0x43, 0x00};
for (int i=0; i < 5; i++) Serial.write(Error[i]);
}
/// ---------------------------------------------------------
void Calc_CRC(unsigned char b, unsigned int* CRC)
{
int carry, i;
CRC[0] ^= b & 0xFF;
for (i=0; i<8; i++)
{
carry = CRC[0] & 0x0001;
CRC[0]>>=1;
if (carry) CRC[0] ^= GP;
}
}
/// ---------------------------------------------------------