I tried the Serial.cpp code from Arduino’s website.
I just made a couple of changes and got the code working properly. However, the issue is, the C++ code serially sends in ASCII values and not integers. For example, if I send in “5” I receive 53 at the receiver side.
I tried changing the buffer type to an int instead of a char, but it returned an error saying:
error C2664: ‘ReadFile’ : cannot convert parameter 2 from ‘int’ to ‘LPVOID’
Is there a way to send integers instead of ASCII values serially?
Below is a snippet of the code.
bool Serial::WriteData(char *buffer, unsigned int nbChar)
{
DWORD bytesSend;
//Try to write the buffer on the Serial port
if (!WriteFile(this->hSerial, (void *)buffer, nbChar, &bytesSend, 0))
{
//In case it don't work get comm error and return false
ClearCommError(this->hSerial, &this->errors, &this->status);
return false;
}
else
return true;
}
int main() {
// Convert from char* to wchar*
char *name_ser="COM7";
cout << name_ser << "(char*)" << endl;
size_t origsize=strlen(name_ser)+1;
const size_t newsize=100;
size_t convertedChars=0;
wchar_t wcstring[newsize];
mbstowcs_s(&convertedChars, wcstring, origsize, name_ser, _TRUNCATE);
wcscat_s(wcstring, L"(wchar_t*)");
wcout<<wcstring<<endl;
Serial serial(wcstring);
if (serial.IsConnected()){
while (1){
char *chr0 = "5";
serial.WriteData(chr0,1);
cout<<chr0<<endl;
}
}
system("pause");
return 0;
}
You’ll probably need to change the method signature. Try something like this:
You’ll need to reconstitute the integers on the Arduino:
ints are typically 4 bytes, and I expect the Arduino receive routines are expecting ASCII by default.