欧美一级特黄大片做受成人-亚洲成人一区二区电影-激情熟女一区二区三区-日韩专区欧美专区国产专区

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

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

編程原理

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

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

INT

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

C語(yǔ)言變成實(shí)現(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; // 定時(shí)器1工作于8位自動(dòng)重載模式, 用于產(chǎn)生波特率

TH1 = 0xFD; // 波特率9600

TL1 = 0xFD;

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

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

TR1 = 1; // 啟動(dòng)定時(shí)器1

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

while(1)

{

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

{

delayms(10); // 延時(shí)去抖動(dòng)

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

{

key_v = key_s; // 保存鍵值

proc_key(); // 鍵處理

}

}

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

{

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)

// 傳送一個(gè)字符

{

SBUF = txd;

while(!TI); // 等特?cái)?shù)據(jù)傳送

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

}

void send_str()

// 傳送字串

{

unsigned char i = 0;

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

{

SBUF = str[i];

while(!TI); // 等特?cái)?shù)據(jù)傳送

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

i++; // 下一個(gè)字符

}

}

void delayms(unsigned char ms)

// 延時(shí)子程序

{

unsigned char i;

while(ms--)

{

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

}

}

拓展資料

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

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

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

單片機(jī)串口232通信c語(yǔ)言

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

/*串口通訊 單片機(jī)晶振:11.0592MHz

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

#includecommon.h

void do_uart(void); //串口接收?qǐng)?zhí)字符時(shí)的操作

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

void sendstr(uchar *p)

{

while(*p!='\0')

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

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

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

p++; //指針加1

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

}

}

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

void int_rec(void) interrupt 4 using 2

{

if(RI) //查詢(xún)接收標(biāo)志位(有數(shù)據(jù)發(fā)送過(guò)來(lái)時(shí)置為1)

{

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

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

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

}

}

/*串口初始化*/

void com_init(uint baud)

{

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

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

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

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

TL1=TH1;

ES = 1; //開(kāi)串口中斷

TR1 = 1; //允許定時(shí)器1工作

EA = 1; //開(kāi)總中斷

}

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

{

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

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

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

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

}

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

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

如何用C語(yǔ)言控制計(jì)算機(jī)串口

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

#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); // 打開(kāi)串口

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; // 用來(lái)處理接受數(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()

{

}

//打開(kāi)串口通信,并返回串口句柄

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

//打開(kāi)窗口其實(shí)就是創(chuàng)建一個(gè)文件

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;

}

}

// 這個(gè)線(xiàn)程是監(jiān)視串口數(shù)據(jù),一旦有數(shù)據(jù)則讀取并調(diào)用CALLBACK通知客戶(hù)端

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單片機(jī) C語(yǔ)言 串口數(shù)組賦值問(wèn)題

貼上來(lái)的程序部分,修改如下:

#includereg52.h

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

#define uchar unsigned char

#define uint unsigned int

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

uint i_index=0; //接收到的字符個(gè)數(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個(gè)字符后顯示數(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]);

}

}

}

文章題目:串口函數(shù)c語(yǔ)言,串口通信c語(yǔ)言程序
文章地址:http://aaarwkj.com/article24/dsspeje.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)建站、關(guān)鍵詞優(yōu)化移動(dòng)網(wǎng)站建設(shè)、微信公眾號(hào)、全網(wǎng)營(yíng)銷(xiāo)推廣、用戶(hù)體驗(yàn)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

網(wǎng)站優(yōu)化排名
日韩在线不卡中文字幕 | 日韩欧美亚洲一区二区| 亚洲伊人久久一区二区| 欧美精品一区二区三区黄片| 丝袜美腿一区二区三区| 九九久久九九精美视频| 亚洲精品一区二区激情| 亚洲精品成av人在线观看| 日本一区二区三区加勒比| 六月丁香花五月婷婷| 一区二区三区毛片av网站| 欧美日韩精品一区二区在线播放| 三级精品一区二区三区| 亚洲国产成人精品女人久久久′| 久久青草视频在线观看 | 91制片国产在线观看| 国产三级视频在线观看视频| 成人午夜欧美熟妇小视频| 国产成人亚洲精品乱码| 日韩av在线专区观看| 91伊人日本在线视频| 精品福利视频蜜臀91| 岛国av在线免费观看| 欧美日韩亚洲一区二区搜索| 人妻鲁丝一区二区三区| 亚洲综合激情另类专区| 日本在线高清不卡免费播放| 欧美亚洲精品在线观看| 蜜臀一区二区三区精品免费| 日韩一区二区三区不卡| 91日韩人妻一区二区三区| 亚洲视频在线视频看视频在线| 欧美精品亚洲精品日韩| 精品久久人人做爽综合| 日本欧美精品一区二区精选| 日韩精品伦理中文字幕| 亚洲av精二区三区四区| 国产精品亚洲综合制服日韩| 午夜久久精品国产亚洲av| 日韩人妻有码中文字幕| 亚洲国产中文日韩欧美在线|