Aaron Caffrey
Aaron Caffrey

Reputation: 1

Sending and receiving int value to/from serial port

I am developing a window's application that sends and receives data to an Arduino via the serial port.

The problem I have is when I try to send an int value to the serial port and pick it up in the Arduino program. I am using WriteFile() in the window's program to send the int and pick it up in the Arduino using Serial.parseInt(). When I send char values I can pick them up with no problems.

If I want to send int values the only time I receive an integer in the arduino is if I send integers 48 to 57 which give me an int value of 0 to 9 which are the ASCII characters for decimal 48 to 57, weird. e.g. 57, The Arduino seems to pick this up as 9 which is the char value for ASCII 57.

below is the code in the windows program and the Arduino

C++ code:

DWORD ComPort::WriteAnalogData(int *buffer)
{
    if (!connected)
        return 0;

    DWORD bytesSent;
    DWORD errors;

    if (!WriteFile(hCom, (void *)buffer, sizeof(buffer), &bytesSent, 0))
    ClearCommError(hCom, &errors, NULL);    // If it fails, clear com errors
    else return bytesSent;      // Return the number of bytes sent
    return 0;                   // Or return 0 on failure
}

the return value(bytesSent) after the int is written is 4 so I think this function is working. I have a similar function for sending single bytes of data which works and the Arduino picks the data up ok

Arduino Code:

int scadaAnalogVal;
scadaAnalogVal = Serial.parseInt();
digitalWrite(scadaAnalogVal,HIGH);

can anyone tell me what's going on. Probably something simple but i can't see what the issue is, thanks.

Upvotes: 0

Views: 1436

Answers (1)

TomServo
TomServo

Reputation: 7409

The Arduino parseInt() method has a long return type (32-bits) in the Arduino architecture. I think you should change your Arduino code to:

long scadaAnalogVal;

ParseInt documentation

Upvotes: 0

Related Questions