Home > Software engineering >  VS2017 sending a string to a serial port on the instructions
VS2017 sending a string to a serial port on the instructions

Time:05-15

Send the command string rotating mechanical arm can be controlled, my assistant can send the same instruction via a serial port functionality, but use the following procedure to send, mechanical arm have no reaction, spontaneous since closed, also tried to use a serial port application sends data and serial assistant data is the same, want to have bosses can give directions,


# include "UART. H"

//serial port variable
HANDLE hCom.

DWORD wCount;//the actual number of bytes to read
BOOL bwriteStat;
BOOL bReadStat;
Const char STR [81]="# L18A180H15T1000Z";//initialize array
Unsigned char STRS [81]={0};
Void UART_Init ()
{
HCom=CreateFile (TEXT (" COM6 "),//COM3 mouth
GENERIC_READ | GENERIC_WRITE,//allow to read and write
0,//exclusive way
NULL,
OPEN_EXISTING,//open rather than create
0,//synchronously
NULL);
If (hCom==(HANDLE) (1))//is the 1 cast HANDLE type.
{
Printf (" open COM failed! \n");
//return FALSE.
}
The else
{
Printf (" COM open success! \n");
}

SetupComm (hCom, 1024, 1024);//input buffer and the size of the output buffer is 1024
COMMTIMEOUTS TimeOuts.//call the COMMTIMEOUTS structure, members of the unit are in milliseconds,
//set read timeout
TimeOuts. ReadIntervalTimeout=1000;//receiving, the largest delay between the two characters
TimeOuts. ReadTotalTimeoutMultiplier=500;//read time coefficient
TimeOuts. ReadTotalTimeoutConstant=5000;//read time constants
//set to write timeout timeout=total bytes ReadTotalTimeoutMultiplier * + ReadTotalTimeoutConstant
TimeOuts. WriteTotalTimeoutMultiplier=500;//write every byte timeout
TimeOuts. WriteTotalTimeoutConstant=2000;//write a serial port data of fixed timeout
SetCommTimeouts (hCom, & amp; TimeOuts);//set the timeout


DCB DCB.//call the BCB structure
GetCommState (hCom, & amp; DCB);//GetCommState function can obtain the COM equipment control block, to obtain relevant parameters read serial port Settings (baud rate, calibration, stop bits, data bits, etc.).
DCB. BaudRate=115200;//baud rate to 115200
DCB. ByteSize=8;//each byte has eight
DCB. Parity=NOPARITY;//and white parity bit
DCB. StopBits=ONE5STOPBITS;//a stop bit
SetCommState (hCom, & amp; DCB);//SetCommState function of equipment control block set the COM:
Sleep (80);
PurgeComm (hCom, PURGE_TXCLEAR | PURGE_RXCLEAR);//PurgeComm () function to empty buffer
//PurgeComm (hCom, PURGE_TXCLEAR | PURGE_RXCLEAR);//before the read/write a serial port to empty buffer
/* PURGE_TXCLEAR this command guidance device driver output buffer, often used with PURGE_TXABORT command symbol
PURGE_RXCLEAR this command is used for device driver input buffer, often used with PURGE_RXABORT command symbol */
//DWORD wCount;//the actual number of bytes to read
//BOOL bReadStat;
}



Void Write_UART ()
{


//char STR [41]={0};//initialize array
//unsigned char STR [81]={' % '};//initialize array
//printf (" % s ", STR);
BwriteStat=WriteFile (hCom, STR, 40, & amp; WCount, NULL);
//bReadStat=ReadFile (hCom, STR., 80, & amp; WCount, NULL);
/* ReadFile () function from the specified file or an I/O device, read data,
If the device supports file pointer, read the position is the location of the file pointer,
This function is set to support both synchronous and asynchronous operation, */
if (! BwriteStat)
{
Printf (" write a serial port failure!" );
//return FALSE.
}
The else
{
Printf (" write a serial port success!" );
//printf (" % LLD, wCount);
}

}


Int main ()
{
int i;
UART_Init ();
Write_UART ();
Sleep (5 * 1000);
system("pause");
return 0;
}
  • Related