PIC to serial with visual basic


Closed Thread
Results 1 to 8 of 8

Hybrid View

  1. #1
    Join Date
    Jul 2006
    Posts
    76

    Default PIC to serial with visual basic

    Hello,

    I am trying to hook up my PIC18F4550 to my serial port and send data using Visual Basic. The code for the PIC is below. I have a 22K resistor on the RX line of the PIC. The RTS and CTS lines I have working (thanks to skimask) but I can't seem to get the RX to work. The PC should transmit a "4" and the PIC should receive it and test its value. For visual basic the instruction I am using is: SerialPort.Write("4"). I believe I have the right baud and everything setup (9600) but I can't be sure (the rest is the default setup). Any help is appreciated! Thanks,

    -Mike

    Code on PIC:
    ------------------
    Include "modedefs.bas"
    define HSER_CLROERR 1
    define HSER_RCSTA 90h
    define HSER_BAUD 9600
    define HSER_TXSTA 20h
    define OSC 4

    rts var porta.2
    cts var portd.3
    led var portd.2
    rx var portc.7
    OK var word

    input rts
    output cts
    output led

    ADCON1 = %00001111

    low led : low cts

    main:

    while rts = 1

    high cts
    SERIN rx,N9600,OK
    if ok = "4" then
    high led
    endif

    wend

    low cts

    goto main

  2. #2
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by mbw123 View Post
    Code on PIC:
    ------------------
    Include "modedefs.bas"
    define HSER_CLROERR 1 : define HSER_RCSTA 90h : define HSER_BAUD 9600
    define HSER_TXSTA 20h : define OSC 4 : rts var porta.2:cts var portd.3
    led var portd.2 : rx var portc.7 : OK var word : input rts : output cts
    output led : ADCON1=15 : low led : low cts
    main:
    while rts = 1
    high cts : SERIN rx,N9600,OK
    if ok = "4" then
    high led
    endif
    wend
    low cts
    goto main
    Why are you using the RTS & CTS lines? Just curious...I've never used them, never had a need, kinda complicates thing unneccesarily. Try this whole thing without the RTS and CTS lines. Set Windows to 'Handshaking-None' in the serial properties. See what happens then. Again, you're breaking it down, starting super-simple, then working your way up.

    Another thing, you're using SERIN with HSER defines. Why?

    And SERIN @ 4mhz isn't exactly reliable. Switch down to 2400 and see what happens.

    You're also not using a MAX232. It should work with a PC using the inverted serial modes, but the voltage levels might not be high enough. You might end up using one. It's always worked for me, but you never know.

    Incidentally, I got over 100 threads here on this site alone all about serial interfacing, PC's, visual basic, hyperterminal, etc.
    Last edited by skimask; - 31st March 2007 at 01:44. Reason: Added info

  3. #3
    Join Date
    Jul 2006
    Posts
    76


    Did you find this post helpful? Yes | No

    Default

    OK, thanks for the response.

    I tried it without the RTS / CTS lines but it still didn't work.

    Sorry about the HSER defines, I used them before and I forgot to get rid of them. Even without them and switching the baud rates, though, it still doesn't work.

    "You're also not using a MAX232. It should work with a PC using the inverted serial modes, but the voltage levels might not be high enough. You might end up using one. It's always worked for me, but you never know."
    Yeah, I may end up with a MAX232. I just want to avoid it if at all possible.

    I really don't know where to go from here. Any help or suggestions?

    Thanks.

    -Mike

  4. #4
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Did you switch to 2400 baud?
    Did you read the manual where it says that 9600 baud might not be possible at 4mhz?

  5. #5
    Join Date
    Jul 2006
    Posts
    76


    Did you find this post helpful? Yes | No

    Default

    Yeah, I switched it to 2400 baud rate and turned off the handshake. Maybe the visual basic code is wrong or the code on the PIC should be "if OK = 4" instead of 4 being in quotation marks. Or am I missing something that's staring me in the face?

    Thanks for the help.

    -Mike

  6. #6
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by mbw123 View Post
    Yeah, I switched it to 2400 baud rate and turned off the handshake. Maybe the visual basic code is wrong or the code on the PIC should be "if OK = 4" instead of 4 being in quotation marks. Or am I missing something that's staring me in the face?

    Thanks for the help.

    -Mike
    Try this. It echo's everything you type on hyperterminal, but echo the same character 3 times and toggle the led every time you hit a key, that way you know it's really working..

    Include "modedefs.bas"
    DEFINE OSC 4
    rx var portc.7 : output rx : tx var portc.6 : input tx
    led var portd.2 : output led : serialdata var byte : ADCON1 = $F
    main:
    toggle led 'just show the program is running
    serin rx , N2400 , serialdata
    serout tx , n2400 , [ serialdata , serialdata , serialdata ]
    goto main


    Once again, you break it down, make it super simple, then build it back up...
    Last edited by skimask; - 31st March 2007 at 02:29.

Similar Threads

  1. Serial VB 2005 pic 16f877a problems
    By Snap in forum mel PIC BASIC Pro
    Replies: 28
    Last Post: - 8th July 2013, 00:52
  2. serial to PIC
    By kindows in forum mel PIC BASIC Pro
    Replies: 22
    Last Post: - 14th July 2009, 13:58
  3. Replies: 11
    Last Post: - 12th July 2008, 02:36
  4. vb6 to pic basic
    By Darrenmac in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 19th December 2005, 01:56
  5. Serial communication PIC to PIC help.
    By Rubicon in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 24th January 2005, 15:45

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts