Danh mục

giáo trình visual basic và pic phần 3

Số trang: 12      Loại file: pdf      Dung lượng: 250.48 KB      Lượt xem: 25      Lượt tải: 0    
Jamona

Hỗ trợ phí lưu trữ khi tải xuống: 1,000 VND Tải xuống file đầy đủ (12 trang) 0
Xem trước 2 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

Tham khảo tài liệu giáo trình visual basic và pic phần 3, công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả
Nội dung trích xuất từ tài liệu:
giáo trình visual basic và pic phần 3Visual Basic - Communicating With The Microcontroller Micro-News Micro-Mailing-List Understanding and Using Visual Basic Part 5 By: Jared Hoylman - Sending Data To A Microcontroller In the last article we went a little more in depth into Advanced String Parsing. These skills will be needed when receiving data from a microcontroller. All of the data received will be in a buffer and you will have to separate the data, and use it accordingly. In this article we are going to learn how to send data to a microcontroller and make the microcontroller respond to the data. For simplicity we are just going to send data to turn certain pins on and off. So lets start off with designing a communications protocol. From VB we will send an ASCII 255, as a synch byte, the pin number (0 to 15), and the on/off state (0 or 1). The microcontroller will wait for three (3) bytes of data. After these three bytes of data are received, it will then either switch the pins to their High or Low state. The VB Part To get started open Visual Basic. Start a new Standard EXE. Next go to the Project | Components... menu Check the MSComm Control. Click OK. Next double-click on the yellow phone in the toolbar to add the MSComm control to your form. Your form should look like this...http://www.rentron.com/sending_data.htm (1 of 6)5/25/2004 8:48:24 PMVisual Basic - Communicating With The Microcontroller Next we are going to add a Combo Box to our form and name it cboPinNumber. We are also going to add an Option Button to the form. Add the first one and name it optState. Select optState and press Ctrl-C to copy, and then Ctrl-V to paste. You will get a propmt like the one below... Select Yes. Now you should notice that the first option button that you created has a name of optState(0) and the second one has a name of optState(1). The number after the control name is the control array index. We will use this to our advantage later. Also add a Command button named cmdSend to the form. Go ahead and pretty up your form and add some labels to make it look better. The only requirement is that you set optState(0)s Caption property equal to Low, optState(1)s Caption property equal to High, cmdSends Caption property equal to Send, and cboPinNumbers Style property equal to (2). Your form should look something like mine, below...http://www.rentron.com/sending_data.htm (2 of 6)5/25/2004 8:48:24 PMVisual Basic - Communicating With The Microcontroller On To The Code Now that the form is set up and ready to go, we need to start adding our code to the project. The user will select a pin number from cboPinNumber, select a pin state from optState, and then click cmdSend to send the data to the microcontroller. So first of all we need to get the pin numbers into the combo box, set up the MSComm control, and select one of the pin states to start with. So lets add the following code to our form... Private Sub Form_Load() Dim Pins As Long Add the pin numbers 0 to 15 to cboPinNumber For Pins = 0 To 15 cboPinNumber.AddItem CStr(Pins) Next Pins Default to Pin 0 being selected cboPinNumber.ListIndex = 0 Default to optState(0) being selected optState(0).Value = True Use COM1 MSComm1.CommPort = 1 2400 baud, no parity, 8 data bits, 1 stop bit MSComm1.Settings = 2400,N,8,1 Open the port MSComm1.PortOpen = True End Sub Now we just need to add the code to send the data. When the user presses cmdSend, we need to do three things. Get the pin number... ...

Tài liệu được xem nhiều: