serial communication and general issues

This is a discussion on serial communication and general issues within the Windows Programming forums, part of the Platform Specific Boards category; Hello Everyone I'm a newbe to these forums and my apologies if i post a few threads in wrong sections ...

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    55

    serial communication and general issues

    Hello Everyone
    I'm a newbe to these forums and my apologies if i post a few threads in wrong sections for these boards.

    I have learnt c a few months back and have got the basics of it and can write a few complicated programs
    I am used to programming in old compilers (turbo c v3...) and quiet frankly i find them much easier to work with.
    But some(well many features..) don't work with them so i tried my hand with a gcc compiler(code blocks) and it works pretty good too.But then again....they also have some flaws (functions that work in borland dont work in gcc ..isnt there a common compiler for all...?)

    I have searched a few threads on these boards and still haven't got a good enough answer to my queries..
    1.Which is better, a GCC compiler or Borland?
    2.Can someone please give me an alternative for the bios.h header,as it is outdated and none of those functions(bioscom..etc) work on windows(vista,7,8) anymore...
    3.I have gone through msdn,and found a few functions like

    HANDLE,read(),write() but do not know how to implement them.
    If someone could give me an example program (including all the headers required,and on which compiler the code would work),i would really appreciate it)
    Here's the sample program using bios.h,which i want to write using the msdn functions.

    Code:
    //basically i have a usb to db9(rs232) connecter and i short the tx and rx pins...so what data i send,it prints the same thing on the screen
    #include <bios.h>
    #include <conio.h>
    #include <dos.h>
    
    
    #define COM1        14
    #define DATA_READY 0x100
    #define TRUE       1
    #define FALSE      0
    
    #define SETTINGS ( 0x80 | 0x02 | 0x00 | 0x00)
    
    int main(void)
    {
       int in, out, status, DONE = FALSE;
       clrscr();
      bioscom(0, SETTINGS, COM1);
       cprintf("... BIOSCOM [ESC] to exit ...\n");
       while (!DONE)
       {
          status = bioscom(3, 0, COM1);
         if (status & DATA_READY)
        if ((out = bioscom(2, 0, COM1) & 0x7F) != 0)
             putch(out);
         if (kbhit())
         {
            if ((in = getche()) == '\x1B')
               DONE = TRUE;
            bioscom(1, in, COM1);
         }
       }
       return 0;
    }
    I will post a few more of these kinds of questions(mostly serial communication based),so i would request the admins to please keep this thread open for sometime.
    Last edited by ak47; 09-17-2012 at 12:56 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,855
    I find "win32" and whatever it is you're trying to do to be effective search terms in google.
    Eg.
    win32 serial port - Google Search
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    I've found only a couple of places in MSDN that talk about serial communications:
    Communications Resources
    Programming Serial Connections (Windows Embedded CE 6.0)
    They seem to contain all the info you need. You should try to read through it.

    EDIT: Salems idea is better because you can probably find a more readable tutorial elsewhere than MSDN (which is not known for readability).
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  4. #4
    Registered User
    Join Date
    Sep 2012
    Posts
    55
    Yes searching for a few books on win32 serial port communication i was able to get a suitable replacement for the bios.h header
    But here arises another problem.I'm currently using win 7 64 bit.My program runs fine on my pc and other 64 bit pcs.But when i run the same program on a 32 bit system ,i am able to only send data...but not receive.Why so?
    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <windows.h>
    #include <string.h>
    
    
    int main(){
        int i;  //looping variable
        char port[10];
       // printf("Enter the com port.");
       // scanf("%s",&port);
        DCB dcbSerialParams = {0};DWORD dwBytesRead = 0;
        char szBuff[25];                    //variable to send data
        char rzBuff[25] = {0};              //variable to read data
    ///Code for opening port
    HANDLE hSerial;
    hSerial = CreateFile("COM1",                        //enter com port value
    GENERIC_READ | GENERIC_WRITE,
    0,
    0,
    OPEN_EXISTING,
    FILE_ATTRIBUTE_NORMAL,
    0);
    if(hSerial==INVALID_HANDLE_VALUE){
    if(GetLastError()==ERROR_FILE_NOT_FOUND){
    //serial port does not exist. Inform user.
    printf("Serial Port does not exist.\n");
    }
    //some other error occurred. Inform user.
    printf("Some other error occured.\n");
    }
    else{
    ///End of Code for opening port
    
    ///Setting parameters
    //DCB dcbSerialParams = {0};        //declared on top
    dcbSerialParams.DCBlength=sizeof(dcbSerialParams);
    if (!GetCommState(hSerial, &dcbSerialParams)) {
    //error getting state
    printf("error getting state.\n");
    }
    dcbSerialParams.BaudRate=CBR_19200;
    dcbSerialParams.ByteSize=8;
    dcbSerialParams.StopBits=ONESTOPBIT;
    dcbSerialParams.Parity=NOPARITY;
    if(!SetCommState(hSerial, &dcbSerialParams)){
    //error setting serial port state
    printf("error setting serial port state.\n");
    }
    ///End of setting parameters
    
    ///Setting timeouts
    COMMTIMEOUTS timeouts={0};
    timeouts.ReadIntervalTimeout=50;
    timeouts.ReadTotalTimeoutConstant=50;
    timeouts.ReadTotalTimeoutMultiplier=10;
    timeouts.WriteTotalTimeoutConstant=50;
    timeouts.WriteTotalTimeoutMultiplier=10;
    if(!SetCommTimeouts(hSerial, &timeouts)){
    //error occureed. Inform user
    printf("error occured.");
    }
    ///End of Setting timeouts
    for(i=1;i <= 9;i++){
    ///Sending data
    //char szBuff[25] = {0};        Declared on top
    
    //printf("Sent data: %s \n",szBuff);
    printf("Sent data:  \n");
    gets(szBuff);
    //szBuff[strlen(szBuff)]='\n';
    strcat(szBuff,"\n");
    if(!WriteFile(hSerial, szBuff, 25, &dwBytesRead, NULL)){
    //error occurred. Report to user.
    printf("Error occured");
    }
    
    ///End of sending data
    
    ///Reading Data
    //char rzBuff[25] = {0};        Declared on top
    
    
    if(!ReadFile(hSerial, rzBuff, 25, &dwBytesRead, NULL)){
    //error occurred. Report to user.
    printf("Error occured");
    }
    else
    printf("Read data: %s\n\n",rzBuff);
    memset(rzBuff,0,25);
    ///End of Reading data
    }
    CloseHandle(hSerial);
    }
    getch();
    return 0;
    }

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,855
    SourceForge.net: Indentation - cpwiki
    Your indentation is rubbish.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Sep 2012
    Posts
    55
    Quote Originally Posted by Salem View Post
    SourceForge.net: Indentation - cpwiki
    Your indentation is rubbish.
    My apologies.Here is the same program with indentation.

    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <windows.h>
    #include <string.h>
    
    
    int main(){
    int i;  //looping variable
    char port[10];
    // printf("Enter the com port.");
    // scanf("%s",&port);
    DCB dcbSerialParams = {0};
    DWORD dwBytesRead = 0;
    char szBuff[25];                    //variable to send data
    char rzBuff[25] = {0};              //variable to read data
    
    
    //Code for opening port
    HANDLE hSerial;
    hSerial = CreateFile("COM1",GENERIC_READ | GENERIC_WRITE,0,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
        
        if(hSerial==INVALID_HANDLE_VALUE){
            if(GetLastError()==ERROR_FILE_NOT_FOUND){
                printf("Serial Port does not exist.\n");
                }
        printf("Some other error occured.\n");
        }
    //End of Code for opening port
    
    else
    {
    //Setting parameters
        dcbSerialParams.DCBlength=sizeof(dcbSerialParams);
        
        if (!GetCommState(hSerial, &dcbSerialParams)) {
                printf("error getting state.\n");
                }
        dcbSerialParams.BaudRate=CBR_19200;
        dcbSerialParams.ByteSize=8;
        dcbSerialParams.StopBits=ONESTOPBIT;
        dcbSerialParams.Parity=NOPARITY;
        
        if(!SetCommState(hSerial, &dcbSerialParams)){
                printf("error setting serial port state.\n");
            }
    //End of setting parameters
    
    
    //Setting timeouts
        COMMTIMEOUTS timeouts={0};
        timeouts.ReadIntervalTimeout=50;
        timeouts.ReadTotalTimeoutConstant=50;
        timeouts.ReadTotalTimeoutMultiplier=10;
        timeouts.WriteTotalTimeoutConstant=50;
        timeouts.WriteTotalTimeoutMultiplier=10;
            
            if(!SetCommTimeouts(hSerial, &timeouts)){
                //error occureed. Inform user
                printf("error occured.");
                }
                
    //End of Setting timeouts
        for(i=1;i <= 9;i++){
    
    //Sending data
            //printf("Sent data: %s \n",szBuff);
            printf("Sent data:  \n");
            gets(szBuff);
            //szBuff[strlen(szBuff)]='\n';
            strcat(szBuff,"\n");
            
            if(!WriteFile(hSerial, szBuff, 25, &dwBytesRead, NULL)){
                printf("Error occured");
                }
    //End of sending data
    
    
    //Reading Data
            if(!ReadFile(hSerial, rzBuff, 25, &dwBytesRead, NULL)){
                printf("Error occured");
                }
            else
                printf("Read data: %s\n\n",rzBuff);
            memset(rzBuff,0,25);
    //End of Reading data
         }
    CloseHandle(hSerial);
    }
    getch();
    return 0;
    }

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,313
    The indentation still sucks. Here's a more positive example:
    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <windows.h>
    #include <string.h>
    
    int main(){
        int i;  //looping variable
        char port[10];
        // printf("Enter the com port.");
        // scanf("%s",&port);
        DCB dcbSerialParams = {0};
        DWORD dwBytesRead = 0;
        char szBuff[25];                    //variable to send data
        char rzBuff[25] = {0};              //variable to read data
    
        //Code for opening port
        HANDLE hSerial;
        hSerial = CreateFile("COM1",GENERIC_READ | GENERIC_WRITE,0,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
    
        if(hSerial==INVALID_HANDLE_VALUE){
            if(GetLastError()==ERROR_FILE_NOT_FOUND){
                printf("Serial Port does not exist.\n");
            }
            printf("Some other error occured.\n");
        }
        //End of Code for opening port
        else
        {
            //Setting parameters
            dcbSerialParams.DCBlength=sizeof(dcbSerialParams);
    
            if (!GetCommState(hSerial, &dcbSerialParams)) {
                printf("error getting state.\n");
            }
            dcbSerialParams.BaudRate=CBR_19200;
            dcbSerialParams.ByteSize=8;
            dcbSerialParams.StopBits=ONESTOPBIT;
            dcbSerialParams.Parity=NOPARITY;
    
            if(!SetCommState(hSerial, &dcbSerialParams)){
                printf("error setting serial port state.\n");
            }
            //End of setting parameters
    
            //Setting timeouts
            COMMTIMEOUTS timeouts={0};
            timeouts.ReadIntervalTimeout=50;
            timeouts.ReadTotalTimeoutConstant=50;
            timeouts.ReadTotalTimeoutMultiplier=10;
            timeouts.WriteTotalTimeoutConstant=50;
            timeouts.WriteTotalTimeoutMultiplier=10;
    
            if(!SetCommTimeouts(hSerial, &timeouts)){
                //error occureed. Inform user
                printf("error occured.");
            }
    
            //End of Setting timeouts
            for(i=1;i <= 9;i++){
                //Sending data
                //printf("Sent data: %s \n",szBuff);
                printf("Sent data:  \n");
                gets(szBuff);
                //szBuff[strlen(szBuff)]='\n';
                strcat(szBuff,"\n");
    
                if(!WriteFile(hSerial, szBuff, 25, &dwBytesRead, NULL)){
                    printf("Error occured");
                }
                //End of sending data
    
                //Reading Data
                if(!ReadFile(hSerial, rzBuff, 25, &dwBytesRead, NULL)){
                    printf("Error occured");
                }
                else
                    printf("Read data: %s\n\n",rzBuff);
                memset(rzBuff,0,25);
                //End of Reading data
            }
            CloseHandle(hSerial);
        }
        getch();
        return 0;
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,855
    So when it's not "receiving", are you getting error messages?

    > gets(szBuff);
    SourceForge.net: Gets - cpwiki

    > if(!WriteFile(hSerial, szBuff, 25, &dwBytesRead, NULL))
    Replace 25 with the length of the actual string you're sending.
    Otherwise, you're flooding your device with garbage.

    > if(!ReadFile(hSerial, rzBuff, 25, &dwBytesRead, NULL))
    If you read 25 bytes, then you won't have a \0 at the end of it to make it a proper string you can print with printf.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Registered User
    Join Date
    Sep 2012
    Posts
    55
    Quote Originally Posted by Salem View Post
    So when it's not "receiving", are you getting error messages?
    No,i am not getting any error.
    The program works fine on a windows 7 64 bit system,its just not showing the "Read Data" on a windows 7 32 bit system.
    I'm not able to figure out whats wrong?
    Quote Originally Posted by Salem View Post
    The thought of using scanf had crossed my mind,but i had rarely used gets before so i though I'd give it a try.Now i came to know its not encouraged to used gets anymore!

    Thanks for all your helpSalem and others who posted in here

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,855
    Well a couple of things.

    1. printf("Error occured");
    Add a \n to the end of the string, or call fflush(stdout)

    2. GetLastError function
    Call this to find out WHY it failed.

    Just saying "error" is about as useful as the usual "it doesn't work" posts that frequent forums.
    Get some concrete information we can use.

    3. On the off-chance it is receiving garbage that printf can't print properly, try something like
    Code:
    for ( i = 0 ; i < dwBytesRead ; i++ ) {
        printf("Byte %d = %d\n", i, rzBuff[i] );
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Serial Communication in C++
    By NewGuy100 in forum C++ Programming
    Replies: 8
    Last Post: 04-24-2006, 02:56 PM
  2. Serial Communication
    By Korn1699 in forum C# Programming
    Replies: 0
    Last Post: 11-29-2005, 12:50 PM
  3. C Serial Communication BCD
    By ZoomCities in forum C Programming
    Replies: 1
    Last Post: 10-13-2005, 08:00 PM
  4. Serial Communication Help
    By NewGuy100 in forum C Programming
    Replies: 4
    Last Post: 07-28-2005, 10:15 AM
  5. serial communication
    By hick.hack in forum C++ Programming
    Replies: 1
    Last Post: 02-13-2003, 12:18 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21