USB to Serial Converter project from LUFA - Connected and disconnected

Go To Last Post
4 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello all!

 

I am working in a project which I am sending, constantly, data to the pc via the “USB to Serial Converter” project from LUFA. I am using the ATmega32U4 and the HTerm to receive the data.

The code works fine.

What I would like to know is: when I power the microcontroller on, the code already starts to send data even before I connect the terminal. Is it possible to change that? And how?

The problem is that the ring buffer gets full and then the data starts to overwrite the other data, etc etc…

I would like to stop putting data to the buffer if the terminal is disconnected. And then continue when the terminal is re-connected.

I would like to know if there is an event that fires when terminal is connected and disconnected. This way I could tell my code to stop/start adding data to the buffer.

 

Can anyone help me with that? Any tip is appreciated!

 

Thank you very much!!

This topic has a solution.
Last Edited: Wed. Feb 4, 2015 - 08:20 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The usual way this is done is with flow control. This prevents the sender from overrunning the receiver, where the receiver could but doesn't keep up.

Hardware flow control in UART and RS232 is normally done using the CTS (clear to send). Your can  study-up on that online.

Software flow control is very common since it doesn't not require the wired signals. There are many ways to do software flow control. They all  boil down to inserting into the data byte stream a special message saying "STOP" or "CONTINUE SENDING".

 

If your USB to serial 3.3V or 5V has the signals named above, and you can wire one to a spare I/O pin, you can sense that in your code, if the other end asserts that signal when it is alive and listening.

 

The ASCII tables have characters called XON and XOFF for this purpose. It gets tricky of your data itself can contain those characters XON/XOFF.  The solution is called byte-stuffing, where if one of the special characters occurs in the data flow, one inserts another one, and so on even if there are many of these in succession.

 

Other systems packetize each message with a header, payload, and trailer, and send then expect a response before sending another.

 

As you can see, there's no magic... you have to alter the microcontroller's code to bring in flow control of some sort.

 

It's not just for startup, it's to prevent data loss.

 

 

Last Edited: Wed. Feb 4, 2015 - 08:37 AM
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

mr.bojangles wrote:
I would like to stop putting data to the buffer if the terminal is disconnected. And then continue when the terminal is re-connected.

I would like to know if there is an event that fires when terminal is connected and disconnected. This way I could tell my code to stop/start adding data to the buffer.

The DTR signal can be used for this.  It is asserted when Hterm opens the COM port, and de-asserted when the COM port is closed.  See this post for an example of its use.  (Note: the example uses the LUFA version from February, 2012.)

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello all!

 

It worked!! Thank you very much for the tips!!