Serial Port Communications 
Author Message
 Serial Port Communications

There are some examples with VC++.
1. TTY - a terminal emulation program which reads from COM port and writes
it on to the display.
2. MTTY- A better version of TTY. YOU can find this MS online site. Search
for it at www.microsoft.com/msdn.
3. VCTERM-  a sample on how to use MSCOMM32.OCX with VC++ . This is good for
simple COMM port operations.
Check them out..

--
Girish Bharadwaj [VC++/MVP]



Quote:
>Does anybody have an example of reading from the PC serial port?
>Thanks in advance for any kind of help.

>Juan P. Arboleda




Mon, 31 Jul 2000 03:00:00 GMT  
 Serial Port Communications

Quote:
>Does anybody have an example of reading from the PC serial port?
>Thanks in advance for any kind of help.

Juan,

Have a look for the MS TTY or MTTTY sample programs. I think TTY is
supplied with VC, and MTTTY is available on MSDN or from the MS web
site.

Dave
----
Address is altered to discourage junk mail.
Remove ".---" for the real address.
http://www.bj.co.uk



Mon, 31 Jul 2000 03:00:00 GMT  
 Serial Port Communications

There are some good examples on the MSDN CD-ROM.  Serial port stuff is
a bit weird, because you can do things like set a timeout interval
that makes a synchronous I/O call like ReadFile(,,,NULL) (that is,
without an OVERLAPPED structure, for a normal, non-overlapped
CreateFile call) act like an asynchronous call.  Generally, serial I/O
is done by creating a separate thread to handle the device itself, and
using user-defined messages to post notifications back to the main
application thread that a message had been received, or an error or
timeout occurred.
                                joe

On Fri, 06 Feb 1998 08:28:44 +0100, Jaume Figueras i Jove

Quote:

>Does anybody have an example of reading from the PC serial port?
>Thanks in advance for any kind of help.

>Juan P. Arboleda


Joseph M. Newcomer

http://www3.pgh.net/~newcomer


Wed, 02 Aug 2000 03:00:00 GMT  
 Serial Port Communications

I'm rather new at Visual C++, so this may not be the best way to use a com
port, but it seems to work.  Besides that, if there is something really
wrong or naive I hope to hear about it.

This works for me, but I'm sure there is a BETTER way:

//
// Init Com port here
//

HANDLE CComm_4_aDlg::k_open_com(int com_port, int baud, int bits, int
stop_bits, int parity)
{

        BOOL kSuccess;

        HANDLE hCom;
        const char K_port[10][6] = {"COM1", "COM2",
                "COM3" , "COM4", "COM5", "COM6",
                "COM7", "COM8", "COM9", "COM10"};

        hCom = CreateFile(K_port[com_port],
          GENERIC_READ | GENERIC_WRITE,
          0,                // comm devices must be opened
w/exclusive-access
          NULL,             // no security attrs
          OPEN_EXISTING,    // comm devices must use OPEN_EXISTING
          0,                // not overlapped I/O
          NULL              // hTemplate must be NULL for comm devices
    );

        if (hCom == INVALID_HANDLE_VALUE) {
                kSuccess = CloseHandle(MC[com_port].last_used_handle);
                strcpy(E_Msg,"ERROR Creating COMM Port ");
                strcat(E_Msg, K_port[com_port]);
                return(hCom);           // funct returned error
        }

        MC[com_port].last_used_handle = hCom;   // Save for later use
        kSuccess = GetCommState(hCom, &dcb);

        if (!kSuccess) {
                strcpy(E_Msg,"ERROR Getting Status COMM Port ");
                strcat(E_Msg, K_port[com_port]);
                return((HANDLE) -(GET_COM_STATUS_ERROR));       // ERROR
        }

        saved_dcb = dcb;                // save old DCB values

  // Fill in the DCB: baud=9600, 8 data bits, no parity, 1 stop bit.

        dcb.BaudRate = baud;
        dcb.ByteSize = bits;
        dcb.Parity = parity;
        dcb.StopBits = stop_bits;

        kSuccess = SetCommState(hCom, &dcb);

        if (!kSuccess) {
                strcpy(E_Msg,"ERROR Setting COMM Port ");
                strcat(E_Msg, K_port[com_port]);
                return((HANDLE) -(SET_COM_STATUS_ERROR));       //ERROR
        }

//
//  Get the current Com timeout configuration.
//
                LP_S_commtimeouts = &S_commtimeouts;
                kSuccess = GetCommTimeouts(hCom, LP_S_commtimeouts);

                if (!kSuccess) {
                        strcpy(E_Msg, "ERROR Getting Timeouts for port ");
                        strcat(E_Msg, K_port[com_port]);
                        return((HANDLE) -(GET_COM_TIMEOUT_ERROR));
//ERROR
                }

//  Now modify the timeouts to our needs                  
                S_commtimeouts.ReadIntervalTimeout = 0;
                S_commtimeouts.ReadTotalTimeoutMultiplier = T_Multiplier;
                S_commtimeouts.ReadTotalTimeoutConstant = T_Constant;

//  Set new modified timeouts
                kSuccess = SetCommTimeouts(hCom, LP_S_commtimeouts);

                if (!kSuccess) {
                        strcpy(E_Msg, "ERROR setting Timeouts for port ");
                        strcat(E_Msg, K_port[com_port]);
                        return((HANDLE) -(SET_COM_TIMEOUT_ERROR));
//ERROR
                }

        return(hCom);   // everything OK

Quote:
}

//
// Sends time out port and expects either ACK or NAK back
// Calling routine calls error handeler if this rets nonzero
//

int CComm_4_aDlg::K_send_time(HANDLE hCom_opened, char * k_time){

        BOOL fSuccess;
        int tries = 0;
        do{
                fSuccess = WriteFile(hCom_opened, k_time, strlen(k_time),
&dwWritten, NULL);; // send time
                if (!fSuccess) {
                        strcpy(E_Msg, "ERROR Writing to port");
                }
                else{

                        fSuccess = ReadFile(hCom_opened, kreceived, 2,
&dwRead, 0);
                        if (!fSuccess || dwRead != 2) {
                                strcpy(E_Msg, "ERROR Reading from port");
                                fSuccess = 0;       // Set error Flag
                        }
                }
                if(tries++ >= MAX_TRIES)
                        break;
                K_wait(1000);           //Time for MC to recoup
        }while(kreceived[0] != ACK);
        if(tries >= MAX_TRIES || !fSuccess){
                if(fSuccess){
                        strcpy(E_Msg, "MC returned NAK");
                        fSuccess = 0;
                }
        }
        return (fSuccess);

Quote:
}

On Fri, 06 Feb 1998 08:28:44 +0100, Jaume Figueras i Jove

Quote:

>Does anybody have an example of reading from the PC serial port?
>Thanks in advance for any kind of help.

>Juan P. Arboleda


___
Ken

When they took the fourth amendment, I was quiet because
   I didn't deal {*filter*}!
When they took the sixth amendment, I was quiet because
   I was innocent.
When they took the second amendment, I was quiet because
   I didn't own a gun!
Now they've taken the first amendment, and I can say
   nothing about it.



Wed, 02 Aug 2000 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Help: RS232 serial port communications

2. Serial port communication

3. Serial Port Communication By C/C++.

4. serial port communication in C#

5. Serial Port Communications

6. Serial port communications

7. Need help on serial port communication.

8. FFT Function & Serial Port Communication

9. serial port communication

10. Serial port communication

11. Serial port communication

12. Serial port communications ...

 

 
Powered by phpBB® Forum Software