Conmutation between pines

Hi, I have a question about which code I should use for the following:

I need to communicate with an arduino through the serial port by sending commands. To do this, I need to activate, for example, pin 4, and when sending the next command, for example, activate pin 6 but turn off pin 4 automatically before sending only the following command.

The part of the communication I have solved, so I only need the way to activate and deactivate pins automatically. I have searched for codes of this type for several sites but none of them matches what I need.

I would like to know first if it is possible, and if it is possible if someone has a solution.

Thank you.

PD: I have an Arduino Mega.

Don't double post even in 2 languages.... answer in the other thread