當前位置:首頁 » 編程語言 » c語言上位機串口
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

c語言上位機串口

發布時間: 2022-05-24 07:14:34

① 怎麼實現利用C#編上位機程序,然後通過串口

#include<stdio.h>
intmain()
{
charsz[30]={0};
charcTmp=Ɔ'
inti=0;
printf("Pleaseinputastring:");
while(1)
{
scanf("%c",&cTmp);
if(cTmp==' '){
break;
}
sz[i]=cTmp;
i++;
}

for(i=0;i<strlen(sz);i++)
{
printf("%c",sz[i]);
}
printf(" ");
return0;
}

② 急!!!單片機c語言實現串口通信編程

以下是我剛改的程序編譯成功了
請參考

#include"reg51.h"
//定義全局變數
unsigned char data_10[10]={0,0,0,0,0,0,0,0,0,0};
unsigned char Time_50ms,count;
bit flag=0;
bit data_flag=0;
/*********************************************************************************************
函數名:UART串口初始化函數
調 用:UART_init();
參 數:無
返回值:無
結 果:啟動UART串口接收中斷,允許串口接收,啟動T/C1產生波特率(佔用)
備 註:振盪晶體為12MHz,PC串口端設置 [ 4800,8,無,1,無 ]
/**********************************************************************************************/
void UART_init (void){
EA = 1; //允許總中斷(如不使用中斷,可用//屏蔽)
ES = 1; //允許UART串口的中斷
TMOD |= 0x20;//定時器T/C1工作方式2
SCON = 0x50;//串口工作方式1,允許串口接收(SCON = 0x40 時禁止串口接收)
TH1 = 0xF3;//定時器初值高8位設置
TL1 = 0xF3;//定時器初值低8位設置
PCON = 0x80;//波特率倍頻(屏蔽本句波特率為2400)
TR1 = 1;//定時器啟動
}
/**********************************************************************************************/
/*********************************************************************************************
函數名:UART串口接收中斷處理函數
調 用:[SBUF收到數據後中斷處理]
參 數:無
返回值:無
結 果:UART串口接收到數據時產生中斷,用戶對數據進行處理(並發送回去)
備 註:過長的處理程序會影響後面數據的接收
/**********************************************************************************************/
void UART_R (void) interrupt 4 using 1{ //切換寄存器組到1
TR0=1; //打開定時器開始計時
RI = 0;//令接收中斷標志位為0(軟體清零)
data_10[count] = SBUF;//將接收到的數據送入變數 UART_data
count++;//接收到一個位元組數據計數+1
if(count>=10) //如果接收到10個數據
{
TR0=0; //停止定時器
TH0 = 0x3C; //給定時器賦初值
TL0 = 0xB0; //給定時器賦初值
count=0;//清零數據計數
//data_flag=1; //數據有效標志位
SBUF = 0x55;//返回數據 55H
while(TI == 0);//檢查發送中斷標志位
TI = 0;//令發送中斷標志位為0(軟體清零)
}
if(flag)
{
TR0=0; //停止定時器
TH0 = 0x3C; //給定時器賦初值
TL0 = 0xB0; //給定時器賦初值
count=0;//清零數據計數
SBUF = 0xff;//返回數據 ffH
while(TI == 0);//檢查發送中斷標志位
TI = 0;//令發送中斷標志位為0(軟體清零)
}
}
/**********************************************************************************************/
/*********************************************************************************************
函數名:定時/計數器初始化函數
調 用:T_C_init();
參 數:無
返回值:無
結 果:設置SFR中T/C1和(或)T/C0相關參數
備 註:本函數控制T/C1和T/C0,不需要使用的部分可用//屏蔽
/**********************************************************************************************/
void T_C_init (void){
TMOD |= 0x01; //高4位控制T/C1 [ GATE,C/T,M1,M0,GATE,C/T,M1,M0 ]
EA = 1;//中斷總開關
TH0 = 0x3C; //16位計數寄存器T0高8位
TL0 = 0xB0; //16位計數寄存器T0低8位(0x3CB0 = 50mS延時)
ET0 = 1; //T/C0中斷開關
TR0 = 0; //T/C0開關
}
/**********************************************************************************************/
/*********************************************************************************************
函數名:定時/計數器0中斷處理函數
調 用:[T/C0溢出後中斷處理]
參 數:無
返回值:無
結 果:重新寫入16位計數寄存器初始值,處理用戶程序
備 註:必須允許中斷並啟動T/C本函數方可有效,重新寫入初值需和T_C_init函數一致
/**********************************************************************************************/
void T_C0 (void) interrupt 1 using 1{ //切換寄存器組到1
TH0 = 0x3C; //16位計數寄存器T0高8位(重新寫入初值)
TL0 = 0xB0; //16位計數寄存器T0低8位(0x3CB0 = 50mS延時)
Time_50ms++; //50ms到 計數+1
if(Time_50ms>=100)
{
Time_50ms=0;// 清零50ms計數
flag=1; //5s時間 標志置位
TR0=0;//關閉計時器
}
}
/**********************************************************************************************/
main()
{
IP = 0x10; //中斷優先順序設置(串口中斷最高優先順序)
UART_init();//初始化串口
T_C_init(); // 初始化計數器
while(1);// 空循環
}

③ c語言 上位機程序 無法讀取串口數據

你把fopen裡面的參數 「r」 改成 「a+」 試一下

④ C語言編程 設計串口通信的上位機軟體通信問題

你好,聽你的描述,你用的是VC設計界面的是么?VC對串口操作可以通過兩個途徑:一個是控制項操作,一個是流操作。
前者:你可以在project----Add to Project----components and ...
中打開registed ActiveX Control
中選擇,Microsoft Communications Control 控制項,選擇insert 然後close 在右邊控制項欄里多了一個電話標志的控制項,繪制到界面中,設置相應屬性即可使用。
具體用法你可以GOOGLE關鍵詞「MSCOMM 控制項用法」
(用法簡單,事件處理方便,但是通信成功率比較低)

另一種是用fopen打開相應的串口流,進行書寫。
具體用法可以GOOGLE關鍵詞「VC 串口通信 fopen」
(其用法類似與文件讀寫。使用fwrite和fread操作,其精確度高,但事件觸發方面的設置比較麻煩。)

⑤ 請教老哥們一個c語言程序,STC89C52單片機用鍵盤通過串口通信給上位機發送信號

設計要求都包括什麼!

⑥ 誰能給我一個上位機控制下位機的串口通信程序(C語言編寫吧)

#include "stdafx.h"
#include "CommWizard.h"
#include "CommWizardDlg.h"
#include "SettingDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
CAboutDlg();

// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA

// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL

// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCommWizardDlg dialog

CCommWizardDlg::CCommWizardDlg(CWnd* pParent /*=NULL*/)
: CDialog(CCommWizardDlg::IDD, pParent)
{
bSend = FALSE;
bReceive = FALSE;

m_nPort = 1;
m_strSettings = _T("9600,n,8,1");
m_strSendString = _T("");
m_nTime = 1000;

m_nInputMode = 1;

//{{AFX_DATA_INIT(CCommWizardDlg)
m_strReceive = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CCommWizardDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CCommWizardDlg)
DDX_Control(pDX, IDC_RECEIVE, m_ctrlReceive);
DDX_Control(pDX, IDC_SEND, m_ctrlSend);
DDX_Control(pDX, IDC_TYPE, m_ctrlDataType);
DDX_Text(pDX, IDC_EDIT_RECEIVE, m_strReceive);
DDX_Control(pDX, IDC_COMMCTRL, m_Com);
//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CCommWizardDlg, CDialog)
//{{AFX_MSG_MAP(CCommWizardDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_RECEIVE, OnReceive)
ON_BN_CLICKED(IDC_SEND, OnSend)
ON_BN_CLICKED(IDC_SETTINGS, OnSettings)
ON_CBN_SELCHANGE(IDC_TYPE, OnSelchangeType)
ON_BN_CLICKED(IDC_CLEAR, OnClear)
ON_BN_CLICKED(IDC_MANUALSEND, OnManualsend)
ON_WM_TIMER()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCommWizardDlg message handlers

BOOL CCommWizardDlg::OnInitDialog()
{
CDialog::OnInitDialog();

// Add "About..." menu item to system menu.

// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);

CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}

// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon

// TODO: Add extra initialization here
m_ctrlDataType.AddString(_T("按ASCII碼"));
m_ctrlDataType.AddString(_T("按2進制"));
m_ctrlDataType.AddString(_T("按16進制"));
m_ctrlDataType.SetCurSel(m_nInputMode);

return TRUE; // return TRUE unless you set the focus to a control
}

void CCommWizardDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}

// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.

void CCommWizardDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting

SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;

// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}

// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CCommWizardDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}

void CCommWizardDlg::OnReceive()
{
// TODO: Add your control notification handler code here
m_Com.GetInput();//先預讀緩沖區以清除殘留數據
bReceive = !bReceive;
if(bReceive)
m_ctrlReceive.SetWindowText(_T("停止接收"));
else
m_ctrlReceive.SetWindowText(_T("開始接收"));
}

void CCommWizardDlg::OnSend()
{
// TODO: Add your control notification handler code here
bSend = !bSend;
if(bSend)
{
m_ctrlSend.SetWindowText(_T("停止發送"));
SetTimer(1,m_nTime,NULL);//時間為1000毫秒
}
else
{
m_ctrlSend.SetWindowText(_T("自動發送"));
KillTimer(1); //取消定時
}
}

void CCommWizardDlg::OnSettings()
{
// TODO: Add your control notification handler code here
CSettingDlg setDlg;

setDlg.nPort = m_nPort;
setDlg.nTime = m_nTime;
setDlg.strSettings = m_strSettings;
setDlg.strSendString = m_strSendString;

if(setDlg.DoModal() == IDOK)
{
m_nPort = setDlg.nPort;
m_nTime = setDlg.nTime;
m_strSettings = setDlg.strSettings;
m_strSendString = setDlg.strSendString;
}
OpenPort();
}

void CCommWizardDlg::OnSelchangeType()
{
// TODO: Add your control notification handler code here
m_nInputMode = m_ctrlDataType.GetCurSel();
}

void CCommWizardDlg::OnClear()
{
// TODO: Add your control notification handler code here
m_strReceive = _T("");
}

BEGIN_EVENTSINK_MAP(CCommWizardDlg, CDialog)
//{{AFX_EVENTSINK_MAP(CCommWizardDlg)
ON_EVENT(CCommWizardDlg, IDC_COMMCTRL, 1 /* OnComm */, OnOnCommCommctrl, VTS_NONE)
//}}AFX_EVENTSINK_MAP
END_EVENTSINK_MAP()

void CCommWizardDlg::OnOnCommCommctrl()
{
// TODO: Add your control notification handler code here
VARIANT variant_inp;
COleSafeArray safearray_inp;
LONG len,k;
BYTE rxdata[2048]; //設置BYTE數組 An 8-bit integerthat is not signed.
CString strtemp;
if(bReceive)
{
if(m_Com.GetCommEvent()==2) //事件值為2表示接收緩沖區內有字元
{
variant_inp = m_Com.GetInput(); //讀緩沖區
safearray_inp = variant_inp; //VARIANT型變數轉換為ColeSafeArray型變數
len=safearray_inp.GetOneDimSize(); //得到有效數據長度
for(k=0;k<len;k++)
safearray_inp.GetElement(&k,rxdata+k);//轉換為BYTE型數組
for(k=0;k<len;k++) //將數組轉換為Cstring型變數
{
BYTE bt=*(char*)(rxdata+k); //字元型

if(m_nInputMode == 2)
strtemp.Format("%02X ",bt); //將字元以十六進制方式送入臨時變數strtemp存放,注意這里加入一個空隔
else
strtemp.Format("%c",bt); //將字元送入臨時變數strtemp存放

m_strReceive = m_strReceive + strtemp; //加入接收編輯框對應字元串
}
m_strReceive += "\r\n";
}
}
UpdateData(FALSE); //更新編輯框內容
}

void CCommWizardDlg::OpenPort()
{
if(m_Com.GetPortOpen())
m_Com.SetPortOpen(FALSE);

m_Com.SetCommPort(m_nPort); //選擇com1
if( !m_Com.GetPortOpen())
m_Com.SetPortOpen(TRUE);//打開串口
else
AfxMessageBox("cannot open serial port");

m_Com.SetSettings(m_strSettings); //波特率9600,無校驗,8個數據位,1個停止位
m_Com.SetRThreshold(1);
//參數1表示每當串口接收緩沖區中有多於或等於1個字元時將引發一個接收數據的OnComm事件
m_Com.SetInputMode(1);
m_Com.SetInputLen(0); //設置當前接收區數據長度為0
m_Com.GetInput();//先預讀緩沖區以清除殘留數據
}

void CCommWizardDlg::OnManualsend()
{
// TODO: Add your control notification handler code here
CString strSend;

if(m_nInputMode > 1)
{
CByteArray hexdata;
int len=String2Hex(m_strSendString ,hexdata);
m_Com.SetOutput(COleVariant(hexdata));
}
else
m_Com.SetOutput(COleVariant(m_strSendString));//發送數據
}

void CCommWizardDlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
OnManualsend();
CDialog::OnTimer(nIDEvent);
}

int CCommWizardDlg::String2Hex(CString str, CByteArray &senddata)
{
int hexdata,lowhexdata;
int hexdatalen=0;
int len=str.GetLength();

senddata.SetSize(len/2);

for(int i=0;i<len;)
{
char lstr,hstr=str[i];
if(hstr==' ')
{
i++;
continue;
}
i++;
if(i>=len)
break;
lstr=str[i];
hexdata=ConvertHexChar(hstr);
lowhexdata=ConvertHexChar(lstr);
if((hexdata==16)||(lowhexdata==16))
break;
else
hexdata=hexdata*16+lowhexdata;
i++;
senddata[hexdatalen]=(char)hexdata;
hexdatalen++;
}

senddata.SetSize(hexdatalen);
return hexdatalen;
}

//這是一個將字元轉換為相應的十六進制值的函數
//功能:若是在0-F之間的字元,則轉換為相應的十六進制字元,否則返回-1
char CCommWizardDlg::ConvertHexChar(char ch)
{
if((ch>='0')&&(ch<='9'))
return ch-0x30;
else if((ch>='A')&&(ch<='F'))
return ch-'A'+10;
else if((ch>='a')&&(ch<='f'))
return ch-'a'+10;
else return (-1);
}

⑦ 要個計算機與單片機的串口通信上位機C語言程序

兄弟,下面是我自己寫的,希望給你幫助,把數據用Send_One_Uart()這個函數直接發送到串口就行了!最後求採納,哈哈!
//-----頭文件
#include
//-----定義全局變數
unsigned
char
Temp;
/***************************************************
**
函數名稱:
UART_Init
**
功能描述:
串口初始化函數
**

入:無
**

出:

**

明:

****************************************************/
void
UART_Init(void)
{
SCON
=
0x50;
//-----SCON:
模式
1,
8-bit
UART,
使能接收
TMOD
|=
0x20;
//-----TMOD:
timer
1,
mode
2,
8-bit
重裝
TH1
=
0xFD;
//-----TH1:
重裝值
9600
波特率
晶振
11.0592MHz
TR1
=
1;
//-----TR1:
timer
1
打開
ES
=
1;
//-----打開串口中斷
EA
=
1;
//-----打開總中斷
}
/***************************************************
**
函數名稱:
SendByte
**
功能描述:
發送位元組函數
**

入:無
**

出:

**

明:

****************************************************/
void
SendByte(unsigned
char
dat)
{
SBUF
=
dat;
while(!TI);
TI
=
0;
}
/***************************************************
**
函數名稱:
SendStr
**
功能描述:
發送字元串函數
**

入:無
**

出:

**

明:

****************************************************/
void
SendStr(unsigned
char
*s)
{
while(*s!='\0')
{
SendByte(*s);
s++;
}
}
/***************************************************
**
函數名稱:
main
**
功能描述:
主函數
**

入:無
**

出:

**

明:

****************************************************/
void
main
(void)
{
UART_Init();
//-----串口初始化
while(1)
{
if(Temp=='H'&&'i')
{
Temp=0;
SendStr("hello");
}
}
}
/***************************************************
**
函數名稱:
UART_SER
**
功能描述:
串口中斷服務子函數
**

入:無
**

出:

**

明:

****************************************************/
void
UART_SER
(void)
interrupt
4
//-----串列中斷服務程序
{
if(RI)
//-----判斷是接收中斷產生
{
RI=0;
//-----標志位清零
Temp=SBUF;
//-----讀入緩沖區的值
}
}

⑧ c語言怎麼實現串口通信

編程原理
程序1為查詢通信方式介面程序,為一典型的數據採集常式。其中bioscom()函數初始化COM1(此函數實際調用BIOS
INT
14H中斷0號功能)。這樣在程序中就避免了具體設置波特率因子等繁瑣工作,只需直接訪問發送/接收寄存器(3F8H)和線路狀態寄存

⑨ c語言串口通訊過程

分接收端和發送端。
接收端:
1·打開com1埠
fd=fopen("/dev/ttys0",方式);
2·取得當前串口值,保存到結構體變數oldtio
tcgetattr(fd,&oldtio);
3·串口結構體變數newtio清0.
bzero(&newtio,sizeof(newtio))
4·設置串口參數
主要設置比特率、是否忽略奇偶校驗錯誤,啟用正規模式等等。
接收端
1·打開com埠
2·取得當前串口值
3·串口結構體變數清0
4·設置串口參數。

⑩ 如何用C語言控制計算機串口

基本方法是使用CreateFile來建立一個串口文件,然後用overlap的方式進行讀寫

#define SERAIL_PORT_BUF_MAX (1024*8)
typedef HRESULT (*PFN_CMD_PARSE_DATA)(HANDLE hParseApp, LPCSTR szRspCmd, int nCmdLen);

class CUsbSrvApp// : public CWinApp
{
public:
CUsbSrvApp();
~CUsbSrvApp();
BOOL OnSendData(const char *szBuf, int nLen);// 發送數據
int ComConnect(CString strPort); // 連接COM口
HANDLE OpenComPort(CString strPort, int nBaudRate, int nDataBits, int nStopBits, int nParity, int nFlowCtrlType); // 打開串口
void Close(); // 關閉串口

HANDLE m_hCom;
BOOL m_bConnected;
OVERLAPPED m_OverlappedRead;
OVERLAPPED m_OverlappedWrite;
CWinThread *m_pThread;

PFN_CMD_PARSE_DATA m_pRspCmdFunc; // 用來處理接受數據的CALLBACK
HANDLE m_hParseApp;
};

CUsbSrvApp::CUsbSrvApp()
{
// TODO: add construction code here,
// Place all significant initialization in InitInstance
m_bConnected = false;
m_hCom = NULL;
m_pRspCmdFunc = NULL;
}

CUsbSrvApp::~CUsbSrvApp()
{
}
//打開串口通信,並返回串口句柄
HANDLE CUsbSrvApp::OpenComPort(CString strPortName,
int nBaudRate,
int nDataBits,
int nStopBits,
int nParity,
int nFlowCtrlType)
{
DCB dcb;
COMMTIMEOUTS CommTimeOuts ;
COMMCONFIG ComConfig;
HANDLE hComPort;

CString strPort;
strPort.Format("\\\\.\\%s",strPortName); // COM口的文件名應該是 \\.\COMXX

//打開窗口其實就是創建一個文件
hComPort = CreateFile(strPort,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL|FILE_FLAG_OVERLAPPED,
NULL);

if (INVALID_HANDLE_VALUE == hComPort)
return INVALID_HANDLE_VALUE;

// 設置一些COM口通訊參數和OVERLAP
CommTimeOuts.ReadIntervalTimeout = -1;
CommTimeOuts.ReadTotalTimeoutConstant = 0;
CommTimeOuts.ReadTotalTimeoutMultiplier = 0;
CommTimeOuts.WriteTotalTimeoutConstant = 0;
CommTimeOuts.WriteTotalTimeoutMultiplier = 0x1388;
SetCommTimeouts( m_hCom, &CommTimeOuts ) ;

SetDefaultCommConfig(strPortName, &ComConfig, sizeof(COMMCONFIG));
GetCommState(m_hCom, &dcb ) ;

dcb.BaudRate = nBaudRate;
dcb.ByteSize = nDataBits;
dcb.StopBits = nStopBits;
dcb.fParity = (NOPARITY != nParity);
dcb.Parity = nParity;
//set the receive char
dcb.EvtChar = 0x0D;

switch(nFlowCtrlType)
{
case 0: //no flow control
break;

case 1://HARD_FLOW_CTRL:

dcb.fOutxCtsFlow = TRUE;
dcb.fOutxDsrFlow = TRUE;
dcb.fDtrControl = DTR_CONTROL_DISABLE;
dcb.fDsrSensitivity = TRUE;
dcb.fRtsControl = RTS_CONTROL_TOGGLE;
break;

case 2://SOFT_FLOW_CTRL:
dcb.fOutX = TRUE;
dcb.fInX = TRUE;
break;
}
BuildCommDCB(_T("baud=115200 parity=N data=8 stop=1"),&dcb);
SetCommState(hComPort, &dcb ) ;
SetCommMask(hComPort, 0);
SetCommMask(hComPort, EV_RXCHAR|EV_CTS|EV_DSR|EV_RLSD|EV_RING);
SetupComm( hComPort, SERAIL_PORT_BUF_MAX,SERAIL_PORT_BUF_MAX) ;
//clear read and write buffer
PurgeComm( hComPort, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR );

return hComPort;
}

void CUsbSrvApp::Close()
{
if(m_bConnected)
{
m_bConnected = false;
CloseHandle(m_hCom);
m_hCom = NULL;
}
}

// 這個線程是監視串口數據,一旦有數據則讀取並調用CALLBACK通知客戶端
UINT ReceiveComData(LPVOID pParam)
{
CUsbSrvApp *pUsbSrv = (CUsbSrvApp *)pParam;
HANDLE hComPort = pUsbSrv->m_hCom;

DWORD dwEvtMask=0;
DWORD dwErrorFlags;

SetCommMask( hComPort, EV_RXCHAR);
OVERLAPPED osRead;
osRead.hEvent = CreateEvent(NULL,FALSE,FALSE,NULL);
DWORD dwTransfer = 0;
while(pUsbSrv->m_bConnected)
{
if( !WaitCommEvent( hComPort, &dwEvtMask,&osRead))
{
if( GetLastError()== ERROR_IO_PENDING)
{
WaitForSingleObject(osRead.hEvent, INFINITE);
if(dwEvtMask&EV_RXCHAR==EV_RXCHAR)
{
COMSTAT ComStat={0} ;
DWORD dwReadLen = 0;
DWORD dwBytesRead = 0;
DWORD dwTotalLen = 0;
ClearCommError(hComPort, &dwErrorFlags, &ComStat );
dwTotalLen = ComStat.cbInQue;
dwReadLen = (SERAIL_PORT_BUF_MAX > dwTotalLen)?dwTotalLen:SERAIL_PORT_BUF_MAX;

BYTE *pBuf = new BYTE[dwTotalLen+1];
memset(pBuf, 0 , dwTotalLen+1);
DWORD nReadBufLen=0;
while(dwTotalLen>0)
{

if(FALSE == ReadFile( hComPort, pBuf+nReadBufLen,dwReadLen, &dwBytesRead,&pUsbSrv->m_OverlappedRead))
{
if(GetLastError() == ERROR_IO_PENDING)
{
GetOverlappedResult(hComPort,&osRead, &dwTransfer, TRUE );
}
break;
}

nReadBufLen +=dwBytesRead;
dwTotalLen -=dwBytesRead;
dwReadLen -= dwBytesRead;
dwReadLen = (SERAIL_PORT_BUF_MAX>dwReadLen)?dwReadLen:SERAIL_PORT_BUF_MAX;
}
if(pUsbSrv->m_pRspCmdFunc!=NULL&&nReadBufLen!=0)
{
pUsbSrv->m_pRspCmdFunc(pUsbSrv->m_hParseApp, (char*)pBuf,nReadBufLen);
}

delete pBuf;

ClearCommError(hComPort, &dwErrorFlags, &ComStat );
int len =0;//= m_retList.GetSize();
}//endif if(dwEvtMask&EV_RXCHAR==EV_RXCHAR)
}//endif if( GetLastError()== ERROR_IO_PENDING)
}//endif if( !WaitCommEvent( hComPort, &dwEvtMask,&o))
else
{
if(GetLastError() == ERROR_IO_PENDING) {
GetOverlappedResult(hComPort, &osRead, &dwTransfer, TRUE ); // sleep thread
}
}
Sleep(1);
} //endwhile while(m_bConnected)
return 0;
}

int CUsbSrvApp::ComConnect(CString strPort)
{
int nBaudRate = 115200;
int nDataBits = 8;
int nStopBits = 1;
int nParity = 0;
int nFlowCtrl = 1;

if (NULL != m_hCom || m_bConnected)
{
return 0;
}
m_hCom = OpenComPort(strPort,nBaudRate,nDataBits,nStopBits,nParity,nFlowCtrl);
if( INVALID_HANDLE_VALUE == m_hCom)
{
m_hCom = NULL;
return 0;
}
memset( &m_OverlappedRead, 0, sizeof( OVERLAPPED ) );
memset( &m_OverlappedWrite, 0, sizeof( OVERLAPPED ) );
m_OverlappedRead.hEvent = CreateEvent( NULL, TRUE, FALSE, NULL );
m_OverlappedWrite.hEvent = CreateEvent( NULL, TRUE, FALSE, NULL );

m_pThread = AfxBeginThread( ReceiveComData,(void*)this,THREAD_PRIORITY_NORMAL,0,CREATE_SUSPENDED ,NULL );

if( NULL == m_pThread )
{
CloseHandle( m_hCom );
m_hCom = NULL;
return FALSE;
}
else
{
m_bConnected = TRUE;
m_pThread->ResumeThread( );
}

return TRUE;
}

int CUsbSrvApp::OnSendData(const char *szBuf, int nLen)
{
BOOL bWriteStat;
BOOL bWrite = TRUE;
DWORD dwBytesWrite = 0;
DWORD dwBytesWritten = 0;
int dwByteswrittenTotal = 0;

if (NULL == m_hCom)
return 0;

int nSentTimes=0;

while(dwByteswrittenTotal<nLen&&nSentTimes<10)
{
nSentTimes++;
dwBytesWrite = nLen-dwByteswrittenTotal;
bWriteStat = WriteFile( m_hCom, szBuf+dwByteswrittenTotal, dwBytesWrite, &dwBytesWritten, &m_OverlappedWrite );

if( !bWriteStat)
{
if ( GetLastError() == ERROR_IO_PENDING )
{
dwBytesWritten = 0;
bWrite = FALSE;
}
}
if (!bWrite)
{
bWrite = TRUE;

bWriteStat = GetOverlappedResult(m_hCom, // Handle to COMM port
&m_OverlappedWrite, // Overlapped structure
&dwBytesWritten, // Stores number of bytes sent
TRUE); // Wait flag
//deal with the error code
}
dwByteswrittenTotal += dwBytesWritten;
}
if(dwByteswrittenTotal<nLen)
return 0;
else
return 1;
}