Hi everyone
I am sorry if similar questions has been asked before, but I can't seem to find a solution yet. I'm quite new to lua and FSUIPC so pardon me for my lack of understanding. I have a question regarding com.write. I'm trying to send data to arduino via the serial communication using com.write function. I have successfully sent data, but I'm having trouble reading the actual data. Here is a snippet of my current code
... Code above here
-- Writer
testString=0
function comWriter(time)
... Some code here, bypassed
if testString == 200 then testString= "000" else testString=200 end
com.write(arduino, "*" .. testString)
end
-- Set the timer for the write
event.timer(200, "comWriter")
-- Receiver
function comReceiver(arduino, datastring, length)
-- Last 2 characters are 0d and 0a (line break and return carriage)
-- Eliminate these 2 characters
datastring= datastring(1, length-2)
-- Check if debugging
if datastring(1, 5) == "DEBUG" then
ipc.log(datastring)
else
-- Check how long is the identifier
-- This is the first character
identifierLength= tonumber(datastring[1])
-- Seperate identifier and values
-- identifier is the event ID found in PMDG sdk
identifier= datastring(2, identifierLength+1)
value= datastring(identifierLength+2); -- After the ID, up to end
-- Call the control for the identifier
ipc.control(baseID + identifier, value)
end
end
As you can see, I'm sending either '*200' or '*000' every 200 ms to the arduino. In return the arduino send back 'DEBUG ' plus what it read in bytes. Here's a snippet of the arduino code
if (Serial.available() && Serial.read() == '*')
{
readSerial();
}
void readSerial()
{
digitalWrite(6,HIGH);
byte il[3];
for (byte i=0; i<3; i++)
{
il[i] = Serial.read();
}
Serial.print("DEBUG ");
Serial.print(il[0]);
Serial.print(il[1]);
Serial.println(il[2]);
}
As you can see above, the arduino wait for a '*', then it reads the next 3 characters, then it sends back what it read along with the 'DEBUG ' string.
However, when I looked at my lua log file, the arduino returns weird results
...
61203 LUA: Global: testString = 200
61203 LUA: ...les\Lockheed Martin\Prepar3D v4\Modules\arduino1.lua:133
61219 LUA: Waiting for an event in "G:\Program Files\Lockheed Martin\Prepar3D v4\Modules\arduino1.lua"
61219 LUA: COM Input event: calling "comReceiver" in "G:\Program Files\Lockheed Martin\Prepar3D v4\Modules\arduino1.lua"
61219 LUA: ...les\Lockheed Martin\Prepar3D v4\Modules\arduino1.lua:142
61219 LUA: Local: datastring = DEBUG 255255255
....
61485 LUA: Global: testString = 000
61485 LUA: ...les\Lockheed Martin\Prepar3D v4\Modules\arduino1.lua:133
61500 LUA: Waiting for an event in "G:\Program Files\Lockheed Martin\Prepar3D v4\Modules\arduino1.lua"
61516 LUA: COM Input event: calling "comReceiver" in "G:\Program Files\Lockheed Martin\Prepar3D v4\Modules\arduino1.lua"
61516 LUA: ...les\Lockheed Martin\Prepar3D v4\Modules\arduino1.lua:142
61516 LUA: Local: datastring = DEBUG 48255255
...
So it returns 255255255 for '200', and 48255255 for '000' (instead of 200 for '200' and 000 for '000'). So I'm not entire sure what's going on here. Is there a mismatch between the datatype com.write sent and what arduino received (if that even make sense)?
If someone could point out to me what's wrong with my code, that would be great
Note, I have successfully send data from the arduino to the lua and its reading it just find (I used a rotary encoder to change heading, and it worked wonderfully).
Cheers!