Maryn
Maryn

Reputation: 35

Microcontroller receives the value "255ld" continuously via virtual serial port

I hope this is not a silly question, but I'm not able to find the answer to my problem myself.

I'm working on an Atmega32U4 microcontroller device, which is supposed to receive a series of bytes and store them. My code is based on a CDC Virtual Serial driver template from the LUFA-Library 151115. In order to test my code, I use the very useful tool ScriptCommunicator 04.11. The microcontroller unit (MCU) is connected via USB to the PC and is treated as a serial com port device by the PC host. My OS is Windows 7 64 bit.

Currently, I'm writing a function in which six values shall be received and stored. For that I use the following code:

int16_t Register_1 = 0;     
int16_t Register_2 = 0;     
int16_t Register_3 = 0;     

int16_t Register_4 = 0;
int16_t Register_5 = 0;
int16_t Register_6 = 0;     

int16_t serial_byte = 0;
int8_t loop = 1;

do
{
    serial_byte = CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface);

    serial_byte &= 0x00FF;

    switch (loop)
    {
        case 1:     Register_1 = ad7194_byte;       break;
        case 2:     Register_2 = ad7194_byte;       break;
        case 3:     Register_3 = ad7194_byte;       break;
        case 4:     Register_4 = ad7194_byte;       break;
        case 5:     Register_5 = ad7194_byte;       break;
        case 6:     Register_6 = ad7194_byte;       break;
    }


    CDC_Device_USBTask(&VirtualSerial_CDC_Interface);

    USB_USBTask();

    loop ++ ;

    printf("%i" PRId32 "\n", serial_byte) ;

    _delay_ms(1000);

} while (loop < 7);

I have previously used a similar code snippet and everything seemed to be working fine. Now I identified the following problem: The device is continuously reveiving the value "255ld". For when I test the above code with the ScriptCommunicator, I receive this value six times(which is why the printf-statement is included in the above code).

I have the suspicion that this happens because Windows is treating the MCU as a common USB device, e.g. a mouse? Could that be the case?

Or what could else be the reason for this?

How can I work around my problem?

Thanks in advance for any hints and answers!

Upvotes: 1

Views: 112

Answers (2)

Maryn
Maryn

Reputation: 35

Thanks for the answer, which did not solve my problem directly, but kind of brought me to the solution.

Solution: I received the 255 values although I didn't send anything to the MCU. But: If no byte is received, the LUFA CDC_Device_ReceiveByte-function returns -1. I forgot to check for that value before the serial_byte &= 0x00FF; statement. The -1 value was then mixed up and the 255 value turned out. Silly mistake again, which costed me a lot of time again.

Upvotes: 0

chux
chux

Reputation: 154146

Wrong use of format.

int16_t serial_byte = 0;
//          v (remove i) 
// printf("%i" PRId32 "\n", serial_byte) ;
//             16 not 32
printf("%" PRId16 "\n", serial_byte) ;

Upvotes: 1

Related Questions