真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

串口函數(shù)c語言,串口通信c語言程序

c語言怎么實現(xiàn)串口通信

編程原理

創(chuàng)新互聯(lián)建站是一家專注于成都網(wǎng)站設(shè)計、成都網(wǎng)站建設(shè)與策劃設(shè)計,黃南州網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)建站做網(wǎng)站,專注于網(wǎng)站建設(shè)十多年,網(wǎng)設(shè)計領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:黃南州等地區(qū)。黃南州做網(wǎng)站價格咨詢:18982081108

程序1為查詢通信方式接口程序,為一典型的數(shù)據(jù)采集例程。其中bioscom()函數(shù)初始化COM1(此函數(shù)實際調(diào)用BIOS

INT

14H中斷0號功能)。這樣在程序中就避免了具體設(shè)置波特率因子等繁瑣工作,只需直接訪問發(fā)送/接收寄存器(3F8H)和線路狀態(tài)寄存

C語言變成實現(xiàn)串口收發(fā)數(shù)據(jù)

#include reg51.h

#include intrins.h

unsigned char key_s, key_v, tmp;

char code str[] = "welcome!??\n\r";

void send_str();

bit scan_key();

void proc_key();

void delayms(unsigned char ms);

void send_char(unsigned char txd);

sbit K1 = P1^4;

main()

{

TMOD = 0x20; // 定時器1工作于8位自動重載模式, 用于產(chǎn)生波特率

TH1 = 0xFD; // 波特率9600

TL1 = 0xFD;

SCON = 0x50; // 設(shè)定串行口工作方式

PCON = 0xef; // 波特率不倍增

TR1 = 1; // 啟動定時器1

IE = 0x0; // 禁止任何中斷

while(1)

{

if(scan_key()) // 掃描按鍵

{

delayms(10); // 延時去抖動

if(scan_key()) // 再次掃描

{

key_v = key_s; // 保存鍵值

proc_key(); // 鍵處理

}

}

if(RI) // 是否有數(shù)據(jù)到來

{

RI = 0;

tmp = SBUF; // 暫存接收到的數(shù)據(jù)

P0 = tmp; // 數(shù)據(jù)傳送到P0口

send_char(tmp); // 回傳接收到的數(shù)據(jù)

}

}

}

bit scan_key()

// 掃描按鍵

key_s = 0x00;

key_s |= K1;

return(key_s ^ key_v);

}

void proc_key()

// 鍵處理

{

if((key_v 0x01) == 0)

{ // K1按下

send_str(); // 傳送字串"welcome!...

}

}

void send_char(unsigned char txd)

// 傳送一個字符

{

SBUF = txd;

while(!TI); // 等特數(shù)據(jù)傳送

TI = 0; // 清除數(shù)據(jù)傳送標(biāo)志

}

void send_str()

// 傳送字串

{

unsigned char i = 0;

while(str[i] != '\0')

{

SBUF = str[i];

while(!TI); // 等特數(shù)據(jù)傳送

TI = 0; // 清除數(shù)據(jù)傳送標(biāo)志

i++; // 下一個字符

}

}

void delayms(unsigned char ms)

// 延時子程序

{

unsigned char i;

while(ms--)

{

for(i = 0; i 120; i++);

}

}

拓展資料

C語言是一門通用計算機編程語言,應(yīng)用廣泛。C語言的設(shè)計目標(biāo)是提供一種能以簡易的方式編譯、處理低級存儲器、產(chǎn)生少量的機器碼以及不需要任何運行環(huán)境支持便能運行的編程語言。

盡管C語言提供了許多低級處理的功能,但仍然保持著良好跨平臺的特性,以一個標(biāo)準(zhǔn)規(guī)格寫出的C語言程序可在許多電腦平臺上進(jìn)行編譯,甚至包含一些嵌入式處理器(單片機或稱MCU)以及超級電腦等作業(yè)平臺。

二十世紀(jì)八十年代,為了避免各開發(fā)廠商用的C語言語法產(chǎn)生差異,由美國國家標(biāo)準(zhǔn)局為C語言制定了一套完整的美國國家標(biāo)準(zhǔn)語法,稱為ANSI C,作為C語言最初的標(biāo)準(zhǔn)。目前2011年12月8日,國際標(biāo)準(zhǔn)化組織(ISO)和國際電工委員會(IEC)發(fā)布的C11標(biāo)準(zhǔn)是C語言的第三個官方標(biāo)準(zhǔn),也是C語言的最新標(biāo)準(zhǔn),該標(biāo)準(zhǔn)更好的支持了漢字函數(shù)名和漢字標(biāo)識符,一定程度上實現(xiàn)了漢字編程。

單片機串口232通信c語言

我寫的串口通信代碼比較長,給幾個關(guān)鍵函數(shù)你吧,可實現(xiàn)PC與單片機的雙工通信

/*串口通訊 單片機晶振:11.0592MHz

T1工作于方式2 波特率=2^smod*fosc/32/12/(256-X)*/

#includecommon.h

void do_uart(void); //串口接收執(zhí)字符時的操作

/*串口發(fā)送字符串 */

void sendstr(uchar *p)

{

while(*p!='\0')

{ SBUF=*p; //待發(fā)送的數(shù)據(jù)寫入緩沖區(qū)

while(!TI); //等待發(fā)送完成

TI=0; //清零發(fā)送標(biāo)志位

p++; //指針加1

cnt_s++; //發(fā)送計數(shù)

}

}

/*串口接收中斷函數(shù)*/

void int_rec(void) interrupt 4 using 2

{

if(RI) //查詢接收標(biāo)志位(有數(shù)據(jù)發(fā)送過來時置為1)

{

RI = 0; //接收標(biāo)志位清零

// sendchar(SBUF);//讓從電腦上傳到單片機的數(shù)據(jù),傳回的電腦顯示

do_uart();//對接受到得字符操作

}

}

/*串口初始化*/

void com_init(uint baud)

{

SCON = 0x50; //串口工作方式為1,串行允許接受

TMOD = 0x21; //定時器1工作在方式2 定時器0工作在方式1

//PCON = 0x80; //SMOD = 1; 波特率加倍

TH1=256-fosc/32/12/baud;

TL1=TH1;

ES = 1; //開串口中斷

TR1 = 1; //允許定時器1工作

EA = 1; //開總中斷

}

void sendchar(uchar uart_dat) //串口發(fā)送字符函數(shù)

{

SBUF = uart_dat; //待發(fā)送的數(shù)據(jù)寫入緩沖區(qū)

while(!TI); //等待發(fā)送完成

TI = 0; //清零發(fā)送標(biāo)志位

cnt_s++; //發(fā)送計數(shù)

}

C語言中如何對串口進(jìn)行操作

C語言會有操作串口的庫函數(shù)的,按照串口庫函數(shù)標(biāo)識實現(xiàn)調(diào)用就可以了。

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

基本方法是使用CreateFile來建立一個串口文件,然后用overlap的方式進(jìn)行讀寫

#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);// 發(fā)送數(shù)據(jù)

int ComConnect(CString strPort); // 連接COM口

HANDLE OpenComPort(CString strPort, int nBaudRate, int nDataBits, int nStopBits, int nParity, int nFlowCtrlType); // 打開串口

void Close(); // 關(guān)閉串口

HANDLE m_hCom;

BOOL m_bConnected;

OVERLAPPED m_OverlappedRead;

OVERLAPPED m_OverlappedWrite;

CWinThread *m_pThread;

PFN_CMD_PARSE_DATA m_pRspCmdFunc; // 用來處理接受數(shù)據(jù)的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口的文件名應(yīng)該是 \\.\COMXX

//打開窗口其實就是創(chuàng)建一個文件

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;

// 設(shè)置一些COM口通訊參數(shù)和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;

}

}

// 這個線程是監(jiān)視串口數(shù)據(jù),一旦有數(shù)據(jù)則讀取并調(diào)用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(dwEvtMaskEV_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(dwTotalLen0)

{

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_MAXdwReadLen)?dwReadLen:SERAIL_PORT_BUF_MAX;

}

if(pUsbSrv-m_pRspCmdFunc!=NULLnReadBufLen!=0)

{

pUsbSrv-m_pRspCmdFunc(pUsbSrv-m_hParseApp, (char*)pBuf,nReadBufLen);

}

delete pBuf;

ClearCommError(hComPort, dwErrorFlags, ComStat );

int len =0;//= m_retList.GetSize();

}//endif if(dwEvtMaskEV_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(dwByteswrittenTotalnLennSentTimes10)

{

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(dwByteswrittenTotalnLen)

return 0;

else

return 1;

}

51單片機 C語言 串口數(shù)組賦值問題

貼上來的程序部分,修改如下:

#includereg52.h

#includestdio.h //打印到串口函數(shù)

#define uchar unsigned char

#define uint unsigned int

uchar date_zi[4][11]; //存放接收到的數(shù)據(jù)

uint i_index=0; //接收到的字符個數(shù)

uint i=0; //i字符用于循環(huán)

unsigned int xia = 0; //用于保存到數(shù)據(jù)的下標(biāo)

void fh_zi(uchar a) //返回字符到串口

{

ES=0;

SBUF=a;

while(!TI);

TI=0;

ES=1;

}

void init()

{

TMOD=0x20;

TH1=0xfd;

TL1=0Xfd;

//TH0=(65536-45872)/256;

//TL0=(65536-45872)%256;

TR1=1;

SM0=0;

SM1=1;

REN=1;

EA=1;

ES=1;

}

void main()

{

i_index=0;

xia=2;

init();

while(1);

}

void ser() interrupt 4 //串口

{

if(RI == 1)

{

RI=0;

date_zi[xia][i_index] = SBUF;

i_index++;

if(i_index10) //接受完10個字符后顯示數(shù)組所有內(nèi)容

{

i_index=0;

for(i=0;i11;i++) //顯示數(shù)組中所有內(nèi)容

fh_zi(date_zi[0][i]);

for(i=0;i11;i++)

fh_zi(date_zi[1][i]);

for(i=0;i11;i++)

fh_zi(date_zi[2][i]);

for(i=0;i11;i++)

fh_zi(date_zi[3][i]);

}

}

}


當(dāng)前名稱:串口函數(shù)c語言,串口通信c語言程序
當(dāng)前URL:http://weahome.cn/article/dsspeje.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部