Jump to content
The simFlight Network Forums

Recommended Posts

Posted

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!

Posted
On 11/13/2017 at 8:05 AM, ariz said:

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)?

Possibly a mismatch. I don't know Arduino, but it looks like the "Serial.print" function sends exactly the numerical value of what you give it, not the character it represents. 

255 is hex "FF"  which realy has no normal printable character reprsentation, but 48 is the code for the character '0'. Maybe the Arduino Serial.read function doesn't wait for characters and returns 255, or more likely -1 (which would look the same as 255 is only an 8-bit part was used) if no data was (yet) available.

Either way, I think you need to investigate the Arduino side of things.

Pete

 

Posted

Yes Pete, you are absolutely right!

I added a line to let the arduino to wait before reading, and everything works now!

Cheers Pete

PS. If anyone ever wonders how I fixed it, simply add: while (!Serial.available()); above the Serial.read() line. It will tell the arduino to wait for incoming character before reading it

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use. Guidelines Privacy Policy We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.