My solution isn't the prettiest, but it works.
First, a full minecart on top of a detector rail with a comparator emits a signal strength of 15 (Full cart - 1 item emits 14). We can also subtract signals using one comparator and a signal coming from the side so this measurement won't take too much space.
With this idea in mind, I made this contraption:
A 2x3 rail cycle with the exit (bottom right normal rail) always powered by a torch (not the case of the pic as the hopper is empty) which would result in the cycle being closed.
On the right side of the picture, we have the hopper with a comparator and torch, so when it is empty, it emits a signal, turning off the torch bellow the entrance rail, making it open (Exactly as seen in the pic)
On the left we have the same comparator situation described earlier, powering the torch below the entrance in case the cart is full.
This solution has minor problems:
- It wont fill the cart chest at full speed (Should be around 0.8x as fast). This can be solved by putting more hoppers around the track, but that would mean more hoppers with comparators in a cramped space.
- The cart is always moving, contributing to server lag (Even if in extremely small quantities).
- Is relatively large for a simple task.
In case you have a farm/storage that outputs different type of items, the cart will only leave if every stack has 64 units. This could even totally freeze the system by having the hopper flooded with one type of item and the car needing another one.
You can solve this by making the comparator from the detector rail receive less than 14 signals from the side. Each 2 stacks-ish represents a signal, except for 0 and 15 which are Full and Empty carts