How to use C++ read data in raspberry pi via RS-485 to USB

For more advanced meter data users. Learn from others and ask questions about how to read meters, how to use the EKM Push Data, and how to display the meter data in useful ways.
Post Reply
dexterppp
Posts: 4
Joined: Mon Nov 30, 2015 5:07 am

How to use C++ read data in raspberry pi via RS-485 to USB

Post by dexterppp » Mon Dec 07, 2015 11:47 pm

I've successfully read data from EKM metering by using Python as in http://forum.ekmmetering.com/viewtopic.php?f=4&t=3472 link.

But before that I've tried to use C++ for a long time but it is unsuccessful. I follow this document http://www.chemie.fu-berlin.de/chemnet/ ... tml#SEC237, the below is my code

Code: Select all

#include <string.h>
#include <stdio.h>
#include <termios.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>

#include <iostream>
using namespace std;

int fd;

struct termios tm;

int main() {

	fd = open("//dev//ttyUSB0", O_RDWR | O_NOCTTY | O_SYNC);

	if (fd == -1)
	{
		printf ("Could not open port.");
		return -1;
	}

	fcntl(fd, F_SETFL, 0);

	memset(&tm, 0, sizeof(struct termios));

	if (tcgetattr(fd, &tm) != 0)
	{
		printf ("error from tggetattr");
		return -1;
	}

	/*9600 Baud, 7 Data Bits, Even Parity, 1 Stop Bit, No Flow Control*/
	cfsetispeed(&tm, B9600);
	cfsetospeed(&tm, B9600);

	tm.c_cflag |= (CLOCAL | CREAD);
	tm.c_cflag |= PARENB;
	tm.c_cflag &= ~PARODD;
	tm.c_cflag &= ~CSTOPB;
	tm.c_cflag &= ~CSIZE; /* Mask the character size bits */
	tm.c_cflag |= CS7;    /* Select 7 data bits */

	tm.c_oflag = OPOST;
	tm.c_iflag &= ~IGNBRK;         // disable break processing
	tm.c_iflag &= ~(IXON | IXOFF | IXANY); // shut off xon/xoff ctrl

	tm.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);

	tm.c_cc[VTIME] = 5;
	tm.c_cc[VMIN] = 0;

	if (tcsetattr(fd, TCSANOW, &tm) != 0)
	{
		printf ("error from tcsetattr");
		return -1;
	}

	unsigned char request[] = {0x2f,0x3f,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x31,0x37,0x36,0x36,0x31,0x21,0x0d,0x0a};
	int result = write(fd, request, sizeof request);
	printf("\nSend result [%d]", result);

	unsigned char Respon[255];
	memset(Respon, 0, sizeof Respon);

	result = read(fd, Respon, sizeof Respon);
	printf("\nRead result [%d]", result);

	for (int j=0 ; j < result; j++)
	{
		printf(" %02x ", Respon[j]);
	}

	printf("\n");
	return 0;
}
The result is
pi@raspberrypi:~/solarD/src $ ./solar.out

Send result [17]
Read result [0]

pi@raspberrypi:~/solarD/src $ ./solar.out

Send result [17]
Read result [2] 30 30
But when I change code for canonical mode of POSIX

Code: Select all

tm.c_cc[VTIME] = 5;
tm.c_cc[VMIN] = 255;
There are not thing display because program is waiting data from serial port

And when I dump serial port, it show me that there are something has received as below
pi@raspberrypi:~ $ tail -f /dev/ttyUSB0
00000001766100000000000000000000000000000000000000000000000000000000000000000000000000000002320000000000000000000000000000000000000000000000000000C000C000C0000000000011512080313555702000000000000000000000000000000000000000000000000000000000000!

=00000001766100000000000000000000000000000000000000000000000000000000000000000000000000000002324000000000000000000000000000000000000000000000000000C000C000C0000000000011512080313560302000000000000000000000000000000000000000000000000000000000000!


Anyone can suggest me how can I fix it.

Jameson
Posts: 788
Joined: Fri Nov 04, 2011 7:42 pm
Location: Santa Cruz, CA
Contact:

Re: How to use C++ read data in raspberry pi via RS-485 to USB

Post by Jameson » Tue Dec 08, 2015 9:02 am

It looks like your meter is responding but your software is not interpreting what is coming back.

I would take a look at this post, it includes some examples on github that read our meters using C++ code: http://forum.ekmmetering.com/viewtopic. ... 5&start=30

"C++" must be the hardest keyword to search my notes for :x

Good Luck!
Jameson
EKM METERING
http://www.ekmmetering.com
831.425.7371

dexterppp
Posts: 4
Joined: Mon Nov 30, 2015 5:07 am

Re: How to use C++ read data in raspberry pi via RS-485 to USB

Post by dexterppp » Tue Dec 08, 2015 9:16 pm

Jameson wrote:It looks like your meter is responding but your software is not interpreting what is coming back.

I would take a look at this post, it includes some examples on github that read our meters using C++ code: http://forum.ekmmetering.com/viewtopic. ... 5&start=30

"C++" must be the hardest keyword to search my notes for :x

Good Luck!
Hi Jameson,

Thanks for your reply. I have been read your suggest post but the example in github use QSerialPort that is lib in Qt ( Qt is very easy to use like I did in Python). But I have limit only use C++ and I understand in C++, it use POSIX for read serial port.

Jameson
Posts: 788
Joined: Fri Nov 04, 2011 7:42 pm
Location: Santa Cruz, CA
Contact:

Re: How to use C++ read data in raspberry pi via RS-485 to USB

Post by Jameson » Tue Dec 08, 2015 9:58 pm

The truth is I dont know. I think it is weird that your software is seeing what looks to me like a blank response. But the serial port dump you posted looks like a full meter response. The response is in ASCII and not in Hex so all characters are not printed (see this post for details: http://forum.ekmmetering.com/viewtopic.php?f=4&t=3472 )

But you can still see that in the first read you have 232.0 volts and in the second read you had 232.4 volts. So the meter data is good, your software is just not able to access the serial data or to parse it. This is where you have to focus I would think.

I would take a look at this page as it seems like it has good details about POSIX serial ports: https://www.cmrr.umn.edu/~strupp/serial.html

Maybe you can explain more why you are limited to only C++ and what the limitations of your project are. Hopefully someone out there with even a slight understanding of C++ (more than me) can step in and post here.

Good luck again,
Jameson
EKM METERING
http://www.ekmmetering.com
831.425.7371

Post Reply