Thread: sending streams over serial port in win32

  1. #1
    Registered User
    Join Date
    May 2004

    sending streams over serial port in win32

    Hi !
    I need a commented sample code for a Win app to send a stream via the serial port. Unfortunately the C
    is the only language I know. I have found some example codes on this forum, but I could'n understand
    the whole codes because I know very little about WinApi. I have already made a window and some menus
    using DevC++ and a skeleton code from SourceForge (changed properly). My app should send a file or a text
    typed in the app window to a PIC microcontroller connected to the serial port over an MAX232 inverter.
    It should run as follows:
    1. Sends an "active(?)" RTS signal to the uC.
    2. Polls the CTS signal from the uC until it turns "active(?)"(or uses an interrupt to do this ?).
    3. Sends the data with params 57600 Kbps, 8bits, no parity, 1 Stop bit.
    Very important:
    It should work on any win95 platform and above! Well, if not i would like the code to be as simple as possible
    (as I know the NT based systems work differently with this kind of peripherals and accesing some drivers would be
    necesarry) . My OS is Vista and if I run the app in WIN95 compatibility mode is it going to work ?
    Also the CTS and RTS are used once per stream (not for every byte) - the uC is smart enough to know how much and how long
    it will receive. It would be nice to know the Rs232 levels when these 2 signals are enabled (and to have the option to
    change their logical level in the code), because I'm a little confused. I have 2 serial monitor programms and if I set
    the RTS pin "ON" i get a positive RS232 level and 0 V TTL level at the uC and vice-versa (from the PiC to the PC). Shouldn't that be
    reversed ?
    I would also like to know how can I get the text typed in the app window and put it in a buffer (in order to send it).

    Thanks in advance !

  2. #2
    Registered User
    Join Date
    May 2004

    help to fix a problem

    I wrote the following code:
    void serialportsetup(){
    hCom = CreateFile( "COM1",
        0,    // exclusive access 
        NULL, // no security attributes 
    if (hCom == INVALID_HANDLE_VALUE){
      MessageBox(hEdit, "The COM port cannot be openned !", "Error!", MB_OK | MB_ICONEXCLAMATION);
    bSuccess = GetCommState(hCom, &dcb);
    if (!bSuccess) {
        MessageBox(hEdit, "Cannot read the COM port state !", "Error!", MB_OK | MB_ICONEXCLAMATION);
    dcb.BaudRate = 57600;
    dcb.ByteSize = 8;
    dcb.Parity = NOPARITY;
    dcb.StopBits = ONESTOPBIT;
    bSuccess = SetCommState(hCom, &dcb);
    if (!bSuccess){
        MessageBox(hEdit, "The COM port cannot be configured !", "Error!", MB_OK | MB_ICONEXCLAMATION);
    bSuccess = SetCommMask(hCom, EV_CTS );
    if (!bSuccess){
         MessageBox(hEdit, "The CTS pin state cannot be changed !", "Error!", MB_OK | MB_ICONEXCLAMATION);  
        o.hEvent = CreateEvent(  NULL,TRUE,FALSE,NULL);
    Send(UINT command1, UINT sendlength){
           char command=(char)command1;
         if(black_bkgd) command=command | 0x20;
          SetCommState(hCom, &dcb);
        MessageBox(hEdit, "Send request !", "Warning !", MB_OK | MB_ICONEXCLAMATION);
        if (WaitCommEvent(hCom, &dwEvtMask, &o)) ;
            if (dwEvtMask & EV_CTS){ 
       	      MessageBox(hEdit, "CTS received !", "Warning !", MB_OK | MB_ICONEXCLAMATION);
    	    SetCommState(hCom, &dcb);
     	    if (!bSuccess) {
        	 MessageBox(hEdit, "Cannot write data to the COM  port!", "Error!", MB_OK | MB_ICONEXCLAMATION); wrong check...not important,anyway
             if(mode!=5) WriteFile(hCom,sendbuffer,sendlength,NULL,&b);
    			     for(j=0;j<16;j++,i++ ){
            MessageBox(hEdit, "Message sent !", "Warning !", MB_OK | MB_ICONEXCLAMATION);
    If I run this code for the first time as Send command within my app window it works. But it doesn't work correctly if I run it from the second time. As described in the post above after putting the RTS to the ACTIVE state(0 logic ,positive voltage),
    the PC should wait for the CTS signal to get to the same state (after receiving the CTS signal, the RTS signal turns to inactive immediately, not after the data is sent !). First run after the app launch is successful but afterwards not anymore. When the microcontroller does not send the CTS signal I get the "CTS received"
    message on the screen(at first run I don't get this - that's a good thing) .Something is wrong with the "event functions". I used the ResetEvent function because if I used the automatic reset the WaitEvent function could falsely detect transisions from active to inactive state . I'm interested only in detecting "inactive to active" state change(1 logic to 0 logic), not both. Could somebody help me?

  3. #3
    Registered User
    Join Date
    Apr 2006
    I think your code should be in Windows Programming section.

    And it seems like you have too much information here, you need to isolate your problem.
    ★ Inferno provides Programming Tutorials in a variety of languages. Join our Programming Forums. ★

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. serial port to poll on request
    By infineonintern in forum C++ Programming
    Replies: 2
    Last Post: 06-11-2009, 06:52 AM
  2. sending data over UDP from serial port
    By forumguy in forum Linux Programming
    Replies: 0
    Last Post: 04-25-2009, 02:10 PM
  3. Sending 1 bit to serial port
    By blackcell in forum C Programming
    Replies: 7
    Last Post: 06-06-2008, 11:20 PM
  4. Can't Read From Serial Port
    By HalNineThousand in forum Linux Programming
    Replies: 14
    Last Post: 03-20-2008, 05:56 PM
  5. DOS, Serial, and Touch Screen
    By jon_nc17 in forum A Brief History of
    Replies: 0
    Last Post: 01-08-2003, 04:59 PM