You will probably have a pressure point that you can exploit with respect to the sale. ie you can refuse to work for potential new owners. Even if you are pretty replaceable, I think most potential buyers would be scared off by having to replace the sole software engineer who understands the codebase. eg the small software business sales I'm aware of typically come with commitments around the engineering staff. Be aware that doing this may burn your relationship with the current owner. As in all negotiations, make sure that you are actually willing to live with any options you bring up. Your rights here are so fact dependent that you must get legal counsel in your jurisdiction.
One smart way to approach this negotiation would be to write a contract agreeing to a minimum work period for the potential new owners; as a cost, you would need some percent of the sale price. Again, you must get yourself a local attorney experienced in these types of negotiations.
One point I would bring up though, is that engineers often wildly overestimate how easy it is to turn lines of code into cash. It is quite possible that the business end of this business holds all the leverage. When OP says eg he or she has "worked many hundreds of hours", this sounds like low maintenance software that the business owner can continue to sell for quite a long while without any ongoing engineering.