Medium-term summary of hardware comprehensive training

Source: Internet
Author: User
Tags readfile

Small semester A course called hardware comprehensive training, has been to the second putting classes, at first I thought it was what water to not the class, to make a report on the finished. When I saw the internet of things teaching and research platform, it changed my view. Six sensors, a Linux gateway, the sensor sends data through the ZigBee network, and the gateway receives it and displays it in a graphical interface. And what we have to do is to simulate some of the functions on this basis so that they can be used in smart homes. For example, there is a temperature and humidity sensor, we can use this sensor to monitor the temperature and humidity of the family, once found that the home is too high, you can control the air conditioning automatically open. This is a computer science and technology professional software dog first time exposure to the process of hardware. (it is said that only the University of this major in the National college has opened this course ...) )

Gossip about here, today is just a week from the first class, the distance I got the experimental platform total four days, the platform is familiar with almost, after a toss, roughly understand the ZigBee network work, and code writing, tomorrow can start to really develop smart home applications.

July 7 Afternoon:

This is not an already ready to use the platform, we do not have to encode the hardware, I just put the sensor data collected through the serial port to read to my computer is not finished. (PS: Platform Gateway and ZigBee Coordinator module is communication through the serial port) gateway is up-cup6410 model, sensors are from Beijing Bo Chong Prosperity Technology Co., Ltd. Baidu search C language Reading serial code, in fact, this is really not good search, the cost of a great effort to find, may be my key is not very right, it may be that I understand the C language is not deep enough, found that the direct use of CreateFile to read the serial port as a file. But the number of my two years of programming experience, read files have only used the file class ... This part of the code, convenient for beginners like me.

<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" >handle test = CreateFile (_t ("COM3"), generic_read, 0, NULL, open_existing, NULL, NULL);   Open the serial port if (test = = INVALID_HANDLE_VALUE)    //Open failed {cout << "open filed\n"; System ("pause"); exit (-1);} DCB *DCB = new DCB ();    The control block Getcommstate (test, DCB) used to set the serial port status;d cb->baudrate = 115200;   Baud rate dcb->bytesize = 8;        Word length dcb->parity = 0;          Parity Check Mode Dcb->foutxctsflow = false;   No hardware flow interrupt setcommstate (test, DCB);     Set serial port status DWORD readsize;dword fileSize = 1;char *buffer = new Char[filesize];char *data = new CHAR[17]; ReadFile (test, buffer, fileSize, &readsize, NULL);    From the serial reading, test is the handle of the serial port, buffer data buffers, filesize to read the size, readsize the actual read size </span>

After reading the serial port, the data is stored in buffer.

Find code to find the burnout, and then began to test, code run for a long time, the console does not output, is the baud rate error (PS: Later only know that the baud rate will also have output, but is garbled), and then I used a few common baud rate re-test, but still no output. Then, I think of that help CD, but my computer CD-ROM bit as early as I dismantled installed SSD, can only wait for the classmate back. After the classmate reminds, to the sensor to write the program, and then according to the tutorial to do the experiment.

PS: Later know, here I made two errors, first, filesize set too large, the serial port sending rate is too small, so it will be read full buffer for a long time. Second, the return value of ReadFile () is judged wrong. The call succeeds, returns nonzero, and does not return 0 successfully!!! I was all messed up, so I wouldn't have output anyway.

July 8:

Finally got the document, the experimental platform supporting tools inside there is a thing called the emulator, that is, to write the program on the sensor, according to the tutorial said the installation of the driver method is not found, open the directory found to drive only for XP system, no way, to debug the program also have to install an XP system in the virtual machine.

After the system, half a morning passed, another lesson, a morning passed, the real time to do things or in the afternoon. Open the tutorial, browse the catalog, and look at the development environment first. Originally, an IDE called IAR was needed to develop the program. Fortunately there are virtual machines, not in my machine on the toss. With a good development environment, followed by the tutorial finished my first embedded program, is to control the LED lights on the board, according to the tutorial step by step Burn to the board, very happy. Although there is little help in what is actually needed, the first step is to cross out. See catalogue Total Twenty or thirty experiments, I selected three experiments to do, a basic experiment, a sensor module experiment, a wireless communication experiment. Several experiments have nothing to say, follow the tutorial to do.

These three experiments are used in the XP system Super Terminal reading serial data, when I finished these several experiments, suddenly thought, since HyperTerminal can read the serial data, then I wrote the program must also be able to read out. Back to the WIN10 system, modify the buffer size, here because the data sent at this time is less, not I realize the data collection and transmission rate differences. Take the test, or not, and then I found the return value of the problem, after changing the return value, sure enough, it can be output. Then I suddenly think of something, in my heart startled. So, yesterday the program can also read the data, but I have the return value is mistaken. The slot, the program on the sensor has been changed by me, which means that several sensors are now unable to form a network. Lying trough, this is really iniquity ah, do not worry, there must be a way, there will be the original code of the program, look again tomorrow.

July 9

Early to get up, in the CD-ROM found another tutorial document, Crouching Groove, unexpectedly has more than 30 pages, densely full of words and code. No hurry, look slowly, there is an interface below, how to see before the same AH. First of all, when this is it, find the source code, burn into the team inside, each burned a copy, inserted in the experimental platform, I hope it can work ah, but the screen is still blank. There must be something wrong, keep looking. There is a line below, the core Board in coordinator way to compile, temperature and humidity sensor compiled in router way, other sensors compiled Enddevice way, but also to find the Sampleapp_init () function, the current sensor type of comments open, The type of the current sensor is not fully commented out, re-burn in, still not. Looking at the document, I really don't know where it went wrong. Forget, give up, re-write a gateway out, anyway the temperature and humidity of wireless data transmission has been done, the other several sensors plus should not be difficult.

Other sensors in front of the experiment, the code copied to the wireless data transmission, the original core board to the serial port to send data through the UART_SEND_STRIRNG () to send, in front of each piece with the sensor type of judgment, well, should be almost. Then write the program into the sensor. Open all switches, the serial port plug in, and no data.

Forget it, heart tired, continue tomorrow.

July 10

Forget last night did what dream, anyway is wake up after I very clearly realized that the code that was found yesterday is absolutely in the classroom to see the source code of the program. Must be where there is a problem, it must have been yesterday to make a dash. The sensor plug in, the program re-write again, to confirm that there is no error, connect all the lines, open the switch, still nothing appears, I am a bit panic, go back to look at the code, or right, then look back to the screen, looking back at the screen, lying trough, unexpectedly appeared the coordinator node, although just a solitary knot point, At least, this proves my idea is right. Do not know why, put all the nodes restarted again, the screen is blank,,, is that I did not wait for a long time yesterday?? This is too fucked up, the whole hardware experiment to get the lab box back in the dorm to do this kind of thing really fucked up. Finally, five sensor nodes appear on the screen, given that the toxic gas sensor is not available, and later experiments will discard it.

Test serial port, so many days, and back to the origin, through the serial port from the sensor to collect information. Plug in the serial port, just think of some properties of the serial port do not know how to set, look at the document, there is no say ... Regardless, with the wireless experiment with the same properties, set the baud rate, parity, etc., open the program, all garbled. What's going on, how does it send the data, open the source code, search uart_send_strirng (), and no results. That this is how to send data, look at the document, the original HAL Library, the function of sending data to the serial port into the Haluartwrite (), so that the baud rate is set in, search all the terms related to the baud rate, there is no result. Baidu Search the HAL Library initialization, also did not find useful information. Can only sacrifice, violent cycle, a few commonly used baud rate test again, finally found a not garbled, baud rate is 115200, unexpectedly is so big baud rate, also did not think. Everything is clear, the rest is to parse the data, remove useless data, leaving only the data I can use. After layers of screening, selected to the sensor node on-line, offline, temperature and humidity sensors to collect information periodically, other sensors interrupt the way to send information such a few data. The code is relatively simple, nothing to say.

The experiment also has a requirement to build a website, receive sensor data, and then get the data from the website on the phone. Think for a long time, think of the previous used Html+nodejs+mongodb a site template, the use of Post request to send data, data stored in the database, and then the front end from the database to display the data. In this case, C language write HttpPost request is not so convenient, so converted into C #, after all, before written, familiar. By the way, put the code

<span Style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;"            >public static void HttpPost (string name, String addr, String parentaddr, string state, string data) {            Encoding Dataencode = Encoding.UTF8;             String url = "Http://localhost:3000/insert";            HttpWebRequest request = httpwebrequest.create (URL) as HttpWebRequest; Request.            Method = "POST"; Request.            ContentType = "application/x-www-form-urlencoded"; String parastr = "Name=" + name + "&addr=" + addr + "&parentaddr=" + parentaddr + "&state=" + state + "&da            Ta= "+ data; byte[] Bytearr = Dataencode.            GetBytes (PARASTR); Request. ContentLength = Bytearr.            Length; Stream newstream = Request.            GetRequestStream (); Newstream.write (Bytearr, 0, Bytearr.            Length);        Newstream.close (); }</span> 

After all, just send data, do not receive the data returned by the site, so there is no httpwebresponse. Start the Web site, but also modify the background to receive the post handler. The first time with MongoDB, naturally inseparable from Baidu, a new table, the previous table all replaced with the present table, sure enough, I really witty.

July 11

Modified yesterday for the web operation of the flaw, and then the rest is to get the Web page data and processing. And there is no difficulty, the morning also on the morning of the class, there is no motivation. I am a strange person, the enthusiasm for what I will not do is particularly high, but if one thing seems to know can certainly be done, then completely into the lazy state, do not want to do anything.

But this is still homework, write it tomorrow.

July 12

Android-side development. From the computer or Web page and Android communication, good many ways, do not know which to choose, good entanglements. More tangled is if the defense teacher asked me why I chose this way I answer, anyway, I can now think of the answer is: There are a lot of options, I just choose one of them, can not always struggle to decide which option is not.

Well, I chose the Android app to access the pages and get the data instead of sending it to Android from this computer. As for why, one reason is that some time ago just finished the curriculum, some things can be used, it is so simple.

But I do not know what to do, but did not use the last page request code, as if I think the last HTTP request only post and get two ways, and I want to access the page is just a static web page, and there is no post and get form can submit the place, so it can not be used post/ GET, however Baidu searches all is the Post/get way HTTP request. Then I was thinking, what kind of request should I have, and then I went to see it with HttpWatch, mom, it was get. (can only say that I do not, to HTTP understanding is not thorough, but there are so many things to learn, how can I in two years to do everything to understand it, most still do one side of Baidu. I have been emphasizing that I have only two years of programming experience, is from the first HelloWorld I knocked out of the beginning, so do not know that now is not good, but I know that I can do better, but the heart is still there is greed and hatred. )

Know is the get way, I still did not use the last code, do not know how to think, this time the HTTP request code is this

HttpClient HttpClient = new Defaulthttpclient (); HttpGet httpget = new HttpGet ("http://192.168.1.50:3000"); Try{httpresponse HttpResponse = Httpclient.execute (httpget) ; if (Httpresponse.getstatusline (). Getstatuscode () = =) {result = Entityutils.tostring (httpresponse.getentity ()); /seestring (result); Message msg = new Message (); msg.what = 0;msg.obj = Result;handler.sendmessage (msg);}} catch (Exception e) {}

I think it is not necessary to add a few more bags for things that can be solved clearly in such a few lines. The first time I wrote the HTTP request directly into the activity of the OnCreate method, and debugging, has been thrown exception, Baidu just know, The original Android4.0 after the HTTP request must be a new thread execution, otherwise it will be abnormal, and manifest.xml also add <uses-permission android:name= " Android.permission.INTERNET "/>. This reminds me of the last time that was an asynchronous HTTP request package, no wonder I didn't encounter this problem before. Continue down, because to do real-time monitoring of the system, so to get the data every once in a while, I opened a thread to control this logic, the role of this thread is to create a thread every time to send HTTP requests to get data.

Continue debugging, found that every time you get the data, to update the content of the UI will throw an exception, Baidu search abnormal, only to find that Android is not allowed to change the UI, if you want to change the UI can only use other methods, here can search several kinds, I chose one of the handler method.

The specific way of working also did not understand very carefully, I only say the problem I encountered, using the handler method is still thrown out of the abnormal, so I can not think of its solution. The last thought is that the thread nesting layer is too many, I am using thread thread to change UI, which is not feasible, so I just write the HTTP request directly in the line thread of the control logic, then can work.

A perfect day.


Medium-term summary of hardware comprehensive training

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.