程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> 簡單的串口通信程序控制光源,串口通信控制光源

簡單的串口通信程序控制光源,串口通信控制光源

編輯:關於C語言

簡單的串口通信程序控制光源,串口通信控制光源


本程序采用簡單的同步串行通信,分為幾個階段:

1、打開串口

2、配置串口

3、設置串口輸入輸出緩存區大小

4、設置串口讀寫超時(若不設置超時,讀寫時會等待讀寫函數返回)

5、發送字符串(每次發送前清空發送緩存區)

6、接收字符(每次接收前清空接收緩存區)

7、關閉串口

 

bmLightComm.h

 1 #pragma once
 2 #include "stdio.h"
 3 #include "stdbool.h"
 4 #include "windows.h"
 5 #include "tchar.h"
 6 
 7 class bmLightComm
 8 {
 9 public:
10     bmLightComm();
11     ~bmLightComm();
12     bool openPort(const char* portNum);
13     bool setupPort(DWORD baudrate, BYTE bytesize, BYTE stopbits, DWORD fparity, BYTE parity);
14     bool setBufferAreaSize(DWORD inQueue, DWORD outQueue);
15     bool setComTimeout(DWORD readinttim, DWORD readToltimM, DWORD readToltimC,DWORD writeToltimM, DWORD writeToltimC);
16     bool purgePort(DWORD flags);
17     bool closePort();
18 
19     bool sendBuff(const char* datas, unsigned int len);
20     char readBuff();
21 
22 private:
23     HANDLE mCom;
24 };

 

bmLightComm.cpp

  1 #include "bmLightComm.h"
  2 #include <iostream>
  3 
  4 
  5 bmLightComm::bmLightComm()
  6 {
  7 }
  8 
  9 
 10 bmLightComm::~bmLightComm()
 11 {
 12 }
 13 
 14 bool bmLightComm::openPort(const char* portNum)
 15 {
 16     printf("open the com %s\n", portNum);
 17 
 18     mCom = CreateFileA(portNum, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
 19     std::cout << GetLastError() << std::endl;
 20     if (mCom == INVALID_HANDLE_VALUE)
 21     {
 22         printf("failed to open com %s %d\n", portNum,  GetLastError());
 23         return false;
 24     }
 25     else
 26     {
 27         printf("cam %s opened\n", portNum);
 28         return true;
 29     }
 30 }
 31 
 32 bool bmLightComm::setupPort(DWORD baudrate, BYTE bytesize, BYTE stopbits, DWORD fparity, BYTE parity)
 33 {
 34     printf("set up port\n");
 35 
 36     DCB mDcb;
 37     if (!GetCommState(mCom, &mDcb))
 38     {
 39         printf("get com state failed %d\n", GetLastError());
 40         return false;
 41     }
 42 
 43     mDcb.BaudRate = baudrate;
 44     mDcb.fParity = fparity;
 45     mDcb.Parity = parity;
 46     mDcb.StopBits = stopbits;
 47     mDcb.ByteSize = bytesize;
 48 
 49     if (!SetCommState(mCom, &mDcb))
 50     {
 51         printf("failed to set up com state %d\n", GetLastError());
 52         return false;
 53     }
 54     else
 55     {
 56         printf("com set up complete\n");
 57         return true;
 58     }
 59 }
 60 
 61 bool bmLightComm::setBufferAreaSize(DWORD inQueue, DWORD outQueue)
 62 {
 63     if (!SetupComm(mCom, inQueue, outQueue))
 64     {
 65         printf("failed to set the buffer size %d\n", GetLastError());
 66         return false;
 67     }
 68     else
 69     {
 70         printf("com set buffer size complete\n");
 71         return true;
 72     }
 73 }
 74 
 75 bool bmLightComm::setComTimeout(DWORD readinttim, DWORD readToltimM, DWORD readToltimC, DWORD writeToltimM, DWORD writeToltimC)
 76 {
 77     COMMTIMEOUTS TimeOuts;
 78     TimeOuts.ReadIntervalTimeout = readinttim;
 79     TimeOuts.ReadTotalTimeoutMultiplier = readToltimM;
 80     TimeOuts.ReadTotalTimeoutConstant = readToltimC;
 81     TimeOuts.WriteTotalTimeoutMultiplier = writeToltimM;
 82     TimeOuts.WriteTotalTimeoutConstant = writeToltimC;
 83 
 84     if (!SetCommTimeouts(mCom,&TimeOuts))
 85     {
 86         printf("failed to set time out %d\n", GetLastError());
 87         return false;
 88     }
 89     else
 90     {
 91         printf("set time out complete\n");
 92         return true;
 93     }
 94 }
 95 
 96 bool bmLightComm::purgePort(DWORD flags)
 97 {
 98     //PURGE_TXCLEAR,清空發送緩沖區;PURGE_RXCLEAR,清空接收緩沖區;
 99     if (!PurgeComm(mCom, flags))
100     {
101         printf("failed to purge com %d\n", GetLastError());
102         return false;
103     }
104     else
105     {
106         printf("purge com complete\n");
107         return true;
108     }
109 }
110 
111 bool bmLightComm::closePort()
112 {
113     this->purgePort(PURGE_TXCLEAR | PURGE_RXCLEAR);
114 
115     if (!CloseHandle(mCom))
116     {
117         printf("failed to close port %d\n", GetLastError());
118         return false;
119     }
120     else
121     {
122         printf("close port complete\n");
123         return true;
124     }
125 }
126 
127 bool bmLightComm::sendBuff(const char* datas, unsigned int len)
128 {
129     this->purgePort(PURGE_TXCLEAR);
130 
131     DWORD pWrite;
132     if (!WriteFile(mCom, datas, sizeof(const char)*len, &pWrite, NULL))
133     {
134         printf("failed to write buff %d\n", GetLastError());
135         return false;
136     }
137     else
138     {
139         printf("write buff complete\n");
140         return true;
141     }
142 }
143 
144 char bmLightComm::readBuff()
145 {
146     this->purgePort(PURGE_RXCLEAR);
147 
148     char buffl;
149     DWORD pRead;
150     if (!ReadFile(mCom, &buffl, sizeof(buffl), &pRead, NULL))
151     {
152         printf("failed to read buff %d\n", GetLastError());
153         return '\0';
154     }
155     else
156     {
157         printf("read buff complete\n");
158         return buffl;
159     }
160 }

 

bmLightManage.h

 1 #pragma once
 2 #ifndef BMLIGHTMANAGE_h
 3 #define BMLIGHTMANAGE_H
 4 
 5 #include "bmLightComm.h"
 6 #include <string>
 7 #include <strstream>
 8 #include <iostream>
 9 #include <vector>
10 
11 typedef struct LightSetting
12 {
13     int deviceID;  //0~2
14     int channelID;   //0~3
15     bool turn;
16     int bright;
17 
18     LightSetting(int dID, int cID, bool tID, int bID)
19     {
20         deviceID = dID;
21         channelID = cID;
22         turn = tID;
23         bright = bID;
24     }
25 }LightSetting;
26 
27 class bmLightControl
28 {
29 public:
30     bmLightControl();
31     ~bmLightControl();
32     void Init(const char* portNum);
33     void setBright(LightSetting ligtemp);
34     void sendBright();
35 
36 private:
37     bmLightComm mCtrl;
38     std::string ch[4];
39 };
40 
41 class bmLightManage
42 {
43 public:
44     bmLightManage();
45     ~bmLightManage();
46     void Init(const char* portNum0, const char* portNum1, const char* portNum2);
47     void setBright(std::vector<LightSetting> ligtemp);
48 
49 private:
50     bmLightControl mCon[3];
51 };
52 
53 
54 #endif

 

bmLightManage.cpp

 1 #include "bmLightManage.h"
 2 
 3 bmLightControl::bmLightControl()
 4 {
 5     ch[0] = ch[1] = ch[2] = ch[3] = "000F";
 6 }
 7 
 8 bmLightControl::~bmLightControl()
 9 {
10     mCtrl.closePort();
11 }
12 
13 void bmLightControl::Init(const char* portNum)
14 {
15     mCtrl.openPort(portNum);
16     mCtrl.setupPort(19200, 8, ONESTOPBIT, FALSE, NOPARITY);
17     mCtrl.setBufferAreaSize(1024, 1024);
18     //mCtrl.setComTimeout(5, 5, 5, 5, 5);
19 }
20 
21 void bmLightControl::setBright(LightSetting ligtemp)
22 {
23     std::strstream ss;
24     std::string str;
25     ss << ligtemp.bright;
26     ss >> str;
27     if (str.length() == 1)
28     {
29         str = "00" + str;
30     }
31     else if (str.length() == 2)
32     {
33         str = "0" + str;
34     }
35 
36     ch[ligtemp.channelID] = str + (ligtemp.turn ? "T" : "F");
37 }
38 
39 void bmLightControl::sendBright()
40 {
41     std::string all = "S" + ch[0] + ch[1] + ch[2] + ch[3] + "C#";
42     mCtrl.sendBuff(all.c_str(), all.length() + 1);
43 
44     std::cout << mCtrl.readBuff() << std::endl;
45 }
46 
47 bmLightManage::bmLightManage()
48 {
49 }
50 
51 
52 bmLightManage::~bmLightManage()
53 {
54 }
55 
56 void bmLightManage::Init(const char* portNum0, const char* portNum1, const char* portNum2)
57 {
58     mCon[0].Init(portNum0);
59     mCon[1].Init(portNum1);
60     mCon[2].Init(portNum2);
61 }
62 
63 void bmLightManage::setBright(std::vector<LightSetting> ligtemp)
64 {
65     for (int i = 0; i < ligtemp.size(); i++)
66     {
67         mCon[ligtemp[i].deviceID].setBright(ligtemp[i]);
68     }
69 
70     for (int i = 0; i < 3; i++)
71     {
72         mCon[i].sendBright();
73     }
74 }

 

qttestcomm.h

 1 #ifndef QTTESTCOMM_H
 2 #define QTTESTCOMM_H
 3 
 4 #include <QtWidgets/QWidget>
 5 #include "ui_qttestcomm.h"
 6 #include "bmLightManage.h"
 7 #include <QSlider>
 8 #include <QVBoxLayout>
 9 #include <QDebug>
10 
11 class qttestcomm : public QWidget
12 {
13     Q_OBJECT
14 
15 public:
16     qttestcomm(QWidget *parent = 0);
17     ~qttestcomm();
18 
19 private:
20     Ui::qttestcommClass ui;
21     QSlider mslider;
22     bmLightManage m;
23 
24     public slots:
25     void slot1(int va);
26 };
27 
28 #endif // QTTESTCOMM_H

 

qttestcomm.cpp

 1 #include "qttestcomm.h"
 2 
 3 qttestcomm::qttestcomm(QWidget *parent)
 4     : QWidget(parent)
 5 {
 6     ui.setupUi(this);
 7     
 8     mslider.setMinimum(0);
 9     mslider.setMaximum(255);
10     QVBoxLayout *lay1 = new QVBoxLayout;
11     lay1->addWidget(&mslider);
12     this->setLayout(lay1);
13     m.Init("COM1","COM2","COM3");
14 
15     connect(&mslider, SIGNAL(valueChanged(int)), this, SLOT(slot1(int)));
16 }
17 
18 qttestcomm::~qttestcomm()
19 {
20 
21 }
22 
23 void qttestcomm::slot1(int va)
24 {
25     qDebug() << va;
26     mslider.setToolTip(QString::number(va));
27     
28     std::vector<LightSetting> ll;
29     ll.push_back(LightSetting(0, 0, true, va));
30     ll.push_back(LightSetting(0, 1, true, va));
31     ll.push_back(LightSetting(1, 0, true, va));
32     ll.push_back(LightSetting(1, 1, true, va));
33     ll.push_back(LightSetting(1, 2, true, va));
34     ll.push_back(LightSetting(1, 3, true, va));
35     ll.push_back(LightSetting(2, 0, true, va));
36     ll.push_back(LightSetting(2, 1, true, va));
37     ll.push_back(LightSetting(2, 2, true, va));
38     ll.push_back(LightSetting(2, 3, true, va));
39 
40     m.setBright(ll);
41 }

 

main.cpp

 1 #include "qttestcomm.h"
 2 #include <QtWidgets/QApplication>
 3 
 4 int main(int argc, char *argv[])
 5 {
 6     QApplication a(argc, argv);
 7     qttestcomm w;
 8     w.show();
 9     return a.exec();
10 }

 

第一次寫串口通信程序,特此記錄

參考:http://www.cnblogs.com/zahxz/archive/2012/12/24/2830535.html

附一個比較全面的串口通信程序:http://blog.csdn.net/wujian53/article/details/4090554

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved