1

I want to communicate with my Arduino. I have the Arduino IDE installed and if I use it, everything works perfectly fine. Here is my sketch:

const int ledPin = 11; // the pin that the LED is attached to

void setup() {
  // initialize the serial communication:
  Serial.begin(9600);
  // initialize the ledPin as an output:
  pinMode(ledPin, OUTPUT);
}

void loop() {
  byte brightness;
  // check if data has been sent from the computer:
  if (Serial.available()) {
    // read the most recent byte (which will be from 0 to 255):
    brightness = (Serial.read()-48)*28;
    // set the brightness of the LED:
    analogWrite(ledPin, brightness);
    Serial.println(brightness);
  }
}

If I start the Serial Monitor now and type in "9", I get back an 252 and the LED jumps to full brightness. And if you type "0", the LED goes off!

This is what I wanted to recreate in Lua. I downloaded rs232 and then I wrote this script:

local rs232 = require "rs232"

local e, p = rs232.open('/dev/ttyACM0',{
  baud         = '_9600';
  data_bits    = '_8';
  parity       = 'NONE';
  stop_bits    = '_1';
  flow_control = 'OFF';
  rts          = 'ON';
})

input=io.read()
p:write(input)
print(p:read(1))
p:close()

But nothing happens. Even without the p.read() part.

Please help.

PS: My system is Linux CentOS 7 and the board is a Uno

dda
  • 6,030
  • 2
  • 25
  • 34
Okaghana
  • 57
  • 6
  • You're not checking for any errors along the way. Bad practice... Check out how they do it at https://github.com/ynezz/librs232/blob/master/doc/example.lua and duplicate that. This will tell you if, and where, anything goes wrong. – dda Dec 18 '17 at 06:46
  • Have you tried disable rts ? – Mali Dec 18 '17 at 08:37
  • @dda Well. Thats what I did. I tried to copy the lines out of this document. But I even ran the whole document as it is (And I just edited the portname and the output). The output is still the same! Nothing! There is just no error message Update: I ran the following console command [/code]echo -e "9" > /dev/ttyACM0 [/code] but still, the LED doesnt jump on. But at least the Arduino receives data (The orange LED blinks), what didnt happen when I wrote via Lua! – Okaghana Dec 18 '17 at 10:44
  • @Mali how? and why? – Okaghana Dec 18 '17 at 10:47
  • As mentioned, you're not checking anything. And default io.read() returns whole line including new line character. – KIIV Dec 19 '17 at 18:25
  • @KIV what do you mean by "you're not checking anything". And... Whats so bad about the new line character? – Okaghana Jan 01 '18 at 19:48

0 Answers0