Archive

Archive for September, 2014

Using Raspberry Pi Serial Port

September 12th, 2014 No comments
             By default the Raspberry Pi’s serial port is configured to be used for console input/output, it means you can’t use the Serial Port in your programs and  needs to be disabled.To enable the serial port for your own use you need to disable login on the port. There are two files that need to be edited

The first and main one is     

              

 /etc/inittab

This file has the command to enable the login prompt and this needs to be disabled. Edit the file and move to the end of the file. You will see a line similar to

                      
      

T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100

Disable it by adding a # character to the beginning. Save the file.


          When the Raspberry Pi boots up, all the bootup information is sent to the serial port. Disabling this bootup information is optional and you may want to leave this enabled as it is sometimes useful to see what is happening at bootup. If you have a device connected (i.e. Arduino) at bootup, it will receive this information over the serial port, so it is up to you to decide whether this is a problem or not.
You can disable it by editing the file

                             

/boot/cmdline.txt

The contents of the file look like this

dwc_otg.lpm_enable=0 console=ttyAMA0,115200 kgdboc=ttyAMA0,115200 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline rootwait

Remove all references to ttyAMA0 (which is the name of the serial port). The file will now look like this

dwc_otg.lpm_enable=0 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline rootwait

In order you enable the changes you have made, you will need to reboot the Raspberry Pi

                         

sudo shutdown -r now

Algotithm to Convert 4 byte Hex to 32 bit float

September 10th, 2014 No comments
 I really struggled hard to find out some sorts of algorithm to convert 4 hex bytes to 32 bit float for my requirement on some analysis of MODBUS protocol. I didn’t find it and it tok me nearly 2 days to design this. Here is the algorithm..

float ModbusRead(byte id,byte msbAddress,byte lsbAddress,byte msbCrc,byte lsbCrc)
{
num=1.00;
Serial.write(id);
Serial.write(0x03);
Serial.write((byte)msbAddress);
Serial.write((byte)lsbAddress);
Serial.write((byte)0x00);
Serial.write(0x02);
Serial.write(msbCrc);
Serial.write(lsbCrc);
delay(1000);

for(i=0;i<9;i++)
{
if(!Serial.available())
{
for (int x=0;x < 9;x++)
{
reply[x]=0x00;
}
while(Serial.available()){}
break;
}
reply[i]=Serial.read();
}
while(Serial.available()){}
if((reply[3] & 0x80)!=0)
num=num*-1;
reply[3]=reply[3]<<1;
if((reply[4] & 0x80)!=0x00)
reply[3]=reply[3] | 0x01;
num=num*pow(2,reply[3]-0x7F);
i=0;
delay(1000);
return num*calculatemantisa(reply[4],reply[5],reply[6]);
}
float calculatemantisa(byte a,byte b,byte c)
{
float x=1.0000;
if((a & 0x40)!=0x00)
x=x+pow(2,-1);
if((a & 0x20)!=0x00)
x=x+pow(2,-2);
if((a & 0x10)!=0x00)
x=x+pow(2,-3);
if((a & 0x8)!=0x00)
x=x+pow(2,-4);
if((a & 0x4)!=0x00)
x=x+pow(2,-5);
if((a & 0x2)!=0x00)
x=x+pow(2,-6);
if((a & 0x1)!=0x00)
x=x+pow(2,-7);

if((b & 0x80)!=0x00)
x=x+pow(2,-8);
if((b & 0x40)!=0x00)
x=x+pow(2,-9);
if((b & 0x20)!=0x00)
x=x+pow(2,-10);
if((b & 0x10)!=0x00)
x=x+pow(2,-11);
if((b & 0x8)!=0x00)
x=x+pow(2,-12);
if((b & 0x4)!=0x00)
x=x+pow(2,-13);
if((b & 0x2)!=0x00)
x=x+pow(2,-14);
if((b & 0x1)!=0x00)
x=x+pow(2,-15);

if((c & 0x80)!=0x00)
x=x+pow(2,-16);
if((c & 0x40)!=0x00)
x=x+pow(2,-17);
if((c & 0x20)!=0x00)
x=x+pow(2,-18);
if((c & 0x10)!=0x00)
x=x+pow(2,-19);
if((c & 0x8)!=0x00)
x=x+pow(2,-20);
if((c & 0x4)!=0x00)
x=x+pow(2,-21);
if((c & 0x2)!=0x00)
x=x+pow(2,-22);
if((c & 0x1)!=0x00)
x=x+pow(2,-23);

return x;
}
Categories: 32 bit float, exponent, mantisa, MODBUS, RS-232, RS-485 Tags: