0

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.

FortyTwo
  • 2,414
  • 3
  • 22
  • 33

1 Answers1

0

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

TomServo
  • 7,248
  • 5
  • 30
  • 47
  • Tried That still get the same problem – Aaron Caffrey May 25 '17 at 20:30
  • Not sure what else to suggest without duplicating your entire experiment. My answer deserves an upvote though (not an accept) because it at least corrected the type of the return value. ;) That's important when working with different processor architectures. – TomServo May 25 '17 at 20:39