-
-
Notifications
You must be signed in to change notification settings - Fork 75
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Mega2560 TX problem #54
Comments
Hi, thank you for the issue ... hmm, |
Yes, just like the example. With Arduino Uno it works as expected, but the same code on Mega2560 doesn't work. I tried with two different MEGA2560 but I have same problem. I asked a friend of mine and he tried with Arduino UNO and no problem and then he tried with Mega2560 but he has the same problem as me. It doesn't write to port. We have a RS485 board without the need of Control Pin. Here is the code (We just changed the value of analogread to 221 just for testing, and it works ok on UNO); #include <ModbusSlave.h>
// explicitly set stream to use the Serial serialport
Modbus slave(Serial2, 1); // stream = Serial, slave id = 1, rs485 control-pin = 8
void setup() {
// register handler functions
slave.cbVector[CB_READ_INPUT_REGISTERS] = ReadAnalogIn;
// slave.cbVector[CB_READ_HOLDING_REGISTERS] = readMemory;
// start slave at baud 9600 on Serial
Serial2.begin( 9600 ); // baud = 9600
slave.begin( 9600 );
Serial.begin( 9600 );
}
void loop() {
// listen for modbus commands con serial port
slave.poll();
}
// Handel Read Input Registers (FC=04)
uint8_t ReadAnalogIn(uint8_t fc, uint16_t address, uint16_t length) {
// write registers into the answer buffer
Serial.println(fc);
for (int i = 0; i < length; i++) {
//slave.writeRegisterToBuffer(i, analogRead(address + i));
slave.writeRegisterToBuffer(i,221);
}
return STATUS_OK;
} |
Nice, can you debug the |
Sure, can you tell me how to do debug it? Thank you |
👍 I think that the easiest way will be to listen on the serial bus while adding debug data. |
I am not sure if this helps. I used also a Debug library to print the value of slave.writeRegisterToBuffer Entering function ReadAnalogIn
Function : 4 / Address : 1 / Length : 1
For i value : 0
DEBUGVAL : slave.writeRegisterToBuffer(i,221) = 0
Line after writeRegisterToBuffer
Line before return STATUS_OK |
a - did you try to look what happen inside the |
Also, what version of the library are you using? |
Try printing out the content of the _responseBuffer to better analyse where the data entrance stops. If the |
I tried with every Serial but no success. if (_responseBufferWriteIndex == 0 && _responseBufferLength >= MODBUS_FRAME_SIZE)
{
// Start the writing.
_isResponseBufferWriting = true;
Serial.println("start writing"); **<-- This is printed on serial output**
}
if(_isResponseBufferWriting || !isBroadcast()){
Serial.println("isResponseBufferWritin || not Broadcast"); **<-- This is printed on serial output**
}
if (!_isResponseBufferWriting || isBroadcast())
; **<-- Can you explain me the purpose of this on line 836?**
{
_isResponseBufferWriting = false;
_responseBufferWriteIndex = 0;
_responseBufferLength = 0;
Serial.println("not writing?"); **<---- This one TOO??**
return 0;
} |
As @Proace commented, can you printout |
Ok. Can you please tell me under which function should I print the responseBuffer? |
line 836 looks like a bug ... what happen if you remove it ? https://github.com/yaacov/ArduinoModbusSlave/blame/master/src/ModbusSlave.cpp#L836 |
The weird thing being that it does work on the UNO. Following the logic of L836 the program should never send anything, which is indeed a bug. |
what happen if you remove it ? |
I commented it and nothing is shown anymore on the Serial output... weird huh? |
a - L836 is a bug ( typo ... ) |
https://github.com/ProAce/ArduinoModbusSlave/tree/asefd |
@Proace tried your code but didn't work. Nothing shows on serial. if (!_isResponseBufferWriting && isBroadcast());
{
//_isResponseBufferWriting = false;
//_responseBufferWriteIndex = 0;
//_responseBufferLength = 0;
Serial.println("not writing?");
//return 0;
} If I don't uncomment these I have to put the famous ";" on the line 836 to have some serial output. Narrowing more.... |
@asefd in the snipt above I see the extra if (!_isResponseBufferWriting && isBroadcast()) >> ; <<
{
//_isResponseBufferWriting = false; do you have it in the code you are using ? |
@yaacov no, right now I have this code that is working: if (!_isResponseBufferWriting)
{
_isResponseBufferWriting = false;
_responseBufferWriteIndex = 0;
_responseBufferLength = 0;
//Serial.println("not writing?");
return 0;
} |
Then the isBroadcast function returns true because it detects a broadcast message. The weird thing is that that should also happen on the UNO. It's also weird that you have no output at all with my code, it should at least print which function you entered. |
@Proace I will test later again and see what I can change to make it work. |
@Proace I used your forked library. For it to work on the function 255
1
Func writeRegisterToBuffer.
598
Func writeResponse
Start writing the response
1
Start writing
Message:
1 4 2 2 86 0 0 CRC: 28216
Length: 7
Func writeResponse
1
Start writing
Length: 0
Transmission ended The 255 and 1 at the beggining is the |
@asefd Thanks ! Can you check if this fix the problem ? bool Modbus::isBroadcast()
{
uint8_t unitAddress = Modbus::readUnitAddress();
return unitAddress == MODBUS_BROADCAST_ADDRESS;
} |
@yaacov I just tried but it doesn't |
sorry to nag ... can you try ? bool Modbus::isBroadcast()
{
return (
_requestBufferLength >= MODBUS_FRAME_SIZE &&
_requestBuffer[MODBUS_ADDRESS_INDEX] == MODBUS_BROADCAST_ADDRESS);
} |
@yaacov tried it but still not working... |
hmm ... , so after removing the @asefd @Proace thanks for debugging this 💚 , I do not have MEGA or UNO to check it, so my help is limited to looking at the code ...
@asefd , can you check if you can find fix ( that does not printout debug data :-) ) ? |
@yaacov sure I will try to find a fix. For the moment I only have the MEGA to test but as soon as I find something I can ask maybe a friend of mine to test on other devices. |
@yaacov at line 635, do we really need to check if broadcast? If I bypass this, all good. |
solution is removing random ; found in the following routine (shown commented out, at the bottom): uint16_t Modbus::writeResponse()
// ; |
@cass3825 Thanks, did you check on a Mega ? Yes, we know about this bug: #55 need to fixed ! AFAIU @asefd 's problem is not related ... see #54 (comment) :-( p.s. |
Yes, removing the ; allowed the library to run successfully on a Mega2560. I will try to make a pull request if i can remember how.
Thanks for this library!
On Monday, April 13, 2020, 02:02:51 AM CDT, Yaacov Zamir <notifications@github.com> wrote:
@cass3825 Thanks, did you check on a Mega ?
Yes, we know about this bug: #55 need to fixed !
AFAIU @asefd 's problem is not related ... see #54 (comment) :-(
p.s.
Can you make a pull request fixing #55 ?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or unsubscribe.
|
The first parameter of the callback is the slave id of the request. What is it? If you are not sending broadcast (slave id == BTW, make sure that you are not sending a broadcast message in the first place. |
Hi,
I am trying the library on Arduino Mega 2560. Receiving the queries works ok but writeRegisterToBuffer doesn't write the response to any Serial. I tried all the serials but none works.
I tried HoldingRegisters function ans also InputRegisters.
It does enter the function to handel the queries but no write to serial is being done.
Any idea?
Thanks
The text was updated successfully, but these errors were encountered: