Thread: RS232 connection set up

  1. #16
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Yeah, I'm pretty sure he's in the "has to" boat. As a student, if your teacher says "do it this way", you kinda need to "do it that way" or risk failing the assignment. He hasn't said anything about which OS he's using here either, and what he's doing here is OS/compiler specific. So if they tell him to use bios.h, as I said earlier, they've probably already told him what compiler he's supposed to use.

    It doesn't do him any good to program it in Win32 if his teacher isn't compiling for Windows, now does it?

    Quzah.
    Hope is the first step on the road to disappointment.

  2. #17
    Registered User jephthah's Avatar
    Join Date
    May 2010
    Location
    seattle
    Posts
    49
    surely his class isnt being taught on Windows 95 machines.... so he can make a distribution kit to distribute his non-insane choice of library.

    yeah, see. a distribution kit. it will be cool, he can burn it to a CD Rom and print lables and everything.

    and speaking of going insane, um .... i think i need to check out of this thread.

    yeah.

  3. #18
    Registered User
    Join Date
    May 2010
    Posts
    7
    I am using windows vista and using codeblocks to compile the program. I found the header files on WATCOM (my lecturer advised me on this) and transfared them to codeblocks and that seems to have worked.

    It is now getting past #include <bios.h>

    however i have now run into a different problem :@ I am trying to use the command bioscom as shown in this link : Serial Communication via RS-232: bioscom example -ElectroSofts.com but it wont work!! when I complie it just says 'undefined referance to _bioscom'. this is what is in the bios.h file:

    #ifndef _BIOS_H_INCLUDED
    #define _BIOS_H_INCLUDED

    #ifndef _ENABLE_AUTODEPEND
    #pragma read_only_file;
    #endif

    #ifndef _COMDEF_H_INCLUDED
    #include <_comdef.h>
    #endif

    #ifdef __cplusplus
    extern "C" {
    #endif

    #ifdef _M_IX86
    #pragma pack( __push, 1 )
    #else
    #pragma pack( __push, 8 )
    #endif

    struct _ibm_diskinfo_t { /* disk parameters */
    unsigned drive; /* drive number */
    unsigned head; /* head number */
    unsigned track; /* track number */
    unsigned sector; /* sector number */
    unsigned nsectors; /* number of sectors to read/write/compare */
    void _WCFAR *buffer; /* buffer to read to,write from, or compare */
    };
    #define _diskinfo_t _ibm_diskinfo

    #if !defined(NO_EXT_KEYS) /* extensions enabled */
    #define diskinfo_t _ibm_diskinfo_t
    #endif /* extensions enabled */

    /* constants for BIOS disk access functions */
    #define _DISK_RESET 0
    #define _DISK_STATUS 1
    #define _DISK_READ 2
    #define _DISK_WRITE 3
    #define _DISK_VERIFY 4
    #define _DISK_FORMAT 5

    /* constants for BIOS serial communications (RS-232) support */

    /* serial port services */

    #define _COM_INIT 0 /* init serial port */
    #define _COM_SEND 1 /* send character */
    #define _COM_RECEIVE 2 /* receive character */
    #define _COM_STATUS 3 /* get serial port status */

    /* serial port initializers. One and only one constant from each of the
    * following four groups - character size, stop bit, parity, and baud rate -
    * must be specified in the initialization byte.
    */

    /* character size initializers */

    #define _COM_CHR7 2 /* 7 bits characters */
    #define _COM_CHR8 3 /* 8 bits characters */

    /* stop bit values - on or off */

    #define _COM_STOP1 0 /* 1 stop bit */
    #define _COM_STOP2 4 /* 2 stop bits */

    /* parity initializers */

    #define _COM_NOPARITY 0 /* no parity */
    #define _COM_ODDPARITY 8 /* odd parity */
    #define _COM_SPACEPARITY 16 /* space parity */
    #define _COM_EVENPARITY 24 /* even parity */

    /* baud rate initializers */

    #define _COM_110 0 /* 110 baud */
    #define _COM_150 32 /* 150 baud */
    #define _COM_300 64 /* 300 baud */
    #define _COM_600 96 /* 600 baud */
    #define _COM_1200 128 /* 1200 baud */
    #define _COM_2400 160 /* 2400 baud */
    #define _COM_4800 192 /* 4800 baud */
    #define _COM_9600 224 /* 9600 baud */

    /* keyboard services */

    #define _KEYBRD_READ 0 /* read next character from keyboard */
    #define _KEYBRD_READY 1 /* check for keystroke */
    #define _KEYBRD_SHIFTSTATUS 2 /* get current shift key status */

    /* services for enhanced keyboards */

    #define _NKEYBRD_READ 0x10 /* read next character from keyboard */
    #define _NKEYBRD_READY 0x11 /* check for keystroke */
    #define _NKEYBRD_SHIFTSTATUS 0x12 /* get current shift key status */

    /* printer services */

    #define _PRINTER_WRITE 0 /* write character to printer */
    #define _PRINTER_INIT 1 /* initialize printer */
    #define _PRINTER_STATUS 2 /* get printer status */

    /* time-of-day services */

    #define _TIME_GETCLOCK 0 /* get current clock count */
    #define _TIME_SETCLOCK 1 /* set current clock count */


    _WCRTLINK extern unsigned short _bios_disk(unsigned __cmd,
    struct diskinfo_t *__diskinfo);
    _WCRTLINK extern unsigned short _bios_equiplist(void);
    _WCRTLINK extern unsigned short _bios_keybrd(unsigned __cmd);
    _WCRTLINK extern unsigned short _bios_memsize(void);
    _WCRTLINK extern unsigned short _bios_printer(unsigned __cmd,unsigned __port,
    unsigned __data);
    _WCRTLINK extern unsigned short _bios_serialcom(unsigned __cmd,unsigned __port,
    unsigned __data);
    _WCRTLINK extern unsigned short _bios_timeofday(unsigned __cmd,long *__timeval);

    #pragma pack( __pop )

    #ifdef __cplusplus
    } /* extern "C" */
    #endif

    #endif

    any ideas?

    PS: thanks for all your suggestions...i am fairly new to programming so I dont really know about different programs out there

  4. #19
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Codeblocks is just a "front end", interface. It is not a compiler. Ming, Watcom, Turbo C, etc., are compilers. Find out what your compiler name, really is.

    I told you, you can't just take headers from one compiler, and put them into another, and always get it to work.

    Is your teacher using Vista? Will your program be tested in a Vista OS? Does he say that this combination of files and compiler will work in Vista?

    Naturally, you should be using the same OS as the one that your program will be tested on, and ideally, you'd also use the same compiler, to eliminate any variables.

    What OS are you sending the data to, or how are you testing the output?

    The program in your link, is right out of the Turbo C help file, verbatim. I have compiled it for you, using Turbo C, and put it up here at Swoopshare:

    http://en.swoopshare.com/file/d50aca...IOSCOM&lang=en

    The big questions are, does it work on the teacher's testing system? And does it work on your computer?

    If so, then d/l Turbo C, and use it for this assignment. Then just return to Codeblocks.

    Turbo C is quick to d/l and quick to install. It doesn't put anything in your Windows Registry, because it doesn't know anything about Windows (get ver. 1.01 Turbo C/C++), and you'll be guaranteed to be able to compile it this program and run it.

    P.S.
    These header files are filled with definitions - they rely on other files, to actually work. That's why I said you can't always just grab a header file, and use it in some other compiler - sometimes, you might get lucky, but if you don't - it's very unlikely anyone can help you out.
    Last edited by Adak; 05-19-2010 at 08:05 AM.

  5. #20
    Registered User jephthah's Avatar
    Join Date
    May 2010
    Location
    seattle
    Posts
    49
    well, you can't grab a header file, if you dont have the library to link to the function.

    i very much doubt you have teh library for bios.h installed.

  6. #21
    Registered User
    Join Date
    May 2010
    Posts
    7
    hey guys! Thanks for all your help! I finally managed to figure out how to do it but in a different way. I used windows.h in the end
    Thanks!

  7. #22
    Registered User
    Join Date
    May 2010
    Posts
    7
    Hi as previously mentioned I have managed to get the RS-232 working however I am having a few more problems. I can see the data coming in from the RS-232 on to the screen however I am having trouble saving it to a text and I really dont know why! Can anyone help me please?? This is the code i have:

    #include <stdio.h>
    #include <stdlib.h>
    #include <windows.h>
    #include <winbase.h>

    #define COM1 4
    #define SETTINGS ( _COM_9600 | _COM_NOPARITY | _COM_STOP1 | _COM_CHR8)

    int main (void)
    {


    DCB dcbCommPort;
    COMMTIMEOUTS CommTimeouts;
    char BytesRead;
    char InBuffer[100];
    int hComm;



    hComm = CreateFile( "COM5",
    GENERIC_READ | GENERIC_WRITE,
    0,
    0,
    OPEN_EXISTING,
    0,
    0);

    if (hComm == INVALID_HANDLE_VALUE)
    {
    printf("Could not open port.\n");
    return;
    }


    dcbCommPort.DCBlength = sizeof(DCB);
    GetCommState(hComm, &dcbCommPort);


    if(!BuildCommDCB("baud=9600 parity=N data=8 stop=1", &dcbCommPort))
    {
    hComm = NULL;
    printf("Cannot build comm DCB.\n");
    return;
    }

    if(!SetCommState(hComm, &dcbCommPort))
    {
    CloseHandle(hComm);
    hComm = NULL;
    printf("Cannot set comm state.\n");
    return;
    }

    CommTimeouts.ReadIntervalTimeout = MAXDWORD;
    CommTimeouts.ReadTotalTimeoutConstant = 0;
    CommTimeouts.ReadTotalTimeoutMultiplier = 0;
    CommTimeouts.WriteTotalTimeoutConstant = 250;
    CommTimeouts.WriteTotalTimeoutMultiplier = 1;

    if(!SetCommTimeouts(hComm, &CommTimeouts))
    {
    CloseHandle(hComm);
    hComm = NULL;
    printf("Cannot set comm timeouts.\n");
    return;
    }

    .

    int CommFlag = 1;

    while(CommFlag)
    {


    Sleep(1);
    ReadFile(hComm, InBuffer, 100, &BytesRead, NULL);

    FILE *fp;
    fp=fopen("test.txt","w");

    if(BytesRead)

    {
    InBuffer[BytesRead]=0;

    printf(fp,"%s\n", InBuffer);
    printf("%s",InBuffer);
    fclose(fp);
    }


    }
    }

    Any one have any ideas? Please I am kind of desperate right now :s!!
    THANKS!!

  8. #23
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    Im not very familiar with file-operations in C but i think this line:
    printf(fp,"%s\n", InBuffer);

    should really read
    fprintf(fp,"%s\n", InBuffer);

    Dont forget to check the return-value of fopen aswell, in case something went wrong.

    also for future reference please use code-tags when posting code, makes it much easier on the eye.

  9. #24
    Registered User
    Join Date
    May 2010
    Posts
    7
    hi,
    yes I have tried fprintf however it still didnt work and the file seems ok as it is being opened everytime i run it

  10. #25
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    1. Post your code in code tags. There is a sticky at the top of this forum that will show you how to do it if you don't know how. (Just highlight the code and press the hash/number system on the posting toolbar).

    2. printf(fp,"%s\n", InBuffer); Shakti is correct, it should be fprintf.

    3. I noticed you have Sleep(1) in your code. The argument to Sleep is in milliseconds so this will pause for 1 millisecond, not sure if this is what you want.

    4. Post any output and compiler warnings that you are getting, if you aren't getting any at all, say so.

    5. You created hComm as an int when it should be a HANDLE. CreateFile returns a HANDLE. Make hComm of type HANDLE.

    6. It looks like CommFlag (the condition in your while loop) is not being changed and there are no break statements, this is an infinite loop.

    7. There are probably some more, your code is poorly formatted (due to lack of code tags, I'd imagine).

  11. #26
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    When i looked again, fopen should be placed before the while-loop, otherwise you will truncate the file whenever you reopen it for writing.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 01-18-2008, 04:06 AM
  2. Replies: 4
    Last Post: 01-13-2008, 02:14 AM
  3. Replies: 7
    Last Post: 08-19-2007, 08:10 AM
  4. The new FAQ
    By Hammer in forum A Brief History of Cprogramming.com
    Replies: 34
    Last Post: 08-30-2006, 10:05 AM
  5. Set default directory with GetTempDir?
    By Bajanine in forum Windows Programming
    Replies: 2
    Last Post: 05-04-2003, 11:36 PM