How to write hexadecimal values to serial port 1 in electron?

i printed values using Serial1.write(78); on docklight its printed as 2C

anybody can help me plz

That should output 4E, I don’t know where 2C comes from, are you sure you aren’t seeing the output from some other function writing to Serial1?

@viraj, 78 is not hexidecimal, it is decimal. Using 0x78 is the correct hex representation. Make sure docklight is set to 8N1 and the correct baudrate. Perhaps sharing your code would help.

@peekay123 :

SYSTEM_MODE(MANUAL);

#include "Serial5/Serial5.h"

const char request[] ={0x01,0x03,0x0F,0x3E,0x00,0x02,0xA6,0xD3};

void setup()
{
  Serial.begin(9600);     // to pc hyperterminal
  Serial1.begin(9600);    // to meter
  Serial5.begin(9600);

}

void loop()
{ 
  Serial.write(0x78);    
  Serial1.write(0x78);
  Serial5.write(0x78);

}

here is my code

@Viscacha, you may want to put a delay(100) in your loop() to slow things. Are you seeing the correct value on Serial?

the values through usb serial port are correctly printing “78” (first line in loop() )
but on other serial ports 1 and 5 printed as “08” for above code

does anybody has solution for above issue?

hex 0x78 is ASC "x", so if your getting "78" on the serial port something is wrong.

@viraj, reduce the code to simply writing to Serial1. How are you monitoring the Serial1 port data?

You can also try this, which will send any text on Serial (via terminal) to Serial1.

void setup()
{
  Serial.begin(9600);     // to pc hyperterminal
  Serial1.begin(9600);    // to meter
}

void loop()
{ 
  if (Serial.available()) {
    char c = Serial.read();
    Serial1.write(c);
  }    
}

i tried the above code but on docklight it shows output for 02 as 3F

also tried with arduino board same output as of particle electron

e.g: for Serial.write(0x78); output is 08 on docklight

Sounds like a problem with docklight.

any other apps to view serial port data

@viraj, docklight is designed for RS232 but the Serial1 and Serial5 ports of the Electron are TTL. How, exactly, are you hooking up your docklight? Perhaps a picture would be good. Another approach is to use a USB-to-TTL (FTDI) converter like this one:

Yo can get cheaper ones if you look around. You can use these with a serial port application like Putty to display the serial data.

i am using Prolific USB to RS232 cable to connect electron serial port to pc (docklight software)

on usb serial port of electron data gets printed correctly but on other ports such as 1,4 & 5 its wrong

and i tried sending data from electron to arduino uno board (on serial port) and displayed data on arduino serial monitor its working correctly

@viraj, again, you are connecting an RS232 device, designed to work at +/-12v directly to the serial lines of the Electron, which work at 0-3.3v!!! You may have damaged the Electron serial lines. An Arduino is designed to work a 0-5v. I am assuming you connected a common ground between the Electron and the Arduino when you tested.

I suggest you do a simple test of Serial1 and Serial5 by connecting each port’s RX pin to it’s TX pin to create a loopback. Then you can test if the ports are working with:

void setup()
{
  Serial.begin(9600);     // to pc hyperterminal
  Serial1.begin(9600);    // to meter
  Serial5.begin(9600);    // to meter
}

void loop()
{
  char c;

  if (Serial.available()) {
    c = Serial.read();
    Serial1.write(c);
    Serial5.write(c);
  }
  if (Serial1.available()) {
    c = Serial1.read();
    Serial.printf("S1: %x\n\r",c);
  }
  if (Serial5.available()) {
    c = Serial5.read();
    Serial.printf("S5: %x\n\r",c);
  }
}

With a terminal program connected to Serial (USB virtual port) and pressing a key on the keyboard, you should see the same character being printed in HEX format for both Serial1 and Serial5.

2 Likes