0

Say some electronic device (TV OR Computer) has an inbuilt program. Depending on received data from Ardiuno, it displays some text based on requirement. For example, if it receives 1 it should display 'error', else if it receives 0 it should display 'ready'.

I am working on only sending signals/bytes from Arduino Uno to some device which already has an inbuilt program. I just want to send 0 or 1 byte from the Ardiuno to the device using TTL to RS 232 COVERTER. this is the code

#include<SoftwareSerial.h>

 #define rxPin 0 //make pin o as rx pin 

 #define txPin 1//make pin 1 as tx pin

 SoftwareSerial mySerial(rxPin ,txPin);//i donot undersatnd this line (i got 
 the code from net//

 void setup() 

 {

     pinMode(rxPin,INPUT);//making rx as input//
     pinMode(txPin,OUTPUT);//making tx as output//
     Serial.begin(9600);
     mySerial.begin(9600);
}

void loop() {
// put your main code here, to run repeatedly:
Serial.print(0x00);//i want to send 0 byte data from ardinuo to device //
delay(200);
}

I am using RealTerm software to check if the signals I have sent from the Arduino Uno are received as they have to be (without any interference). Serial monitor output RealTerm software output

I am sending continuously 0 from ardinuo to realterm in this case(for testing purpose). I am receiving in RealTerm in Hexadecimal format. Since I'm sending 0s continuously, I should be getting a continuous output of 0s on RealTerm as well. Instead, I am getting some random values initially (while uploading the sketch) and RealTerm stops displaying output soon after.

Can someone please help me solve my issue.

Update 1: i have modified my code as per @chrisl's suggestion. Now, whn i send 0 i am getting a correct ouput ,but whn i am sending 1 contineosly i am getting many 0's and random values

#include<SoftwareSerial.h>
#define rxPin 2 
#define txPin 3
SoftwareSerial mySerial(rxPin ,txPin);
void setup() 
 {
 pinMode(rxPin,INPUT);
  pinMode(txPin,OUTPUT);
  Serial.begin(9600);
  mySerial.begin(9600);
  }

  void loop() {
     // put your main code here, to run repeatedly:
  Serial.print(0x01);//i want to send 1 byte data from ardinuo to 
  device(realterm)
  delay(200);
   }

output whn sending 1 from ardinuo

  • RealTerm seems to display the hexadecimal values. Since you send the ASCII code for "0" (which is 30) (by using print() instead of write()) you should see a repeating 30 there. Don't assume zeros, when you are sending ASCII. Though this doesn't explain the seemingly random values you get. – chrisl Jun 26 '18 at 8:49
  • 1
    You have configured pins 0 and 1 of the Uno for software serial, which makes no sense since those pins have hardware serial. Then you don't even use software serial in your code. You need to take the time to actually understand what the code does instead of just randomly copy pasting and hoping it might work. There is a ton of information available to you, use it! – per1234 Jun 26 '18 at 8:55
  • use Serial.write(0), not Serial.print(0) if you want to send value 0 and not the character '0' – Juraj Jun 26 '18 at 10:58
1

You have 2 problems:

  1. You use the Serial.print() function, which outputs ASCII encoded characters. When sending "0" this way, actually the value 48 (or 30 in hex) will be send. RealTerm seems to display hexadecimal values, so you would get a long series of "30"'s. This is not really a big problem, you only shouldn't expect zeros to display there with this code. You can use the Serial.write() function to send direct byte values instead of ASCII codes.
  2. You initiate the SoftwareSerial on the same pins, that are reserved for the Hardware Serial. This cannot work properly, because both libraries will try to use the same pins. Choose other pins for Rx and Tx of the SoftwareSerial interface.

EDIT:

Also you are setting pin modes for Rx and Tx of the SoftwareSerial. I don't know, if this can cause a problem, but at least you don't have to do this. The library will handle setting the pins correctly.

EDIT2: Further explanation about binary, hexadecimal and ASCII representation of the Serial byte stream

There are different methods to display a stream of byte data (like the stream from your Serial interface). Let's look at the decimal value (the one you are using when invoking arithmetic operations like +, -) 48. These are the different representation of this number in different numeral system (The first two characters mark the numeral system; 0b for binary, 0x for hexadecimal):

  • binary: 0b00110000
  • hexadecimal: 0x30

There are more numeral systems, but these are the most important ones. But the byte stream can also be used to look like a sequence of characters. For this the ASCII standard was born long time ago. In simple ASCII every value from 0 to 127 got assigned to a character or control action. The terminal program reads the byte stream and shows the corresponding characters according to the ASCII table. There are expansions to this model, to display more possible characters, but this is the very basic principle. With it, you are able to actually send human readable text from your Arduino to another device.

The Serial.print() function is made for this human readable text, so it will send an ASCII representation of the input parameter. The ASCII value of the character "0" is 48, which corresponds to a hexadecimal value of 30.

Now I'm not sure, if your terminal program shows hexadecimal or ASCII values. I though of the first, because all characters in your first image are digits or characters from A to F, which is the range of hexadecimal values. It didn't seem likely to not get other characters, when you have ASCII encoded text with random values in it. But I'm currently not sure, if the Serial.print() function would automatically print a hexadecimal representation in form of ASCII text, if given a hexadecimal value (I don't think so, but it might be possible). Please try to use the Serial.write() function and try sending different value while watching the output of RealTerm.

And please search for a settings, that determines, how the received byte stream is interpreted. This would make clear, what RealTerm is doing here. Also make sure you understand the relation between binary value and ASCII character. If this is still not clear enough, you can google for in detail explanations. There are tons of tutorials on that.

  • i have updated the question please check – jalath Jun 26 '18 at 9:45
  • as your suggestion i followed step 2 so ii am now able to get continuously output ,i can't understand 1 st part of your answer – jalath Jun 26 '18 at 9:54
  • @jalath I added further explanations. – chrisl Jun 26 '18 at 17:23
  • According to basic serial communication principles, the transmitter (Tx) of the Arduino must be connected to the receiver (Rx) of the device. However, I am getting a correct output when I connect the transmitter of the Arduino to the transmitter pin of the device. Why is this so? – jalath Jun 27 '18 at 4:43
  • Perhaps the pin information on your device is wrong. You didn't specify, what you have there. Or the TTL RS232 converter might already twist the two lines. From the given information it is impossible to say – chrisl Jun 27 '18 at 4:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.