Home > Back-end >  How to use C language to read the serial data
How to use C language to read the serial data

Time:10-31

GPS module the connect the USB port on the computer how to use C language to connect a serial port and reads the data inside the
Please let us know your small white consult bosses in detail

CodePudding user response:

Library search "serial"

CodePudding user response:

 # # ifndef _COMM_H_ 
# define _COMM_H_
# define WM_FOUNDCOMM WM_USER + 1//custom message WM_FOUNDCOMM, received the news that a serial port has found
# define WM_READCOMM WM_USER + 2//custom message WM_READCOMM, received the message buffer with the data, can read
Extern void FindComm ();//declaration for external function
Extern void OpenComm (int nBaud, int nData, int nStop, int nCal);
Extern void CloseComm ();
Extern UINT ThreadFunc (LPVOID pParam);//that global thread processing function
Extern cstrings DisplayCString2Hex (cstrings Data, bool Blank_allow);
Extern cstrings DisplayHex2CString cstrings (Data);
Extern int ConvertHexC2String (cstrings STR, CByteArray & amp; Senddata publishes the event);
Extern bool ComIsOK;//declaration for external variables
Extern HANDLE hCom.
Extern cstrings strcomname;
# endif


 # include "stdafx. H" 
# include "commassist. H"
# include "commassistDlg. H"
H # include "comm."
Char ConvertHexChar (char ch);
HANDLE hCom.//handle to a serial port
Cstrings strcomname;//a serial port, such as "COM1"
Bool ComIsOK;//serial port open identity, open to true says it has, or not to open the
//============automatic searching for a serial port function=================================
//function function: by scanning the registry to find out the current all the physical serial port
//input parameters: no
//return type: no
//Ming said: if the search is successful, the search to a serial port and send each message notify the main dialog box, and the serial number passed in WPARAM
Void FindComm ()
{
//enumerated the current system of serial port
LONG result=0;
Hkeys key=NULL;
Result=RegOpenKeyEx (HKEY_LOCAL_MACHINE,//the name of the need to open the primary key of the
"The HARDWARE \ \ DEVICEMAP \ \ SERIALCOMM",//need to open the key the name of the son, equipment serial
0 ,//保留必须设置为0
KEY_READ,//安全访问标记也就是权限
& Key);//得到的将要打开键的句柄当不再需要句柄
//must call RegCloseKey close it
If (result)
{
AfxMessageBox("无法获取串口请确认是否安装并连接串口!" ); return;
}
TCHAR portname [250].//serial port name
TCHAR data [250].
DWORD portnamelen=0; The length//serial port
DWORD datalen=0;
Int index=0;
While (1) after//find the COM out
{
Portnamelen=255;
Datalen=255;
Result=RegEnumValue(key,//Long一个已打开项的句柄或者指定一个标准项名
Index++,//Long欲获取值的索引,注意第一个值的索引编号为零
Portname,//String用于装载位于指定索引处值名的一个缓冲区
& Portnamelen,//Long用于装载lpValueName缓冲区长度的一个变量,
//一旦返回它会设为实际载入缓冲区的字符数量
NULL,//Long未用设为零
NULL,//Long用于装载值的类型代码的变量
(LPBYTE)data,//Byte用于装载值数据的一个缓冲区
& Datalen);//Long用于装载lpData缓冲区长度的一个变量,
//一旦返回它会设为实际载入缓冲区的字符数量
If (result) break;
//send a message to WM_USER + 1 for custom message, or find a serial port, and the serial number "COMx" through the WPARAM parameter passed to main dialog window
//: : AfxGetMainWnd () - & gt; M_hWnd, obtain the main dialog box handle
//(WPARAM) (LPCTSTR) data, type conversion

: : SendMessage (: : AfxGetMainWnd () - & gt; M_hWnd, WM_FOUNDCOMM, (WPARAM) (LPCTSTR) data, 0);
}
RegCloseKey (key);//call RegCloseKey close the handle to open the key
}
//============looking for a serial port function automatically end==================

//==========open serial port function===========================
//can work: open the serial port, will have to open the serial port handle assigned to hCom, given a serial port open ComIsOK, completion string
//input parameters: baud rate, data bits, stop bits, parity
//return type: no
Void OpenComm (int nBaud, int nData, int nStop, int nCal)
{
HCom=CreateFile (strcomname,//serial no.
GENERIC_READ | GENERIC_WRITE,//allowed to read or write
0,//exclusive way
NULL,
OPEN_EXISTING,//open rather than create
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,//overlapping way, used in asynchronous communication
NULL);

If (hCom==INVALID_HANDLE_VALUE)
{
AfxMessageBox("打开COM失败串口不存在或已被占用!" );
ComIsOK=false;
return;
}
ComIsOK=true;
SetCommMask (hCom, EV_TXEMPTY | EV_RXCHAR);//set up the event mask, temporarily useless on
SetupComm (hCom, 1024, 1024);//set the input buffer and the size of the output buffer is 1024
COMMTIMEOUTS TimeOuts.
//set read timeout
TimeOuts. ReadIntervalTimeout=MAXDWORD;
TimeOuts. ReadTotalTimeoutConstant=0;
TimeOuts. ReadTotalTimeoutMultiplier=0;
//set to write timeout
TimeOuts. WriteTotalTimeoutConstant=500;
TimeOuts. WriteTotalTimeoutMultiplier=100;
If (SetCommTimeouts (hCom, & amp; TimeOuts)==false)
{
The CloseHandle (hCom);
ComIsOK=false;
return;
}
//serial port configuration properties
DCB DCB.
GetCommState (hCom, & amp; DCB);
DCB. BaudRate=nBaud;//DCB. BaudRate=9600;//baud rate to 9600
DCB. ByteSize=nData;//DCB. ByteSize=8;//each byte is eight
DCB. StopBits=nStop;//DCB. StopBits=ONESTOPBIT;//one stop bit
DCB. Parity=nCal;//DCB. Parity=NOPARITY;//and white accidentally test a total of 39 pages 18
SetCommState (hCom, & amp; DCB);
PurgeComm (hCom, PURGE_TXCLEAR | PURGE_RXCLEAR);
If (SetCommState (hCom, & amp; DCB)==false)
{
The CloseHandle (hCom);
ComIsOK=false;
return;
}
return;
}
//==========open serial port function end=====================


//==========serial port closed control function=====================
Void CloseComm ()
{
The CloseHandle (hCom);
HCom=NULL;
ComIsOK=false;
}
//==========end of serial port closed control function==================

//==========a serial port to monitor the thread function======================
UINT ThreadFunc (LPVOID pParam)
{
//CCommassistDlg * PDLG=(CCommassistDlg *) pParam;//define the pointer to the main dialog
The COMSTAT COMSTAT;
DWORD dwErrorFlags;
While (ComIsOK)
{
DWORD dwBytesRead=100;
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related