arduino - SoftwareSerial example not working as expected -
i bought arduino uno read data outputted smart meter. meter uses serial communication , see values being outputted on laptop screen. figured need use softwareserial library read incoming data , print data on screen using hardware serial , serial monitor in arduino ide. become familiar (software) serial communication on arduino, reviewed documentation of softwareserial library. problem is, can't basic example work , have been stuck on quite while now. example code below, example can found here
#include <softwareserial.h> softwareserial myserial(10, 11); // rx, tx void setup() { // open serial communications , wait port open: serial.begin(57600); while (!serial) { ; // wait serial port connect. needed native usb port } serial.println("goodnight moon!"); // set data rate softwareserial port myserial.begin(4800); myserial.println("hello, world?"); } void loop() { // run on , on if (myserial.available()) { serial.write(myserial.read()); } if (serial.available()) { myserial.write(serial.read()); } } as far understand this, following should happen: - type text in serial monitor window. - serial.read() reads data , writes software serial. - software serial reads data , writes serial. - written appears on screen.
but whatever try, nothing happens. among things tried following: - change baud rate both software , hardware serial (9600 instance). - tried different softwareserial library (altsoftserial). - tried different rx , tx pins softwareserial. - instead of serial.write(myserial.read());, store result in char first.
i'm missing obvious. grateful shed light on or offer alternative way me read data smart meter.
edit
i had no wiring, because example specified "there no circuit example". tried 3 options suggested @slash-dev, none had expected behaviour:
softwareserial wires connecting pin 1 pin 10 , pin 0 pin 11. prints strange characters:
goodnight moon! Ùniÿhtÿmoÿn!ÿ nihtmoÿttt altsoftserial wires connecting 1-8 , 0-9. first prints goodnight moon! , keeps printing Ô당¥�¡Ñ�moon!.
neoswserial wires connecting 1-10 , 0-11. same altsoftserial keeps printing Ôë‹–+ë¡Ñ�j½½¹…j.
the baud rates must same on serial , myserial.
and don't describe connections, have ask: did connect wire pin 1 (serial transmit) pin 10 (myserial receive), , wire pin 0 (serial receive) pin 11 (myserial transmit)? note how crossed.
altsoftserial best choice, works on pin 8 (rx) , pin 9 (tx), require connecting 8 1 , 9 0. softwareserial inefficient, because disables interrupts long periods of time. can interfere other parts of sketch or other libraries.
my neoswserial library alternative. it's efficient altsoftserial, works on 2 pins. can transmit , receive @ same time (unlike softwareserial), works @ bauds 9600, 19200 , 38400.
edit:
i think you've tried working ok. software serial libraries use interrupts processing individual bits instead of 1 interrupt per character. when there other interrupts in system (millis() timer0 or serial), bit "calculations" can affected. manifests receiving wrong byte. loopback test makes susceptible because sending , receiving synchronized (the initial receive interrupt occurs while transmit interrupt starting next char).
if hook 0 1, think work, because uart able send , receive @ same time, , deals complete characters, not bits. character interrupts not disturb sending or receiving of bits.
in developing neoswserial library, have seen manifest same way. had use 2 arduinos test asynchronously (i.e., not synchronized). in case, using altsoftserial smartmeter should work fine, , can choose different baud rates. if echoing smartmeter characters serial, sure have higher baud rate on serial.
Comments
Post a Comment