clear CDC buffer

17 views
Skip to first unread message

vsurducan

unread,
Aug 4, 2025, 2:24:56 PMAug 4
to jal...@googlegroups.com
Hi,
I'm trying to clear the last received character through USB communication, however it seems that last char is  reloaded from the receiving buffer.
How can be cleared the last position of the CDC buffer?

thank you,

vsurducan

unread,
Aug 5, 2025, 5:09:37 AMAug 5
to jal...@googlegroups.com
Ok, here is one solution. If I do  no clear the CDC rx buffer, after the first press of "D" or "d"character  the chr value remains the same.
To clear the CDC RX buffer, the pointer must be first decremented, than the value stored in the buffer cleared.
A new press on "D" or "d" works ok now.

 forever loop
 if (chr == "d") | (chr == "D") then
    for 8 using i loop
       format_word_dec (usb_serial_data, some_data[i], 4, 0)
       print_crlf (usb_serial_data)
    end loop
       print_crlf (usb_serial_data)
;    chr = 0          ; chr "D" or "d"  is not cleared with this
     cdc_rx_rd = cdc_rx_rd - 1       ; decrement pointer cdc_rx_rd
     cdc_rx_buffer[cdc_rx_rd] = 0    ; chr is now 0

  end if
 end loop

Rob CJ

unread,
Aug 5, 2025, 1:11:03 PMAug 5
to jal...@googlegroups.com
Hi vsurducan,

I do not understand the problem. I never had issues with the USB library that a character is reloaded again from the receive buffer.

Is it not a problem of the PIC you are using? Have you checked the errata document?

Kind regards,

Rob


Van: jal...@googlegroups.com <jal...@googlegroups.com> namens vsurducan <vsur...@gmail.com>
Verzonden: dinsdag 5 augustus 2025 11:09
Aan: jal...@googlegroups.com <jal...@googlegroups.com>
Onderwerp: [jallib] Re: clear CDC buffer
 
--
You received this message because you are subscribed to the Google Groups "jallib" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jallib+un...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/jallib/CAM%2Bj4qsM9KHOmFegcU0GjWeRxvSggDbVz84pCeddjQbGsbj5Zw%40mail.gmail.com.

vsurducan

unread,
Aug 5, 2025, 1:24:29 PMAug 5
to jal...@googlegroups.com
Hi Rob, 
I'm sending a command via USB by pressing a key,  usbserial with interrupts. I am also use other interrupts as well. After the key is pressed, to stop the sequence I  need to press any other key or to clear the receiving buffer. I do not want to press other key. One press equals one command. Clearing the buffer do the job fir the command, but is freerzing sometime the communication, it needs some workaround on other usb registers.
We discussed about the usb library issues many times in the past, mostly abot its low speed. Microcontroller is ok, PIC 18F26j50.


Oliver Seitz

unread,
Aug 6, 2025, 1:30:00 AMAug 6
to jal...@googlegroups.com
Hi Rob,

we also had discussions about serial_int_cts in the past, it was mainly due to people abusing the circular receive buffer as linear input buffer. Instead of using the library's designated functions to read one character from the circular buffer, they try to read directly from the circular buffer, which is meant to be internal to the library only. 

This might be a similar issue. 

Greets,
Kiste

Am Dienstag, 5. August 2025 um 19:11:04 MESZ hat Rob CJ <rob...@hotmail.com> Folgendes geschrieben:


vsurducan

unread,
Aug 6, 2025, 3:02:05 AMAug 6
to jal...@googlegroups.com
Oliver, that is just because else it does not work properly.
How to use the usb serial to send a single ascii command to the picmicro, in return the microcontroller will send a burst of data and stop.
Sending again the same command, returning another burst of data.
This is not working for me. I can presume there is an interrupt issue which interfere with my code.

The serial int_cts does work much better (and faster) by "abusing it".:) 
The usb serial unfortunately does not.
As long you have a pointer and a circular buffer, I think is not a sacrilege trying to work directly with them.

Oliver Seitz

unread,
Aug 6, 2025, 4:33:24 AMAug 6
to jal...@googlegroups.com
If you go to the tool shop to have a screwdriver replaced because the nail won't go in, whether you're hammering with the tip or the handle, you won't succeed. You can use any tool in any way, but if you're using it in a way it wasn't designed for, and it doesn't work as you expect, you can't call that an error. If you want to use a ring buffer as a linear buffer, feel free, but it's better to understand the mechanism rather than trying to change it.

Greets,
Kiste

Am Mittwoch, 6. August 2025 um 09:02:11 MESZ hat vsurducan <vsur...@gmail.com> Folgendes geschrieben:


vsurducan

unread,
Aug 6, 2025, 5:41:03 AMAug 6
to jal...@googlegroups.com
You did not help me. A tool is a tool. You will never know all the ways it can be used. Just watch on youtube for the hammer + drilling machine possibilities.

So far the usb serial does not work  for the explained situation. It retain the last received char in the circular buffer. To use the same char for next command you have to either clear the whole rx buffer, or at least the place where the char is located. Disabling  the usb interrupt during buffer clear  might be usefull.




Oliver Seitz

unread,
Aug 6, 2025, 5:53:20 AMAug 6
to jal...@googlegroups.com
Ok, so here's the help: If the reading pointer and the writing pointer is the same, don't read the character. So you won't get a character twice, for you don't read it.

Last time you chose to ignore that advice, so I thought to omit it now.

When you use tools in a way they are not designed for, you need to find out how. Not complain.


Am Mi., Aug. 6, 2025 at 11:41 schrieb vsurducan

vsurducan

unread,
Aug 6, 2025, 7:43:32 AMAug 6
to jal...@googlegroups.com
Uh, I do not remember that advice, sorry, but now I have fixed it on document. 

The usbserial have different circular buffers for rx and tx with different sizes, so I do not know if the advice will work, mostly not...

It  works as described. Turn off USB interrupts, clear the rx circular buffer,  turn on the USB interrupts.
But the usb cdc driver is very lazy...
 Congrats to Albert Faber because it is a super complicated piece of software.
But it works below 115200bps (around 100-105kbps)  which is the maximum which can be obtained no matter if set or not other communication speed.

Rob CJ

unread,
Aug 6, 2025, 1:45:59 PMAug 6
to jal...@googlegroups.com
Hi Vsurducan,

Is it possible to make a simple program that shows your problem and send it to me? I would like to reproduce is because I still do not recognize the problem you mentioned. And with the program can you tell the exact reproduction scenario?

And about the speed of the USB library. The speed can be increased by using the ping-pong buffers. This is not implemented. I tried this a few years ago to add that but it did not work. I did not spend too much time on it so maybe I could see if I can get that working. It would - I assume - more or less double the speed since one buffer is then read while the other buffer is filled.

Kind regards,

Rob



Verzonden: woensdag 6 augustus 2025 13:43
Aan: jal...@googlegroups.com <jal...@googlegroups.com>
Onderwerp: Re: Re: [jallib] Re: clear CDC buffer
 
Reply all
Reply to author
Forward
0 new messages