Blog Image

guivi

About the blog

In this blog I will keep track of projects I develop though out this year and may be in the future. For now it is juts a testing ground for developing the blog itself but I hope as I put more material it will become a good place for me to hold information.

Simple Server Socket C++

C/C++ Programming, Linux Posted on 01 Jun, 2020 12:01:41

This is a simple sever written in C++ and the original code can be found in “The definitive guide to Linux network programming” book.

#include <stdio.h>
#include <stdlib.h>
#include <cstring>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <unistd.h>

const char APRESSMESSAGE[] = "APRESS - For Professionals, by Professionals!\n";

int main(int argc, char *argv[])
{
	int simpleSocket = 0;
	int simplePort = 0;
	int returnStatus = 0;
	struct sockaddr_in simpleServer;
	
	// make sure we va a port number
	if (argc != 2){
		fprintf(stderr, "Usage: %s <port>\n", argv[0]);
		exit(1);
	}
	
	// Create streaming socket
	simpleSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	if (simpleSocket == -1){
		fprintf(stderr, "Could not create a socket!\n");
		exit(1);
	}
	else {
		fprintf(stderr, "Socket created!\n");
	}
	// retrieve the port number for listing
	simplePort = atoi(argv[1]);
	
	// Set up address strucuter
	//use INADDR_ANY
	std::memset(&simpleServer, '\0',sizeof(simpleServer));
	simpleServer.sin_family = AF_INET;
	simpleServer.sin_addr.s_addr = htonl(INADDR_ANY);
	simpleServer.sin_port = htons(simplePort);
	
	returnStatus = bind(simpleSocket, (struct sockaddr*)&simpleServer, sizeof(simpleServer));
	
	if (returnStatus == 0){
		fprintf(stderr, "Bind Completed!\n");
	}
	else {
		fprintf(stderr, "Could not bid the address!\n");
		close(simpleSocket);
		exit(1);
	}
	// Listen on the socket for connection
	returnStatus = listen(simpleSocket, 5);
	
	if (returnStatus == -1){
		fprintf(stderr, "Cannot listen to socket\n!");
		close(simpleSocket);
		exit(1);
	}
	
	while(1) {
		struct sockaddr_in clientName = { 0};
		int simpleChildSocket = 0;
		socklen_t clientNameLength = sizeof(clientName);
		
		simpleChildSocket = accept(simpleSocket, (struct sockaddr *)&clientName, &clientNameLength);
		if (simpleChildSocket == -1){
			fprintf(stderr, "Cannot accept connections!\n");
			close(simpleSocket);
			exit(1);
		}
		
		write(simpleChildSocket, APRESSMESSAGE, strlen(APRESSMESSAGE));
		close(simpleChildSocket);
	}
	close(simpleSocket);
	return 0;
}


MCP4725

Linux Posted on 09 Apr, 2019 15:07:55

MCP4725 12-Bit DAC Interface to Raspberry Pi

by Lewis Loflin

YouTube video see MCP4725 12-Bit DAC Interface to Raspberry Pi.

The MCP4725 is a 12-Bit Digital-to-Analog Converter with EEPROM Memory. Here I’ll connect the MCP4725 to Raspberry Pi and illustrate how to program the device. I’ll be Debian based Raspbian Linux.

Fig. 1 MCP4725 breakout board available from a number of vendors. The boards usually have pull up resistors that need to be disconnected or use a level translator which is what I did. The Raspberry Pi I2C buss is 3.3V while I operated mine at 5V.

The features of the MCP4725:

The MCP4725 is a low-power, high accuracy, single channel, 12-bit buffered voltage output Digital-to-Analog Converter (DAC) with non-volatile memory (EEPROM).

Its on-board precision output amplifier allows it to achieve rail-to-rail analog output swing. The DAC input and configuration data can be programmed to the non-volatile memory (EEPROM) by the user using I2C interface command.

The non-volatile memory feature enables the DAC device to hold the DAC input code during power-off time, and the DAC output is available immediately after power-up. This feature is very useful when the DAC device is used as a supporting device for other devices in the network.

The device includes a Power-On-Reset (POR) circuit to ensure reliable power-up and an on-board charge pump for the EEPROM programming voltage. The DAC reference is driven from VDD directly. In power-down mode, the output amplifier can be configured to present a low, medium, or high resistance output load.

Fig 2 MCP4725 internal block diagram.

Looking Closer

The MCP4725 DAC is a 12-bit device with values from 0-4095, with 4095 outputting a voltage near Vcc. First we write a control byte with the following specifications:

(Refer to page 18-19 spec sheet), uint8_t control_byte = 0b01000000; bits 7-5 are 010 write DAC only, 011 write DAC and EEPROM. We can write the 12-bit value to the DAC only or the DAC and the EEPROM. The values stored in the EEPROM sets the power up output voltage. This is byte 0.

If say 2048 is stored in the EEPROM the voltage output when power up is 1/2 Vcc.

The uint8_t is an unsigned 8-bit integer value that’s defined across multiple versions of C.

In the example here I write to the DAC only because there’s a delay writing to the EEPROM, in addition so many write cycles will shorten the life of the EEPROM.

Bits 4-3 are unused while bits 2-1 setup power down (PD) where a selected resistor value is switched from the output to ground. I set this value to open. Bit 0 is also unused.

The next two bytes are also uint8_t variables. The uint16_t val is an unsigned 16-bit integer value where one enters a value from 0-4095. The variable val is shifted four places right and loaded into byte 1 as the 8 MSB of val.

Then val is shifted four places left and loaded into byte 2 where bits 7-4 are the LSB if val while bits 3-0 are unused.

Then all three bytes are sent over I2C to the MCP4725 that outputs a voltage based on the value of val. After the three bytes are written the program exits.

// MCP4725.c
// Program the MCP4725 DAC
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h> // read/write usleep
#include <stdlib.h> // exit function
#include <inttypes.h> // uint8_t, etc
#include <linux/i2c-dev.h> // I2C bus definitions

int fd;
int mcp4725_address = 0x62;
int16_t val;
uint8_t writeBuf[3];
float myfloat;

int main() {

// open device on /dev/i2c-1 the default on Raspberry Pi B
if ((fd = open(“/dev/i2c-1”, O_RDWR)) < 0) {
printf(“Error: Couldn’t open device! %d\n”, fd);
exit (1);
}

// connect to ads1115 as i2c slave
if (ioctl(fd, I2C_SLAVE, mcp4725_address) < 0) {
printf(“Error: Couldn’t find device on address!\n”);
exit (1);
}

// 12-bit device values from 0-4095
// page 18-19 spec sheet
writeBuf[0] = 0b01000000; // control byte
// bits 7-5; 010 write DAC; 011 write DAC and EEPROM
// bits 4-3 unused
// bits 2-1 PD1, PD0 PWR down P19 00 normal.
// bit 0 unused
writeBuf[1] = 0b00000000; // HIGH data
// bits 7-0 D11-D4
writeBuf[2] = 0b00000000; // LOW data
// bits 7-4 D3-D0
// bits 3-0 unused

// input number from 0-4095
// 2048 50% Vcc
char buffer [15];
printf (“Enter a number 0-4095: “);
scanf(“%s”, buffer); // string to
int val = atoi(buffer);
printf(“You entered %d “, val);

// write number to MCP4725 DAC
writeBuf[1] = val >> 4; // MSB 11-4 shift right 4 places
printf(“WriteBuf[1] = %d “, writeBuf[1]);
writeBuf[2] = val << 4; // LSB 3-0 shift left 4 places
printf(“WriteBuf[2] = %d \n”, writeBuf[2]);
if (write(fd, writeBuf, 3) != 3) {
perror(“Write to register 1”);
exit (1);
}
close(fd);
return 0;
}

All the information above has been copied from a webpage which is very informative and I recommend visiting:

http://www.bristolwatch.com/rpi/mcp4725.html



Mounting Partition for Owncloud

Linux Posted on 11 Aug, 2018 08:27:28

To be able to access a partition from own cloud one the proper permissions have to be given.

To list the partitions run the following command:

sudo fdisk -l

A list of all hard drives and partitions will follow. To mount a partition locate the one you want o mount, this should look something like /dev/sdx#. Where x represent a letter and # represent a number.

Then go to /mnt and create a folder.

cd /mnt

mkdif hhd1

This folder needs to be given the right permission and the owner needs to change as follow.

chmod 0750 hdd1/

chown www-data:www-data hdd1/

Now mount can be done with:

umount /dev/sdx# /mnt/hhd1

Remember to change the x and # accordingly. And now you should have access to the hard dive in through the folder /mnt/hhd1.



Serial Port Persistent

Linux Posted on 08 Aug, 2018 09:27:46

Making a serial port persistent in Linux.

When plugging multiple usb serial converters there is no security that the device will maintain the name after reboot or after re plugging. For this purpose there is a way of linking the device information to a file so that this file can be use instead of the traditional /dev/tty… file to connect over serial.

To find the needed information of the device to be able to make this linked file one needs the following:

This list the usb with their manufacturing identifiers.


This list the serial number of the device. With all these information now we can make the usb serial persistent by introducing a new rule in the udev. To do this a file needs to be created in /etc/udev/rules.d/ this file we are going to name it 99-usb-serial.rules
to do this the following command can be used:

sudo nano /etc/udev/rules.d/99-udev-serial.rules

In this file we put the following information:

UBSYSTEM==”tty”, ATTRS{idVendor}==”0403″, ATTRS{idProduct}==”6001″, ATTRS{serial}==”FT0IFMES”, SYMLINK+=”serial1″

This will make the specified device link to “/dev/serial1” file so now we can use this to open the serial port. The device has to be re plugged before it takes effect.



Interesting SOCAT Commands

Linux Posted on 06 Aug, 2018 21:37:39

To be able to make a virtual null modem one can tun the following command:

socat -d -d pty,raw,echo=0 pty,raw,echo=0

This should output the follow:

2018/08/03 21:54:16 socat[32628] N PTY is /dev/pts/3
2018/08/03 21:54:16 socat[32628] N PTY is /dev/pts/4
2018/08/03 21:54:16 socat[32628] N starting data transfer loop with FDs [5,5] and [7,7]

Or similar. Obviously the dates will be different. To be able to open the location the right permission may be needed on execution of the terminal program of choice.

To make a serial port accessible over the network one can use the TCP listener with socat and use the following command:

sudo socat tcp-l:54321,reuseaddr,fork file:/dev/ttyUSB0,
nonblock,raw,echo=0,waitlock=/var/run/ttyUSB0.lock

The serial port may change depending on serial used and the port can also be selected as desired.

Finally anotehr interesting command with the socat is to make a virtual port connect to a network tcp server. For this the following command can be use:

socat pty,link=$HOME/dev/vmodem0,raw,echo=0,waitslave tcp:server.com:54321

Sources:

http://www.dest-unreach.org/socat/doc/socat-ttyovertcp.txt

https://rabinnankhwa.wordpress.com/2013/12/25/creating-a-null-modem-virtual-serial-port-pair-connection-with-socat/