Home > Software engineering >  A serial port Readfile two problems
A serial port Readfile two problems

Time:10-06

# include "stdafx. H"
# include "stdafx. H"
# include "iostream"
#include
#include
#include
using namespace std;

Int main (int arg c, char * argv [])

{
//define institutions body object DCB
DCB DCB.
//return status variables
BOOL status;
//define the error number
DWORD error_number;
//define a port name array
LPCWSTR port_name;
//LPCWSTR PORT;

//initialize state variables
The status=TRUE;
//initialize the port name array
COM2 port_name=_T (" ");
//memset (port_name, 0, sizeof (port_name));
//the name of the port assignment
//1. Open the specified serial port
//serial port name (COMx)
//printf (" % c ", _T (" COM2 "));
HANDLE hComm=CreateFile (port_name
//serial port attributes to the read/write
, GENERIC_READ | GENERIC_WRITE
//serial port devices must be exclusive access
, 0
//no security attribute
, 0
//serial port devices must use OPEN_EXISTING parameter
OPEN_EXISTING,
//asynchronous I/O
, 0
//for a serial port equipment this parameter must be zero or NULL
, 0);

//if the return is invalid
If (hComm==INVALID_HANDLE_VALUE)
{
//if the serial interface does not exist or is being used by another application,
//open a failure, the program exits
Cout<& lt;" Open port failure "& lt; //return (FALSE);
}
//2. Set the serial port parameters: baud rate, data bits, parity, stop bits information such as
//get the default parameters of the port GetCommState reads the serial port Settings (baud rate, calibration, stop bits, data bits, etc.).
if(! GetCommState (hComm, & amp; DCB))
{
Cout<& lt;" Failed to get a serial port information "& lt; //return (FALSE);
}

//modify baud rate
DCB. BaudRate=CBR_9600;
DCB. ByteSize=8;
DCB. FParity=0;
DCB. Parity=NOPARITY;
DCB. StopBits=ONESTOPBIT;
DCB. FRtsControl=RTS_CONTROL_ENABLE;
//reset the parameter
if(! SetCommState (hComm, & amp; DCB))
{
Error_number=GetLastError ();
Cout<& lt;" A serial port assignment failure "& lt; Status=0;
}
//if a serial port assignment failure, close a serial port
If (the status!
=1){
Cout<& lt;" A serial port assignment fail closed "& lt; The CloseHandle (hComm);
system("pause");
Return (status);
}

//3. To write data//serial data to be written
,2,3,4,5,6,7,8,9,0 char cWriteBuffer [10]={1};
//to be written data length
DWORD nNumberOfBytesToWrite=strlen (cWriteBuffer);
DWORD nBytesSent;
//DWORD nNumberOfBytesToWrite=sizeof (lpBuffer)/sizeof (lpBuffer [0]).
//write operation on
If (WriteFile (hComm, cWriteBuffer nNumberOfBytesToWrite, & amp; NBytesSent, NULL)==TRUE)
{
//nBytesSent +=1;
If (nNumberOfBytesToWrite!=nBytesSent)
{
Cout<& lt;" Data sent failure "& lt; }
The else
Cout<& lt;" Data sent successfully "& lt; }

//4.//is going to read the data to the serial port to read data

SetFilePointer (hComm, 0, 0, FILE_BEGIN);
TCHAR cReadBuffer [1024]={};
//TCHAR cReadBuffer [1024]
//to be written data length
DWORD nNumberOfBytesToRead=_tcslen (cReadBuffer);
//printf (" % d \ n ", nNumberOfBytesToRead);
DWORD nBytesReceive;
SetupComm (hComm, 1024102, 4);//input buffer and the size of the output buffer is 1024
COMMTIMEOUTS TimeOuts.//set read timeout
TimeOuts. ReadIntervalTimeout=1000;
TimeOuts. ReadTotalTimeoutMultiplier=500;
TimeOuts. ReadTotalTimeoutConstant=5000;//set to write timeout
TimeOuts. WriteTotalTimeoutMultiplier=500;
TimeOuts. WriteTotalTimeoutConstant=2000;
SetCommTimeouts (hComm, & amp; TimeOuts);//set the timeout
If (ReadFile (hComm, cReadBuffer nNumberOfBytesToRead, & amp; NBytesReceive, NULL)==TRUE)
{
Cout<& lt;" Read the data, the data is as follows: "& lt; Printf (" % d ", cReadBuffer);
}

system("pause");
//4. Close the serial port
The CloseHandle (hComm);
//system (" pause ");
return 0;
}

Output is now
Data sent successfully
Read data is successful, the following
* * * (random) press any key to continue,,,,,,,

Defines an array behind the parentheses are removed
Instead of TCHAR cReadBuffer [1024]; Output is
Data sent successfully

To receive serial data to print the code after

Q: 1, why the output is garbled, how can I print?
2, why array defined later read directly, but do not want it to directly read serial data, such as how to operate?
Such as online thanks


CodePudding user response:

Fyi:
 void HexDump) (char * buf, int len, int addr) {
int i,j,k;
Char binstr [80].

for (i=0; iIf (0==16) (I %) {
Sprintf (binstr, "% x - 08", I + addr);
Sprintf (binstr, "% s % 02 x", binstr, buf (unsigned char) [I]);
} else if (15==16) (I %) {
Sprintf (binstr, "% s % 02 x", binstr, buf (unsigned char) [I]);
Sprintf (binstr, "% s", binstr);
For (j=I - 15; j<=I; J++) {
Sprintf (binstr, "% s % c," binstr, ('! '& lt; Buf [j] & amp; & Buf [j] <='~')? Buf [j] : '. ');
}
Printf (" % s \ n ", binstr);
} else {
Sprintf (binstr, "% s % 02 x", binstr, buf (unsigned char) [I]);
}
}
If (0! 16)=(I %) {
16 k=16 - (I %);
for (j=0; jSprintf (binstr, "% s", binstr);
}
Sprintf (binstr, "% s", binstr);
K=16 - k;
For (j=I - k; jSprintf (binstr, "% s % c," binstr, ('! '& lt; Buf [j] & amp; & Buf [j] <='~')? Buf [j] : '. ');
}
Printf (" % s \ n ", binstr);
}
}

CodePudding user response:

reference 1/f, zhao teacher reply:
are for reference only:
 void HexDump) (char * buf, int len, int addr) {
int i,j,k;
Char binstr [80].

for (i=0; inullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related