1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

USART/RS232 VB6 communication

Discussion in 'Microcontrollers' started by Mark Lazarides, Feb 8, 2004.

  1. Mark Lazarides

    Mark Lazarides Member

    Joined:
    Feb 8, 2004
    Messages:
    47
    Likes:
    0
    I'm designing a system that monitors an area for people carrying tags, and then tailors the area for them based on their preferences. I'm using Visual basic 6 for the user/pc interface. Being new to the joys of microcontrollers i'm slightly baffled by the USART module of the PIC 16f627 that I'm using though.

    From what i've read, i've opted for the 8N1 asynchronous format. I'll have three pieces of information to send (user, area, and device) although these could just be ascii characters. what format should the data to be sent take? :roll:

    Any help would be fantastic, or if anybody knows of some decent (there's a lot of rubbish out there) online tutorials I'd be most grateful.
     
  2. Exo

    Exo Active Member

    Joined:
    Sep 18, 2003
    Messages:
    1,953
    Likes:
    1
    Location:
    Belgium
    You could send the data directly to the pc, but it depends how error sensitive your application is. If you want some degree of error detection then you could send the data in packets, each packet accompanied by a checksum.

    Many ppl use packets in the shape as <STX><...DATA...><Checksum><ETX>

    where STX is a start-of-text symbol, wich indicates a packet has begun, then the data is followed and stored until you reach a ETX end packet indicator, Then the checksum is used to check the data received was not corrupted.
    Now you have the problem the values of STX and ETX cannot be sent anymore as data. Simplest way is too choose 2 codes for STX and ETX wich you won't use for data. But you could use byte stuffing also.

    I'm working on a bootloader wich uses this technique, but the pc side of things is in visual C++ so it wouldn't help you
     
  3. Mark Lazarides

    Mark Lazarides Member

    Joined:
    Feb 8, 2004
    Messages:
    47
    Likes:
    0
    STX etc.

    Thanks for the reply.

    will the MCU will ignore the first and last bytes using the STX etc? IS this a protocl i set on start up or is it ignored by the code on the MCU? let's say for example i use £ for the start and @ for the end. can i just send these (MSComm) to the MCU directly and with my three bytes of ID data? so it'd look something like:


    £ABD@?
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. Exo

    Exo Active Member

    Joined:
    Sep 18, 2003
    Messages:
    1,953
    Likes:
    1
    Location:
    Belgium

    You''ll have to write code for the pic to send/receive data, this code must include the detection of the packet begin and end indicators and not store them.

    Normal practice would be to wait for a STX to arrive before starting to store data. If a second STX arrives before the packet was ended then everything is reset (so the data already received is cleared). When an ETX is received calculate the checksum and process the data if the checksum is correct.

    you can simply send the data with MSCOMM, but you'll have to add code to detect errors. In my bootloader i let the pc send a command to the pic (in a packet) and the pic must respond to the pc (also with a packet). If anything goes wrong while receiving i make the pic go back to wait for a new STX, so it will not respond, The pc will then time-out and retry.

    You should also keep track of a counter wich counts the number of received bytes to detect receive buffer overflows.
     
  6. Mark Lazarides

    Mark Lazarides Member

    Joined:
    Feb 8, 2004
    Messages:
    47
    Likes:
    0
    Thanks for the advice exo, very much appreciated.
     
  7. StupidDum

    StupidDum New Member

    Joined:
    Nov 3, 2003
    Messages:
    242
    Likes:
    0
    Location:
    Malaysia
    Now i realize everybody using similar protocol :p
    In VB, it is convenient to define it as string, as in nature, the MSCOMM1 output is in string.
    Furthermore, VB is not good in handling binary or hexadecimal.

    If you are to use hexadecimal, remember to use the command "CHR()".
    HEX() instruction converts the number into STRING in hexadecimal
    for instance, 0X2A will be "2" and "A".

    I am currently working on similiar thing. COmmunication between PC and microcontroller. Certain error handling should be included, as sometime there might be error in the data. If the progam is not written efficiently, the microcontroller may just halt if there is error in the packet.
     
  8. reeve333

    reeve333 New Member

    Joined:
    Apr 7, 2011
    Messages:
    41
    Likes:
    0
    Hi, I am doing project that use VB6.0 to sent signal to PIC 16F877A via RN41(blueetooth device)

    Unfortunery, i meet a problem when i success to connect the bluetooth device to my computer( mena change red LED to Green) I unable to sent any signal or data.

    Anyone know any solution please tell me.....
    Really thanks for the help.......

    in the VB i used

    MSComm1.Output = "a"

    I think it should be correct right??

    If need, i will able to upload my program....Thanks.........
     
  9. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,240
    Likes:
    645
    Location:
    Ex Yorks' Hants UK
    ONLINE
    hi,
    Attach your VB program so that we can check the MSCOMM settings.
     
  10. reeve333

    reeve333 New Member

    Joined:
    Apr 7, 2011
    Messages:
    41
    Likes:
    0
    Thanks for reply, i uploading my vb here. It just the simple program i make.

    Sorry, the correct one is 1.7kb file size dy.............Thanks
     

    Attached Files:

    Last edited: Apr 14, 2011
  11. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,240
    Likes:
    645
    Location:
    Ex Yorks' Hants UK
    ONLINE
    hi,
    Will download your program and have a look.
     
  12. reeve333

    reeve333 New Member

    Joined:
    Apr 7, 2011
    Messages:
    41
    Likes:
    0
    I am much appreciated for your help..........
     
  13. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,240
    Likes:
    645
    Location:
    Ex Yorks' Hants UK
    ONLINE
    After looking thru your program, I can see it will take me a little while to sort out, will reply with a result tomorrow.
    I like a challenge.:)
     
  14. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,240
    Likes:
    645
    Location:
    Ex Yorks' Hants UK
    ONLINE
    hi,
    Look at this version of your program.
    I have used the binary option for the MsComm1 as you may also want to send control codes to the modem.

    The program has been tested by being linked to another PC acting as a modem and it works OK.

    I have set the Comport as #2, set it back to #1 for your project.

    If you have any questions, please ask...:)
     

    Attached Files:

    Last edited: Apr 14, 2011
  15. reeve333

    reeve333 New Member

    Joined:
    Apr 7, 2011
    Messages:
    41
    Likes:
    0
    Really thanks for that.......
    I will try it now.......
     
  16. reeve333

    reeve333 New Member

    Joined:
    Apr 7, 2011
    Messages:
    41
    Likes:
    0
    Sorry, i have a problem when i run the program you give me. The problem is when i run it, it will say method or data member nhot found for ".SelText"

    May i know what i should i change it??
     
  17. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,240
    Likes:
    645
    Location:
    Ex Yorks' Hants UK
    ONLINE
    hi,
    I changed the simple Text box into a Rich Text box, so that print out could be formatted 'better'.

    Use the VB menu, Projects/Components and Add the Microsoft Rich Text box control...OK.?

    If you cannot do that, use a simple text box and change all the rtb1.selxxx commands to Text1.xxxx, I would recommend the RTBox use.
     
  18. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,240
    Likes:
    645
    Location:
    Ex Yorks' Hants UK
    ONLINE
    hi,
    Have changed the boxes to simple Text.
    Its been tested linked to another PC and it works.

    Use the modcomm2.vbp within the zip.
     

    Attached Files:

    Last edited: Apr 15, 2011
  19. reeve333

    reeve333 New Member

    Joined:
    Apr 7, 2011
    Messages:
    41
    Likes:
    0
    Thanks so much.........I will try it now.......
    you mean that this program able to sent out the data ??
     
  20. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,240
    Likes:
    645
    Location:
    Ex Yorks' Hants UK
    ONLINE
    hi,
    If you want to 'talk' to a modem, you must have two way communication between the modem and the MCU or PC.
    When then MCU initially sends the "AT" command to the Modem, it will expect "OK" as a reply.
    Usually the MCU then sends 'setup' commands to the modem an awaits the required reply.

    After the modem is setup data can be transferred between the MCU and modem and vice versa.

    The VB program transmit data string is fixed in the program I posted, but it could easily be edited to enable it to transmit data and commands from the keyboard.

    Exactly what are you trying to do.???
     
  21. reeve333

    reeve333 New Member

    Joined:
    Apr 7, 2011
    Messages:
    41
    Likes:
    0
    icic.........

    actually my project is transmit a control signal (such as 'K') to microcontroller via bluetooth (RN-41). Then the microcontroller will do somethings ragarding to the control signal.

    so it mean that in the program of my PIC, i need to sent feed back also??
    cause i meet the same probelm, after i sent the signal, it such as nothings happen.
    May i know, what should i do now??change my PIC program??
     

Share This Page