Sending markers to EEG through serial port

What you are currently doing is sending the bytes corresponding to the literal set of characters '$typesoundfile1'. By putting the variable name inside quotes, it is no longer a variable name pointing to a numerical value. It is just a sequence of characters.

Assuming that typesoundfile1 represents an integer variable, you could just try this directly:

port.write(typesoundfile1)

i.e. if this is an integer variable, then it is already effectively a number, not a character string that needs to be translated. But if it does need to be expressly converted to a particular byte representation, then try something like this:

port.write(typesoundfile1.to_bytes(length = 1, byteorder = 'little')) 

Note that the dollar sign has no particular meaning in the Python language: it is just a marker used in PsychoPy’s Builder interface to indicate to Builder itself that a value should not be taken literally but should be regarded as a Python expression.

NB just noticed that you use time.sleep() in your code component. You must remove that. Builder operates on a drawing cycle which requires it to actively do things on every screen refresh. By using this statement, you will be mucking up Builder’s timing control seriously and unpredictably. If you have a need for such a pause, it must be implemented differently.