Microsoft Developer Network >
Forums Home
>
Visual Studio Express Editions Forums
>
Visual Basic Express Edition
>
Visual Basic 2008 Express Edition - how to send 2 different signal to PIC using serial port(rs232)
Visual Basic 2008 Express Edition - how to send 2 different signal to PIC using serial port(rs232)
- Hi, what i am doing now is a GUI that can send signal to a PIC to control of (turn on / turn off) function of 2 devices
For example : Light 1 and Light 2
i have 4 button for my GUI which is TURN ON/OFF Light 1 & TURN ON/OFF Light 2
what programming method i can use for this kind of application?how to programming each of this buttons?
thanks~
All Replies
- Hi,
This is one of those questions that cannot be answered, as asked. The solution totally depends on the serial protocol that the PIC programmer established to receive commands from the Host program, and how it replies. To be clear... There is no single answer. Serial data is sent from the host and received by the client (PIC processor), and interpreted by the code executing on the PIC -- there isn't any predefined way to do this.
I have a code example in my book (Visual Basic Programmer's Guide to Serial Communications 4) that does this sort of thing using a BasicStamp. The BasicStamp uses a PIC processor with a built-in BASIC interperter. There are other versions of BASIC interperters sold for PIC processors, and you might use one of those -- these interperters have built-in support for serial communications, and you can write some fairly simple code that translates data received into suitable actions. You can do exactly the same thing if you are writing the PIC code in assembler or C. At the end of the day, you write both ends of the communications channel, and choose what each serial command will mean, and what the response should be.
Dick
Dick Grier, MVP. Author of Visual Basic Programmer's Guide to Serial Communications 4. See www.hardandsoftware.net. - I agree with Dick, you need to get some knowledge about the protocol used.
Regarding SerialPort, I have written a big tutorial where you may be able to find most of the knowledge you need: http://www.innovatic.dk/knowledg/SerialCOM/SerialCOM.htm
Everything should be made as simple as possible, but not simpler. Any fool can write code that a computer can understand. Good programmers write code that humans understand. - Thanks for reply for both of you....
but what i want to do now is just the programming in the vb 2008 to send few different output signal to the PIC.
the rest action will be handle by the PIC.
since i am a newbie, i don't know how to program the VB to transmit 2 or more different signal to the PIC.
Any idea?
thanks a lot again~ - Yes. Read my tutorial - see my last post! It is all in front of your eyes if you bother to look - including a small sample program, which may get you started.
I know that the tutorial has grown to a size where it gives many beginners headache. Then it is easier just to ask again, but I have spend months writing that tutorial and digging up all the necessary information. If beginners would just use a fraction of that time trying to understand it, it could bring them very far.
If there is something in the tutorial, you don't understand, or something, which is not described, just ask and I will try to help.
Everything should be made as simple as possible, but not simpler. Any fool can write code that a computer can understand. Good programmers write code that humans understand. - Ok... thanks~
- You just make up a protocol.
For example, send the strings:
"Turn1On" & vbCr
"Turn1Off" & vbCr
"Turn2On" & vbCr
"Turn2Off" & vbCr
to do the obvious things. Then, in your PIC serial code you look for strings that resemble the above, and execute the appropriate code.
There aren't any "rules" for how such protocols should be devised, so you are on your own. You can make this simpler, or more complex (such as adding error-checking -- though that shouldn't be needed).
Then, to implement these commands, use any one of the many online examples (mine or Carsten's, for example). They all show how to open the serial port, configure it, and to send and receive data.
Dick
Dick Grier, MVP. Author of Visual Basic Programmer's Guide to Serial Communications 4. See www.hardandsoftware.net. - Hi, i had read some example and create some of the coding ... is this concept/code near to my purpose already ?
i still figuring how to modify all this code
http://img.photobucket.com/albums/v629/nicky1986/vb-1.jpg
- Edited byMartin Xie - MSFTMSFT, ModeratorMonday, November 02, 2009 2:19 AMModify HTML View code to display an illustration on your post.
- What is this? SerialPort has no TurnOffLight or TurnOnLight methods. Have you made a new class derived from SerialPort?
Everything should be made as simple as possible, but not simpler. Any fool can write code that a computer can understand. Good programmers write code that humans understand. - My best guess (only a guess), is that your Button1_Click code might look like:
Dim Buffer() As Byte = {1}
SerialPort1.Write(Buffer, 0, 1) 'turn on light????
And Button2_Click code, perhaps:
Dim Buffer() As Byte = {2}
SerialPort1.Write(Buffer, 0, 1) 'turn off light????
You'd help use if you told us in words, what command does what.
I think you may be making this more complex than is necessary.
Dick
Dick Grier, MVP. Author of Visual Basic Programmer's Guide to Serial Communications 4. See www.hardandsoftware.net. - Hoi,
why didn't you use the Printerport ?
Its much better for thins like this, and you pic can for sure easily connected to the LPT-Port.
Doei
Franz
Be a good forum member - mark posts that contain the answers to your questions or those that are helpful - Sorry for make clear for my code
The red arrow there actually is what i try to declare the "turn on light 1" / "turn off light 1" and "turn on light 2" / "turn off light 2"
but i not sure i should declare it as buffer or "turn on light 1" / "turn off light 1" and "turn on light 2" / "turn off light 2"
and
The blue arrow is the code for the button that will show "OFF" word when it on "on" state & "ON" word when it is on "off" state
and it is use to send "ON"/"OFF" signal when it is click
Trixi-N : i dint use printer port because i dint know about printer port and i dint have the printer port on my laptop.By the way,i think it is more easy to get a usb to serial cable and i had bought it. - You seem to be very confused and need to study some basics about object oriented programming and methods.
Dick has shown you the code. Why have you not tried it? Converted to the code with the red arrow you get:
Dim Buffer(1) As Byte ' You have this code already
Buffer(0) = &H1
Buffer(1) = &H2
SerialPort1.Write(Buffer, 0, 1) ' Turn light on
SerialPort1.Write(Buffer, 1, 1) ' Turn light off. Note that the offset is 1 to send &H2
Everything should be made as simple as possible, but not simpler. Any fool can write code that a computer can understand. Good programmers write code that humans understand. - http://img.photobucket.com/albums/v629/nicky1986/vb-2.jpg
Thanks a lot... i had change it.. this is my code after advise from both of you...
any comment? is it right for my purpose? - Button1_Click seems to be OK, but not Button2_click. In Button2_Click, you still use the values for button1. You need to change the offset in the Write statements.
Everything should be made as simple as possible, but not simpler. Any fool can write code that a computer can understand. Good programmers write code that humans understand. - [IMG]http://img.photobucket.com/albums/v629/nicky1986/code.jpg[/IMG]
i update it already
does the offset value i set for button2 correct? - Seems OK, but if you really don't know if the offset is correct, you have totally lost the overview of what you are doing. In that case, it is time to do some study and thinking yourself. I am here to help with specific problems - not to write the entire code for you or check every line you write.
Everything should be made as simple as possible, but not simpler. Any fool can write code that a computer can understand. Good programmers write code that humans understand. - Ok... thank you..
Actually this is part of my final year project.I,m student from electronic engineering, i just learn the very basic things of visual basic 6.0 before but it dint deal with serial port thing and i get lost when i come to visual basic 2008.
I had read the tutorial from your website but i,m still confused.
By the way,hope it can work.Thanks a lot Carsten Kanstrup & Dick Grier - The reason why you are still confused is probably because you don't take it easy step by step and go to a new chapter before you fully understand the previous one. If you rush through the tutorial, it will just leave you more confused.
I know that you may have a tight time schedule with your project, but it is extremely important that you fully understand every line you write. You should also clean-up your code and make it more readable. For example, what is the empty Button4-click subroutine doing? You have removed the Handles clause so even if you put some code into it, it will not work unless you make an AddHandler statement. Besides, it may be a good idea to rename the buttons to something more describing than just Button1, Button2 etc. I am an electronic engineer myself and it actually shocks me to see how little programming you have learned. It is certainly not what you would expect from a Danish final year student. When are you going to finish your project?
Visual Basic is probably the most simple object oriented language you can choose, but it is object oriented and you need to know what that means before you can use it. SerialPort is also very simple, but since it uses multithreading, you also have to know what this means before you can understand the necessary code - at least when it comes to the receive part. This is why most of my tutorial deals with basic knowledge about .Net before it describes SerialPort.
Everything should be made as simple as possible, but not simpler. Any fool can write code that a computer can understand. Good programmers write code that humans understand. - Thanks for your advice Carsten =)
I still got around 2 weeks for project - 2 weeks are not much. Does it work or do you still have problems?
Everything should be made as simple as possible, but not simpler. Any fool can write code that a computer can understand. Good programmers write code that humans understand. - I dint finish my PIC programming part yet... so i use "loop back" method to test my visual basic program
but i got a question that what character does visual basic send to PIC? [http://img.photobucket.com/albums/v629/nicky1986/sp1.jpg]
is it from what the result that i get from the "loop back" test? (Please refer to the photo)[http://img.photobucket.com/albums/v629/nicky1986/sp2.jpg]
Besides that, i also change some of the value of my Button 2 because i cant get result if i use the previous code
[http://img.photobucket.com/albums/v629/nicky1986/sp3.jpg]
After that, i had change the value of &H1 to 48
and the result i get from the receive box is "0"
so does what i try to change is correct ?
[http://img.photobucket.com/albums/v629/nicky1986/sp4.jpg]
I found the value using this list that i found online [http://img.photobucket.com/albums/v629/nicky1986/sp5.jpg] - You confuse ASCII text and binary data.
You have not described the communication protocol to the PIC controller and you change your code all the time so it is very difficult to to see what you actually wants to transmit to the PIC.
Since you have two lamps, it may be natural to use two bits to control them - for example the least significant bit 0000000x for lamp 1 and 000000x0 for lamp2 so that to turn lamp 1 on you send 00000001 and to turn it off you send 00000000. In the same way, to turn lamp 2 on you send 00000010 and to turn it off you send 00000000. To turn both lamps on, you send 00000011. Another possibility is to use four bits - two for each lamp - so that 000000xx controls lamp 1 and 0000xx00 controls lamp 2. To turn lamp 1 on, you can for example send 00000010 and to turn it off, you can send 00000001. In the same way, to turn lamp 2 on, you can send 00001000 and to turn it off, you can send 00000100. What do you want to do? You need to describe exactly how you want the communication to be done if I should be able to help you.
When you make a loop back test of your program and use ReadExisting, you try to display the characters as ASCII, but this is wrong. For example, 00000001 is the ASCII character SOH (Start Of Heading), which is not printable. This is why you get a strange graphical character. When you send 48 decimal = 00110000 it is printable as a 0, but if you intend to transmit binary values, it should instead be displayed as for example 30H (hex). If you want to receive and display binary values, you must use a binary read method and then convert the value to something, which is printable like it is done in my sample program. My sample program shows all binary values as a hexadecimal value. The sample program is also able to transmit hexadecimal values as well as ASCII. Why not use this test program to get hole through to the PIC?
Some remarks to consider:
The way you wants to communicate with the PIC is extremely primitive and far from what would be expected from an electronic engineer. There is no check bits and what will you do if there are 5000 process signals instead of just two lamps? I know that two weeks left may not be enough to change it to something much better, but what I would do is to use the so-called producer/consumer model known from CAN. With this model you give each process value a name. This means that you no longer transmits a value to a specific device, but just broadcast that for example the value with the name A1 now has the value 12. Any number of devices - not just one - can then grap this telegram and use the information if they like. This is how our new fieldbus system Max-i (see http://www.innovatic.dk/fieldbus.htm ) works. In such a communication protocol you need the name of the value, the value and some checkbits. If you are interested in such a communication method, please let me know. There may still be time to do it. To keep it very simple, you may for example use the 7 most significant bits of your byte as the name of the value (lamp) and the least significant bit to turn the lamp on or off. You can then use parity to add at least a little safety to the telegram. In this way, you can address 128 lamps. To turn lamp 1 on, you send 00000011 and to turn it off, you send 00000010. In the same way, to turn lamp 2 on, you send 00000101 and to turn it off, you send 00000100.
Everything should be made as simple as possible, but not simpler. Any fool can write code that a computer can understand. Good programmers write code that humans understand.- Edited byCarsten Kanstrup Saturday, November 14, 2009 12:50 PMCorrection
- Actually the loop back test part is not necessary,i create it to test because i dint finish the PIC programming part yet.
And for the sending part,i,m just confuse with what format of the data(Hex/binary/ASCII) that sent by default if i dint set anything..so that i can know what type of value i need to put into my coding.
Thanks
For the remarks, it,s good knowledge for me to learn. I'll take some times to understand it. - To send binary data, just use the only byte related send method there is: Write(Byte Array, Offset, Count). Note that the array must be a byte array! See chapter "SerialPort Class" of my tutorial for further details about data coding and decoding.
Everything should be made as simple as possible, but not simpler. Any fool can write code that a computer can understand. Good programmers write code that humans understand. - I had modify it already ..is this what you mean ?so now i am transmitting binary number to the microcontroller.
http://img.photobucket.com/albums/v629/nicky1986/byte.jpg - You still confuse almost everything and still have not told us what you want to do!
Button 1 seems all right if you intend to send binary data.
Button 2 is a mystery to me. You send the two DECIMAL values 11 and 10, but these values have the binary bit patterns 00001011 and 00001010. It doen't make sense. What do you want to do? If you want to use the 7 most significant bits of the byte as a lamp address as I recommended you, you need to send the binary values 00000011 and 00000010 for lamp 1. However, these values have the decimal representation 3 and 2 - not 11 and 10. You probably confuse decimal and binary data.
When you just type a value like 63, VB regard it as a decimal value and therefore convert it to the bit pattern 00111111. Anything internal in a computer is binary 8-bit, 16-bit, 32-bit or 64-bit data, but what you type on a keyboard and see an the screen is string (Text), so some conversion must be done. Unfortunately, there is no way to type in a binary bit pattern directly. You can do it by means of octal data or hexadecimal data, but not binary. If you wish to type the data as a hexadecimal (4-bit) values, the bit pattern 00111111 must be specified as &H3F (0011 and 1111). You can also use 3-bit octal representation and divide the bit pattern in 3-bit groups instead of 4-bit groups although it is hardly used any more. You can then specify &O77 (111 and 111). If you want the bit pattern 00111111, you therefore have 3 possibilities to specify it: 63, &H3F and &O77 - but unfortunately not &B111111.
TextBoxRcv.Text = SerialPort1.ReadByte is also madness and VB ought to give you an error message for that. SerialPort1.ReadByte returns an 8-bit byte, but TextBoxRcv.Text is a string of Char characters, that is, 16-bit unicode character. What VB does in this case is to regard the received byte as a 7-bit ASCII character, which it then converts to 16-bit Unicode according to the selected code page. You haven't read the chapter "SerialPort Class" of my tutorial, which I recommended you in my last post, have you? If you want to display the received binary value as something, which is printable, you need to convert it to two or more ASCII characters as I told you before and as it is done in my sample program.
You can't program this way. You will simply not succeed. You need to understand what binary data is, what hexadecimal data is, what decimal data is, what 7-bit ASCII text is and what 16-bit Unicode is. Before you do that, forget about programming. You will just fool around without the slightest knowledge of what you are doing and can just hope that somebody finally write the entire code for you, but nobody is able to do that before you tell us what you want to do! It is not enough that you just write: "I have modified it already ... is this what you mean? " What have you modified? The communication protocol or just a minor detail? If you have modified the communication protocol as I recommended, where is the parity check (or a better error detection)?
There are probably only a few days left to finish your project. If I should be able to help you before the deadline, maybe it is a good idea to visit this forum a little more frequently than you do for the moment and try to do and read what I recommend you!
Everything should be made as simple as possible, but not simpler. Any fool can write code that a computer can understand. Good programmers write code that humans understand.- Edited byCarsten Kanstrup Sunday, November 22, 2009 1:48 PMAddition
- Ok ...thanks for your guides.I need to take some time to work on it.Recently busy with assignments.
By the way, i had get permission to extend 1 more week to do on my project.
Let me clarify what i want to do.What i want to do just send 4 different signal( light 1 on/off, Light 2 On/off ) to the PIC microcontroller . It just that simple.
But i had to spend so much time to do it because i get confuse with the use and meaning of keyword when i reading other's tutorial and your tutorial.
For Button 2, i actually want to send the binary code 00000011 and 00000010.. but after i type in then the 0 in front disappear itself.I think i should put the " " ,is it? so it should be "00000011" and "00000010" ?
Thanks again. Have a nice day~
- "xxx" is used to specify a string. You really need some basic knowledge about the VB syntax - and I think that it is time that you begin to read what I write a little more carefully! I told you that there are 3 possiblities to specify the wanted input data - decimal, octal or hexadecimal. If you want the binary bit pattern 00000011, you can specify it as decimal (3), Hexadecimal (&H3) or octal (&O3). Because the value is less than 8, the value is 3 no matter which representation you choose.
You want to send 4 different signals - fine, but what is your communication protocol? There are numerous ways to transmit 4 signals, but of course it must match the way your PIC controller interpret the telegram. If you want to be an electronic engineer, start to behave like one! When you write your project, the absolutely first thing to do when you come to the communication with the PIC is to specify the communication protocol. How data is transmitted and how you detect errors etc.? Then you can write the program according to this specification. In a big company, you may have one programmer to work on the PIC and one on the PC end. If they do not have a common specification, it will never work.
If you want to do it really professional, you can for example specify your communication protocol according to the OSI 7-layer model where you divide the communication in 7 layer. This is the way our new fieldbus Max-i is specified.
Layer 1 is the Physical Layer, which is responsible for transmitting raw bits without any particular meaning over the network. It describes the cable type, connectors, voltage levels (in your case RS-232), bit coding (in your case NRZ - Not Return to Zero ) etc.
Layer 2 is the Data Link Layer where small packages of data are converted to raw bits. This layer creates and recognizes frame boundaries etc. In your case, you can describe the asynchronous communication with start and stop bit (UART), that is, how 8-bit data are converted to raw bits for layer 1 and how the start and stop is recognized. Note that if you expand the telegram to more bytes, this layer must also specify a safe method to separate the various telegrams.
Layer 3 is the Network Layer. The purpose of this layer is to select a route between nodes on the network and translate net names to physical addresses for layer 2. This layer is also responsible for any gateways between networks and for splitting big data files into smaller packages before transmission and recombining them again in the receiver (each package does not necessary follow the same route). In your case, this layer is empty.
Layer 4 is the Transport Layer. The purpose of this layer is to ensure end-to-end reliability and data safety by means of error detection, error correction, acknowledge telegrams, telegram serial numbers etc. In your case with your very limited time, a simple parity check will have to do. It is anyway as good as the fieldbus ASI (Actuator Sensor Interface), which does not have anything better either. Note that since the parity bit is a part of the data frame, this bit must also be mentioned in layer 2, but in this layer you must specify how this bit is used to detect errors, that is, which type of parity you use - even or odd. If you use more checkbits, the coding of these must be specified.
Layer 5 is the Session Layer, which sets up a communication channel between devices and handles retransmissions etc. for example in case of a missing acknowledge telegram. In your case, this layer is probably empty because you do not have any acknowledge that a lamp is actually turned on.
Layer 6 is the Presentation Layer. The purpose of this layer is to ensure correct interpretation of the received data and perform data conversions like translation between different code pages, data compression, data encryption and decryption etc. In this layer, you may specify that you use the most significant 7 bits as lamp address (if this is what you do) and use the least significant bit as data and that logical 1 means light on. It is very important that you specify exactly how each bit in the byte is interpretated.
Layer 7 is the application layer, which gives access to high-level network functions. In your case, this layer is probably empty.
Everything should be made as simple as possible, but not simpler. Any fool can write code that a computer can understand. Good programmers write code that humans understand.

