Why Arduino waits forever to receive the ACK?

As I asked upon here In try to initiate sdata sending via serial and read the via python.

What I want is to send an ACK signal in order to initiate the data sending:

import serial
from time import sleep
import base64

def ACK():
    ser.write(0x06)
    ser.write(0xD)
    ser.write(0xA)

ser = serial.Serial('/dev/ttyACM0', baudrate=9600, timeout=1)

if not ser.isOpen():
    ser.open()

print("OPEN Serial\n")
print("READ DATA\n")
while 1:
    ACK()
    line = ser.readline()
    if(line == b""): continue;
    if(line[:1] == b'2'):
        print("DATA:")
    print(line)
    print("####")

And receive it via this sketch:

#define LASTBLOCK 1023
#define LASTPAGE 63
#define LASTBYTE 2111

#define PAGE_READ 0x13
#define PAGE_READ_CACHE_SEQUENCIAL 0x03
#define READ_FROM_CACHE 0x3B
#define PAGE_READ_CACHE_END 0x3F

#define PAGE_SIZE 2111 
#define BUFFER_LEN 32 

#define ACK 0x06
#define NACK 0x06


unsigned int page_to_read = 1;
unsigned int block_to_read = 1;
boolean spi_begun = false;
uint8_t bytes_index = 1;

typedef struct  {
  // Page that is transmitted
  unsigned int page;

  // Block that is transmitted
  unsigned int block;

  // A mumeric representation of the 32 byte segment that I send. 
  // I cannot send the page altogether, therefore a byte intexing is required
  uint8_t bytes_index;
  
  // Despite my buffer is 32 bytes the usable data may be less
  uint8_t length; 

  // Data buffer containing the data to be sent via serial
  byte data[BUFFER_LEN];
} usb_msg;


void sendMsg(usb_msg msg){
  byte* ptr = (byte*)&msg;
  Serial.print(0x02);
  for (size_t i = 0; i < sizeof(usb_msg); i++){
    Serial.print(*ptr >>4, HEX);
    Serial.print(*ptr & 0x0F, HEX);
    ptr++;
  }
  Serial.print(0x03);
  Serial.println();
}

bool clientAck()
{
  uint8_t incomingByte = NULL;
  digitalWrite(13,HIGH);
  do {
    uint8_t incomingByte = Serial.read();
  } while(incomingByte!=ACK);
  digitalWrite(13,LOW);
}

void setup() {
 Serial.begin(9600);
 pinMode(13,OUTPUT);
}

void loop() {
  
  // I need to reset the counts before reading any block 
  if(page_to_read > LASTPAGE){
    page_to_read = 1;
    block_to_read ++;
  }

  if(block_to_read > LASTBLOCK){
    spi_begun=false;
    return ;
  }

  if(!spi_begun){
    spi_begun=true;
  }

  clientAck();
  // Reading page there's no need for seperate function in order to avoid stack usage
  for(int bytes_to_read = PAGE_SIZE; bytes_to_read > 0; bytes_to_read-=BUFFER_LEN){
    uint8_t bytes_to_send = bytes_to_read < BUFFER_LEN ? bytes_to_read: BUFFER_LEN;
    usb_msg msg = {page_to_read, block_to_read, bytes_index, bytes_to_send, {1, 1, 1, 1, 1, 1, 1, 1, 1, 1}};
    sendMsg(msg);
    delay(100);
    bytes_index++;
  }
  
  page_to_read++;
  delay(100);
}

But arduino in:

bool clientAck()
{
  uint8_t incomingByte = 0;
  digitalWrite(13,HIGH);
  do {
    incomingByte = Serial.read();
  } while(incomingByte!=ACK);
  digitalWrite(13,LOW);
}

Waits forever, do you know why?
What I try to do is to dump a serial flash memory and as a first step I try to wirte the USB IO code, so I can focus on dumping itself as a latter step.

after ser.open() the Arduino resets (auto reset) and waits a short time in bootloader

Why do I see no attempt to serial.Print() variables and status as your program executes?

It is kinda hard because anythink, I Serial.print is sent upon a python script that Accepts a hex encoded string.

I could somehow had some sort of hjijacker and sniff any Serial communication without Serial.print. Can I do it via wireshark?

So I have make the script sleep for a while right?

Tried both with

sleep(0.3)

or

sleep(2)

After:

ser = serial.Serial('/dev/ttyACM0', baudrate=9600, timeout=1)

But still the same behaviour happens.

If you add a software serial connection, you could use that to go to a second PC or other terminal program. You seemed to have worked yourself into a corner with no easy way out.

Can I use Software Serial as a means for secondary UART connection so I can echo any debug Message in it?

I mean I can use 2 arduinos one as serial dump and one executing the code.

What would the second one do that the first one cannot do?

Sniff USB data and echo it back in a secondary USB port in a seperate Serial monitor.

And how would you be able to debug the code for this?

I can echo via software serial to another arduino and the secondary arduino will echo back to another USB.

Would it not be quicker and easier to add This to your Arduino and display the debug message on it?

It would be easy. But I do not have it and the cheaper one will take 1 month for me to come from China. Therefore, I hafta use anything I have in my hand.

Then modify the PC program to accept two types of messages, One type to display on the PC and other to process. Make similar changes to the Arduino program to send debug messages to the PC.

Or can Use a simple screen as second serial monitor.

It is as easy as using this sketch for my debug Monitor:

#include <SoftwareSerial.h>

SoftwareSerial mySerial(2, 3); // RX, TX

void setup()
{
  // Open serial communications and wait for port to open:
  Serial.begin(115200);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Native USB only
  }


  Serial.println("Goodnight moon!");

  // set the data rate for the SoftwareSerial port
  mySerial.begin(38400);
}

void loop() // run over and over
{
  if (mySerial.available())
    Serial.write("RECEIVED: \n");
    Serial.write(mySerial.read(),HEX);
    Serial.write("\n");
    Serial.write("######### \n");
  sleep(300)
}

And I can just open a serial in my sketch and use it as my debug connection.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.