alioth Posted October 13, 2018 Report Share Posted October 13, 2018 (edited) Hi. I am trying to interface Arduino with p3dv4 Sending data from lua script to Arduino is fine. And no problem receiving data in arduino side. To read data coming from arduino in lua script, I really like the event.com concept. It makes (or should) things easy. The idea is to isolate a string between '/n' or whatever character, so I can manage a string as a complete comand. The comands I send from arduino have the format "A=xxxxx/n", and I have manage to isolate the key 'A' and the number xxxxx But I having problems to isolate the "A=xxxxx" and not being mixed with the command before or behind. This is an example code: Quote function Arduino_Data(Arduino_Com_Port, strng, length) ipc.display (strng) if string.match(strng, "=") then --codes with '=' inside, isolate the key (before the '= ')and value (behind =), then send de command with the value. local key = string.match(strng, "^.-=") key = string.sub(key, 1, -2) --now the key is isolated local value = string.match(strng, "=.*$") value = tonumber(string.match(value, "[^=%s].*")) --number isolate if string.match(key, "A") then ipc.control(65716, value) end else -- codes without =, direct command -- if string.match(strng, "A23") then ... -- if string.match(strng, "F14") then ... end end -- function end event.com(Arduino_Com_Port, 12, 2 , '/n' , "Arduino_Data") I am sending the data from arduino every 500 milisecs. Quite slow, to be sure no problems with buffers.. (I think). I have test '/n', 13, even other characters like 'X', space. But the string comes truncated. I see it with ipc.display (strng) Only works if using Quote event.com(Arduino_Com_Port, 12, 9 , '/n' , "Arduino_Data") I don't know the reason. Is '/n' two bytes and then total lenght is 9 perhaps..? With the 9, I see in the ipc.display window a blank line, and in the next line the complete code. So, it works in this way. I can use this method if always sending the same commands lenght... But I would like to use diferent string lengths with "term" parameter working. What I am doing wrong? 😕 thanks in advance Arturo. Edited October 13, 2018 by alioth Link to comment Share on other sites More sharing options...
Pete Dowson Posted October 19, 2018 Report Share Posted October 19, 2018 On 10/13/2018 at 10:51 AM, alioth said: I don't know the reason. Is '/n' two bytes and then total lenght is 9 perhaps..? Yes, '\n' is two characters: '/' and 'n'. A new line is '\n'. You are using the wrong Escape character. All of the special characters use \,. For a single \ you need \\. Pete Link to comment Share on other sites More sharing options...
alioth Posted October 22, 2018 Author Report Share Posted October 22, 2018 (edited) Thanks for the answer and sorry for my mistake 😕 (oh my god). But, I have problems yet. I have installed a serial monitor software, as you suggested in some thread (Now I dont know how I was living without it). The bytes sent from Arduino in a typical command is something like this: 41 3D 30 30 30 37 39 0D 0A A=00079.. So, the "\n" are the 0x0D + 0x0A bytes. With this command format, if I use Quote event.com(Arduino_Com_Port, 9 , 9 ,0x0A, "Arduino_Data") it works fine all the time. But, If I use Quote event.com(Arduino_Com_Port, 16 , 4 ,0x0A, "Arduino_Data") As I know, it should work the same way. And It works most of the time. But something like 1/100 I receive a diferent format string and my functions fails to isolate the key and the value. Can be related to event.com function? With " 9 , 9" works always fine.. Thanks. EDIT: I have looged the string received from event.com and I get this: Quote 3822250 LUA.0: A=00260 3822359 LUA.0: A=00262 3822453 LUA.0: A=00263 3822484 LUA.0: B=00029 3822578 LUA.0: A=00265 3822781 LUA.0: A=00266 3822984 LUA.0: A=00265 3823093 LUA.0: A=00263 3823203 LUA.0: A=00 3823218 LUA.0: 261 3823297 LUA.0: A=00259 3823422 LUA.0: 3823453 LUA.0: A=00256 3823531 LUA.0: A=00255 3823547 LUA.0: B=00028 Edited October 22, 2018 by alioth Link to comment Share on other sites More sharing options...
Pete Dowson Posted October 22, 2018 Report Share Posted October 22, 2018 1 hour ago, alioth said: But something like 1/100 I receive a diferent format string and my functions fails to isolate the key and the value. By having a miimum of 4 acceptable, if there is a delay after 4 characters are received then you will get what has already arrived. The delay may be from the Arduino end, or it could be a higher priority action happening in the PC. Altough I do set the comms input at high priority, it isn't at "critical" level, which would be needed to have exclusive use of a processor core for that thread, but not warranted and problematic. 1 hour ago, alioth said: Can be related to event.com function? With " 9 , 9" works always fine.. So why not use that? Fixed length messages are bound to be more reliable and easier to handle. 3823203 LUA.0: A=00 3823218 LUA.0: 261 This illustrates what I said. 4 characters arrived but there was a delay before the "261" part. 3823297 LUA.0: A=00259 3823422 LUA.0: Here the delay was before the terminator you otherwise wait for. In any case, shouldn't you be discarding invalid input? Pete Link to comment Share on other sites More sharing options...
alioth Posted October 22, 2018 Author Report Share Posted October 22, 2018 1 hour ago, Pete Dowson said: So why not use that? Fixed length messages are bound to be more reliable and easier to handle. This was my thought, but I wanted to understand what was happening. Now is perfectly clear. I will use fixed length, and I now understand the needed of some line discarding invalid inputs. Even with fixed length I supose it is a good idea. Thank you so much. Arturo. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now