Hello everyone, as my title suggests, PIO Debugger is stopping in main()
but not at my breakpoint in setup()
or loop()
, as shown in the image below.
It stops in main()
but not at my breakpoint.
Here is my code main.cpp
#include <SPI.h>
#include <Ethernet.h>
// Pin Definitions
// Network Configuration
byte mac[] = { 0xA8, 0x61, 0x0A, 0xAE, 0x0A, 0xCC };
IPAddress server(192, 168, 123, 209); // ModbusTCP server address
IPAddress localIP(192, 168, 123, 207); // Local IP address as slave
EthernetClient ethClient;
EthernetServer slaveServer(502); // Modbus slave server
// ModbusTCP Configuration
uint16_t transactionId = 0;
uint16_t holdingRegisters[100]; // Holding registers array
bool isMasterMode = true; // Mode switching flag
unsigned long modeChangeTimer = 0; // Mode change timer
const unsigned long MODE_CHANGE_INTERVAL = 5000; // Mode change interval(ms)
unsigned long requestStartTime = 0; // Request start time
unsigned long lastRequestTime = 0; // Last request time
const unsigned long REQUEST_INTERVAL = 2000000; // 2 seconds = 2000000 microseconds
void sendModbusRequest(byte slaveId, byte functionCode, uint16_t startAddr, uint16_t quantity) {
byte request[12];
// Modbus TCP Frame Header
request[0] = highByte(transactionId);
request[1] = lowByte(transactionId++);
request[2] = 0x00; // Protocol Identifier High Byte
request[3] = 0x00; // Protocol Identifier Low Byte
request[4] = 0x00; // Length High Byte
request[5] = 0x06; // Length Low Byte
request[6] = slaveId;
request[7] = functionCode;
request[8] = highByte(startAddr);
request[9] = lowByte(startAddr);
request[10] = highByte(quantity);
request[11] = lowByte(quantity);
ethClient.write(request, 12);
}
void processModbusRequest(EthernetClient &client, byte *buffer, int len) {
uint16_t transId = (buffer[0] << 8) | buffer[1];
byte functionCode = buffer[7];
uint16_t startAddr = (buffer[8] << 8) | buffer[9];
uint16_t quantity = (buffer[10] << 8) | buffer[11];
if(functionCode == 0x03) { // Read Holding Registers
byte response[256];
int responseLen = 9; // MBAP header(7) + Function code(1) + Byte count(1)
// Fill response header
response[0] = buffer[0]; // Transaction ID
response[1] = buffer[1];
response[2] = 0x00; // Protocol ID
response[3] = 0x00;
response[4] = 0x00; // Length High Byte (filled later)
response[5] = 0x00; // Length Low Byte (filled later)
response[6] = buffer[6]; // Unit ID
response[7] = functionCode;
response[8] = quantity * 2; // Byte count
// Fill data
for(int i = 0; i < quantity; i++) {
response[responseLen++] = highByte(holdingRegisters[startAddr + i]);
response[responseLen++] = lowByte(holdingRegisters[startAddr + i]);
}
// Update length field
response[4] = highByte(responseLen - 6);
response[5] = lowByte(responseLen - 6);
client.write(response, responseLen);
}
}
void readModbusResponse() {
byte buffer[256];
int len = 0;
while (ethClient.available() && len < 256) {
buffer[len++] = ethClient.read();
}
if (len > 0) {
Serial.print("Response received, length: ");
Serial.println(len);
Serial.print("Data content: ");
// Print each byte in hexadecimal format
for(int i = 0; i < len; i++) {
if(buffer[i] < 0x10) {
Serial.print("0"); // Add leading zero
}
Serial.print(buffer[i], HEX);
Serial.print(" ");
}
Serial.println(); // New line
// Parse Modbus response
if(len >= 9 && buffer[7] == 0x03) { // Verify read holding register response
byte dataLen = buffer[8]; // Number of data bytes
if(len >= (9 + dataLen)) {
uint16_t registerValue = (buffer[9] << 8) | buffer[10]; // Combine high and low bytes
Serial.print("Register value: ");
Serial.println(registerValue);
unsigned long responseTime = micros() - requestStartTime; // Calculate response time
Serial.print("Response time (microseconds): ");
Serial.println(responseTime);
}
}
}
}
void runMasterMode() {
if (!ethClient.connected()) {
if (ethClient.connect(server, 502)) {
Serial.println("Connected to Modbus server");
} else {
Serial.println("Connection failed");
return;
}
}
// Check if sending interval has elapsed
if (micros() - lastRequestTime >= REQUEST_INTERVAL) {
requestStartTime = micros(); // Record sending time
sendModbusRequest(0x01, 0x03, 0x0000, 0x0001);
lastRequestTime = micros();
}
if (ethClient.available()) {
readModbusResponse();
}
}
void runSlaveMode() {
EthernetClient client = slaveServer.available();
if (client) {
byte buffer[256];
int len = 0;
while (client.available() && len < 256) {
buffer[len++] = client.read();
}
if(len >= 12) { // Minimum Modbus TCP frame length
processModbusRequest(client, buffer, len);
}
}
}
void setup() {
Serial.begin(9600); // Initialize Serial for debugging
// Initialize Ethernet
Ethernet.begin(mac, localIP);
slaveServer.begin();
// Initialize default register values
for(int i = 0; i < 100; i++) {
holdingRegisters[i] = i + 1;
}
}
void loop() {
// Switch between master/slave mode based on timer
if(millis() - modeChangeTimer >= MODE_CHANGE_INTERVAL) {
isMasterMode = !isMasterMode;
modeChangeTimer = millis();
Serial.print("Switching to ");
Serial.println(isMasterMode ? "Master Mode" : "Slave Mode");
}
if(isMasterMode) {
runMasterMode();
} else {
runSlaveMode();
}
}
and my platformio.ini
.
; PlatformIO Project Configuration File
;
; Build options: build flags, source filter
; Upload options: custom upload port, speed and extra flags
; Library options: dependencies, extra library storages
; Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html
[env:due]
platform = atmelsam
board = due
framework = arduino
debug_tool = stlink
upload_protocol = stlink
board_upload.offset_address = 0x80000
I have looked into solutions for similar issues, but they don’t seem to work for me.
I am using an Arduino DUE and an ST-Link V2.
Please help, thank you all!