About Me

My photo
India
Hey there, lovely people! I'm Hemant Menaria, and I'm passionate about programming. Having completed my MCA in 2011, I've delved into the world of coding with fervor. I believe in sharing knowledge, making complex concepts easy to grasp for everyone. JAVA, PHP, and ANDROID hold a special place in my heart, and I spend most of my time immersed in them. Currently, I'm deeply engaged in API/Webservice frameworks and crafting Hybrid mobile applications to enhance flexibility in the digital realm. If you ever find yourself stuck with a programming challenge, feel free to reach out to me at +91-8955499900 or drop me a line at hemantmenaria008@gmail.com. I'm always eager to help fellow enthusiasts navigate the intricacies of coding!

Monday, November 21, 2011

RS232 Serial Communication


I am designing a program in Xcode on my Mac using C, the program needs to receive power usage data from a wireless node and then insert the data into my database. The connection is using RS232 serial communication. Every 5 seconds a node sends 2 bytes, which my program must check and combine to extract the power usage data. The bytes are organized as follows:

Byte: X N P _ _ _ _ _
Bit #:7 6 5 4 3 2 1 0


X - not used
N - identifies 1 of 2 nodes
P - identifies upper or lower byte, 1 = upper and 0 = lower
The remaining bits are combined to get the power usage data.


Power Usage = bits 0 through 4 of upper combined with bits 0 through 4 of lower.


I am receiving the bytes, but my code is not converting the power usage data correctly. I am working with an Engineering team, who have designed and built the nodes. They are developing everything in windows. They have a similar program for testing purposes written in C#. Their communications work fine, and I modeled my byte conversion after theirs. (Not syntax, but the bitwise operations) However, my program only outputs 0.0 for power usage data.
Code:



uint8 chout; // serial input data
uint8 nodeBuff[2]; // array to hold node byte data
nodeBuff[0] = 0x00;
nodeBuff[1] = 0x00;
int mainfd = 0; // File descriptor
int nodeNum; // keeps track of which node data refers to
int usageData; // holds raw power usage data
double powerData; // holds power usage data to be sent to database
struct termios options;
mainfd = open_port(); // Get file descriptor for port
fcntl(mainfd, F_SETFL, FNDELAY); // Configure port reading
tcgetattr(mainfd, &options); // Get the current options for the port  
// Set the baud rates to 9600
cfsetispeed(&options, B9600);
cfsetospeed(&options, B9600);  
// Enable the receiver and set local mode
options.c_cflag |= (CLOCAL | CREAD);
options.c_cflag &= ~PARENB; // Mask the character size to 8 bits, no parity
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |=  CS8; // Select 8 data bits
options.c_cflag &= ~CRTSCTS; // Disable hardware flow control
   
// Enable data to be processed as raw input
options.c_lflag &= ~(ICANON | ECHO | ISIG);
   
// Set the new options for the port
tcsetattr(mainfd, TCSANOW, &options);
//Serial Comm Connection Initialization
while (1){      
    read(mainfd, &chout, sizeof(chout)); // Read character from ABU      
    if (chout != 0){          
        // checks for upper byte flag
        if (((chout & 0x20) == 0x20)) {
            nodeBuff[0] = chout;
        }
           
        // else it is the lower byte
        else{
            nodeBuff[1] = chout;              
            // combine the upper and lower power usage data
            usageData = (nodeBuff[0] & 0x1f) << 5; // upper half
            usageData = usageData + (nodeBuff[1] & 0x1f); // lower half
            powerData = usageData * 1.83255;              
            // check for node number
            if ((nodeBuff[0] & 0x40) == 0x40) {
                nodeNum = 1; // data is for node 1
                                // add data to database
            }
            else {
                nodeNum = 0; // data is for node 0            
                                // add data to database
            }
        }
    }      
    chout = 0; // reset serial input data variable
    usleep(10); // pause for 10 miliseconds  
}

Download: Click Here

Sunday, November 20, 2011

A TCP port scanner in 'C'


This is a simple port scanner coded in c. It uses a simple socket and a for loop. The port scanner uses TCP Connect to check whether the port is opened or closed.

This is for beginners who are trying to grasp simple sockets in C.

By the way this is for linux platform you can easily compile this on win32 using cygwin.


/* A TCP port scanner created by billy www.softhardware.co.uk*/

#include < stdio.h >
#include < sys/types.h >
#include < sys/socket.h >
#include < netinet/in.h >
#include < netdb.h >
#include < stdlib.h >
#include < errno.h >

/* Main programs starts*/
int main(int argc, char **argv)
{
   int   sd;         //socket descriptor
   int    port;         //port number
   int   start;         //start port
   int    end;         //end port
   int    rval;         //socket descriptor for connect  
   char    responce[1024];      //to receive data
   char   *message="shell";       //data to send
   struct hostent *hostaddr;   //To be used for IPaddress
   struct sockaddr_in servaddr;   //socket structure  
   if (argc < 4 )
   {
      printf("------Created By www.Softhardware.co.uk-----------\n");
      printf("--------------------------------------------------\n");
      printf("Usage: ./tscan   \n");
      printf("--------------------------------------------------\n");
      return (EINVAL);
   }
   start = atoi(argv[2]);
   end   = atoi(argv[3]);
   for (port=start; port<=end; port++)
   {         //portno is ascii to int second argument    
   sd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); //created the tcp socket
   if (sd == -1)
   {
     perror("Socket()\n");
     return (errno);
   }  
   memset( &servaddr, 0, sizeof(servaddr));
   servaddr.sin_family = AF_INET;
   servaddr.sin_port = htons(port); //set the portno  
   hostaddr = gethostbyname( argv[1] ); //get the ip 1st argument  
   memcpy(&servaddr.sin_addr, hostaddr->h_addr, hostaddr->h_length);    
   //below connects to the specified ip in hostaddr
   rval = connect(sd, (struct sockaddr *) &servaddr, sizeof(servaddr));
   if (rval == -1)
   {
   printf("Port %d is closed\n", port);
   close(sd);
   }
   else
   printf("Port %d is open\n",port);  
   close(sd);         //socket descriptor
   }  
}


Download:  Click Here