0

replace ctrl-c, e.g. by ctrl-g
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

replace ctrl-c, e.g. by ctrl-g

by bludin on Fri Dec 18, 2020 12:17 pm

I have the following problem:
I want to use a circuitpython device (itsybitsy M4, but I think that's irrelevant) to receive and forward communication between a PC and an Omron temperature controller that uses the Compoway protocol. It shall receive messages via USB (stdin) and forward them to RS232 (busio.UART) and vice versa. That works fine in principle because the Comopway messages are 7bit standard ASCII. The only problem is that they are always framed by STX and ETX = 0x02 and 0x03 = ctrl-b and ctrl-c. Ctrl-b is no problem, but ctrl-c, when received via stdin, elicits a KeyboardInterrupt, i.e. CP quits. Catching it with try/except Keyboardinterrupt never works 100% and even if it did, it would make the programming a lot more cumbersome (because the CP device shall actually not just forward but also analyze and manipulate the messages in certain cases).

To cut a long story short: would it be possible to have CP take a different code for the KeyboardInterrupt, e.g. BELL = 0x07 = ctrl-g? If so, how big would the effort be to achieve this?

bludin
 
Posts: 40
Joined: Thu Apr 16, 2020 8:57 am

Re: replace ctrl-c, e.g. by ctrl-g

by danhalbert on Fri Dec 18, 2020 12:42 pm

Switching the interrupt character would require a custom build of CircuitPython. There is one `#define` to change, but this is probably more trouble than adding some escaping code to your commmunications programs to handle special characters.

danhalbert
 
Posts: 2262
Joined: Tue Aug 08, 2017 12:37 pm

Re: replace ctrl-c, e.g. by ctrl-g

by bludin on Fri Dec 18, 2020 12:58 pm

Thanks a lot for the prompt reply. I have no influence on protocol, only on the CP device. And learning how to build CP is high on my list anyway :)
Would you happen to know where to look for the #define statement?

bludin
 
Posts: 40
Joined: Thu Apr 16, 2020 8:57 am

Re: replace ctrl-c, e.g. by ctrl-g

by danhalbert on Fri Dec 18, 2020 1:08 pm

The interrupt character is enabled here: https://github.com/adafruit/circuitpyth ... xec.c#L112
But looking at it further, you don't want to change the #define, since there are other uses for the ctrl-c definition that are not for interrupts.

There may be several other places where ctrl-c is checked without using that define, so you'll need to track them down.

We have been talking about adding a second USB-serial (CDC) channel that is not the REPL (and would not have this ctrl-C) issue, but that's not an immediate priority

danhalbert
 
Posts: 2262
Joined: Tue Aug 08, 2017 12:37 pm

Re: replace ctrl-c, e.g. by ctrl-g

by bludin on Fri Dec 18, 2020 2:09 pm

Ok, thanks again. Yes, I've followed the discussions around a second USB channel and for me, that would be a tremendously useful feature in many ways.

bludin
 
Posts: 40
Joined: Thu Apr 16, 2020 8:57 am

Re: replace ctrl-c, e.g. by ctrl-g

by bludin on Tue Jan 05, 2021 4:37 pm


bludin
 
Posts: 40
Joined: Thu Apr 16, 2020 8:57 am

Re: replace ctrl-c, e.g. by ctrl-g

by danhalbert on Tue Jan 05, 2021 4:55 pm

bludin wrote:I just stumbled over https://circuitpython.readthedocs.io/en/6.0.x/docs/library/micropython.html#micropython.kbd_intr which does exactly what I need

This exists and you can set it, but it does not work in circuitpython.

danhalbert
 
Posts: 2262
Joined: Tue Aug 08, 2017 12:37 pm

Re: replace ctrl-c, e.g. by ctrl-g

by bludin on Wed Jan 06, 2021 3:18 am

well...it does, partially. The re-assignment to another character doesn't seem to work. But any other value than 3 will disable the KeyboardInterrupt by ctrl-c perfectly fine in my hands. That's not as convenient as it could be (and not as clean as having a second channel, by far), but it's already worth a lot for my application.

bludin
 
Posts: 40
Joined: Thu Apr 16, 2020 8:57 am

Re: replace ctrl-c, e.g. by ctrl-g

by bludin on Thu Jan 07, 2021 6:06 am

....it works with some caveats. It looks to me like ctrl-c needs to be disabled in each module that accesses/reads StdIn. Does that make sense?

bludin
 
Posts: 40
Joined: Thu Apr 16, 2020 8:57 am

Re: replace ctrl-c, e.g. by ctrl-g

by danhalbert on Thu Jan 07, 2021 9:17 am

Do you mean by calling micropython.kbd_Intr() over and over? No, that would not be per module. But ctrl-C is hard-coded several places.

As usual, we don't do schedule promises, but we now have plans to work on the second CDC channel, which I mentioned as a possibility earlier.

danhalbert
 
Posts: 2262
Joined: Tue Aug 08, 2017 12:37 pm

Re: replace ctrl-c, e.g. by ctrl-g

by bludin on Fri Jan 08, 2021 4:07 am

Not over and over. Just once upon importing the module.

I'm looking very much forward to you keeping your ungiven promises ;-)

bludin
 
Posts: 40
Joined: Thu Apr 16, 2020 8:57 am

Re: replace ctrl-c, e.g. by ctrl-g

by jp23 on Sat Jan 16, 2021 9:49 am

Greetings, it looks like I have a similar request:

viewtopic.php?f=60&t=174186&p=849083#p849057

jp23
 
Posts: 3
Joined: Fri Jan 15, 2021 11:33 pm

Please be positive and constructive with your questions and comments.