Original serial port library V2 use example, very simple, master

xiaoxiao2021-03-06  77

Have many friends to believe in the last time the serial class library is used. I have a simple program. You can take a look at the friends who need it; cncomm.h See http://blog.9cbs.net/wujian53 /archive/2004/10/23/148385.aspx

C code color by C 2HTML

.comment {color: # 999999; font-style: italic;

.pre {color: # 000099;

. String {color: # 009900;

.char {color: # 009900;

.float {color: # 996600;

.int {color: # 999900;

.bool {color: # 000000; font-weight: bold;}

.Type {color: # ff6633;

.flow {color: # ff0000;}

Keyword {color: # 990000;

. galan {color: # 663300; font-weight: bold;

#pragma Warning (Disable: 4530)

#pragma Warning (Disable: 4786)

#include

#include

Using namespace std;

#include

#include

#include "cncomm.h"

// The following procedure is sent once every 500ms from COM1 to COM2

// I have no actual test, not in the unit, no connection

/ / But I believe there is no problem.

// Receiving data is in the thread

Class mycomm: public cncomm // extension from CNCOMM, handles yourself

{

PUBLIC:

// This is the virtual function of CNCOMM, which is called for the monitoring thread, and the monitoring thread will call this function.

// If you are a desktop app to send a custom window message, this is the default usage in CNCOMM

Virtual void onRecEive ()

{

Char buffer [256];

IF (read (buffer, 256)) // read data from serial port and print on the console window

COUT << "Receive Data Form" << _dwport << ":" << buffer << endl;

}

}

Int main (int Argc, char * argv [])

{

Try

{

// Test the COM1 COM2 connected to the PC

CNCOMM COM1 (False, 0); // Blocking serial port 1 is used to send data

COM1.Open (1, 1200); // baud rate 1200

Mycomm COM2; // Inherited Extension Use Non-Block Serial Port 2 to Accept Data

COM2.Open (2, 1200);

CHAR BUFFER [100] = {0};

For (int i = 0; i <10; i )

{

Sleep (500); // Send once every 500ms

Sprintf (buffer, "This is the first% D group data from Com1", i 1);

/ / This is here for asynchronous, ie non-blocking read and write mode, and the function will return immediately, and the number of bytes returned to 0 is 0.

// The actual transmission is in the background of the system

// I use it here to blocked, for you can see the process of sending, and the number of bytes sent; // Generally use non-blocking (asynchronous)

Cout << "Com1 Send Data" << com1.write (buffer) << "Byte." << endl;

}

SLEEP (500);

COUT << "exit ..." << endl;

Com1.close ();

}

Catch (Exception & E)

{

Cout << E.WHAT () << endl;

}

Return 0;

}

转载请注明原文地址:https://www.9cbs.com/read-94162.html

New Post(0)