Menu

Serial receive help

Help
Ryan
2016-06-23
2016-06-24
  • Ryan

    Ryan - 2016-06-23

    I want to be able to receive a command and do the appropriate output bassed off it, but im not sure exactly how to get it to work for maore than a single character, heres what im trying to do, I commented out what i started with:

    chip 12F683, 8 'mhz

    config MCLRE=off, WDT=off, OSC=INTOSC, boden=off

    dim temp as byte
    dir gpio.0 in
    dir gpio.1 out

    define RecAHigh GPIO.0 off

    define RecALow GPIO.0 on

    InitSer 1,r9600,1,8,1,none,invert
    temp = 0
    dim temp as word
    set gpio.1 on
    main:
    SerReceive 1, Temp
    'if temp = 49 then
    if temp = "on" then
    set gpio.1 off
    temp = 0
    end if
    'if temp = 48 then
    if temp = "off" then
    set gpio.1 on
    temp = 0
    end if
    goto main

     
  • mmotte

    mmotte - 2016-06-24

    The SerReceive would receive ascii characters from a terminal. I assume you are sending the commands from a terminal. It only receives one byte char at a time.

    To receive a string like "on" you must save the chars ,one at a time, and wait for an ending char like a carriage return (CR). After you have the complete command, then you compare the received command to to programmed commands and excute the appropriate method. See example below.

    Your first try, that you commented out, commands should work. A 48 is an ascii '0' which would be a one char command to shut off your I/O point. You receive a byte char and execute it.

    One little error is that you "Dim"med temp differently twice.

    If you look in the Great Cow Basic documentation >command references>serial communications>RS232(hardware)>HSerGetString you will find an example of turning I/O on and off with long commands. Your 12F683 can't do this because it don't have a hardware usart but if you look up the HSerGetString command in the Include directory GreatcowBasic>include>lowlevel>usart.h you will find the code that makes that command work. Actually it is a short routine that could be adated to Soft Serial.

    sub HSerGetString (Out HSerString As String, optional In comport = 1)
        HSerIndex = 0
      Do
        comport = comport 'not really required but added for clarity
        HSerReceive ( HSerInByte )
        'Enter key?
        If HSerInByte = 13 Then
             Exit Sub
            End If
        'letters,numbers,punctuation
        If HSerInByte >= 32 and HSerInByte <= 126 Then
                HSerIndex++
            HSerString(HSerIndex) = HSerInByte
            HSerString(0) = HSerIndex
        End If
        Loop
    End Sub
    
     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.