Menu

Receiving a control string from an FTDI serial interface

Help
2022-12-05
2022-12-11
  • Terry Platt

    Terry Platt - 2022-12-05

    Hi All,
    This is probably a simple question to answer, but some advice would be appreciated. I have a small board with an FTDI input chip and a 16F687 processor. This is intended for controlling some small servos in an optical device and expects to receive blocks of 7 characters at intervals from the PC. These character strings are typically something like 'MN00010' and are decoded to provide direction and speed information. The problem is that I have used some of the GC demo examples, but they all seem to receive single characters, rather than strings. Can I easily receive a 7 character string at each download?
    Thanks for any solutions!

     
  • mkstevo

    mkstevo - 2022-12-05

    For receiving the track count from a DfPlayer MP3 module, I use this:

    #Chip 16F1829, 32
    #Define Ser_In        PortA.3
    #Define SerDF         PortA.5
    #Define SendAHigh Set SerDF On        'Serial Out                      (-)      Pin2 Pin2 DFPlayer
    #Define SendALow  Set SerDF Off       'Serial Out                      (-)      Pin2 Pin2 DFPlayer
    
    #Define RecAHigh  Ser_In On           'Serial In                       (-)      Pin4 Pin3 DFPlayer
    #Define RecALow   Ser_In Off          'Serial In                       (-)      Pin4 Pin3 DFPlayer
    
    InitSer 1, r9600, 1 + WaitForStart, 8, 1, none, normal
    SendAHigh                              'Idle High
    Dim     SerData(10)  'As Array
    Dim     ReturnTracks  As Word
    
    Sub Serial_Fetch
    
        Let ReturnTracks = 0
    
        SerReceive 1, SerData(1) '0x7e
        SerReceive 1, SerData(2) '0xff
        SerReceive 1, SerData(3) '0x06
        SerReceive 1, SerData(4)
        SerReceive 1, SerData(5)
        SerReceive 1, SerData(6) 'High Byte of track count
        SerReceive 1, SerData(7) 'Low  Byte of track count
        SerReceive 1, SerData(8) '0xef
    
        If SerData(6) > 0 Then
              Let ReturnTracks = SerData(6)
              Let ReturnTracks = ReturnTracks * 256 'High Byte
        End If
    
        If SerData(7) > 0 Then
              Let ReturnTracks = ReturnTracks + SerData(7) 'Low Byte
        End If
    
    End Sub
    

    As you can see, this returns 8 bytes of data which are placed in the array SerData(), so maybe you could use something similar for your 7 bytes? It has worked reliably for me. The entire code for the program is posted in the finished projects area, as my MP3 mini jukebox project if this doesn't make a deal of sense by itself.

     
  • William Roth

    William Roth - 2022-12-11

    @Terry Platt

    Since your device has a USART, you can use HSerGetString to receive strings. Usage is described in the Help File under: Command Reference >> Serial Communication >> RS232 ( hardware) >> HserGetString. The string being sent must be terminated with a Carriage Return.

    There is an example provided in the Help File.

    William

     

    Last edit: William Roth 2022-12-11
  • Terry Platt

    Terry Platt - 2022-12-11

    Hi William (and mkstevo),
    Thanks for the ideas - it looks like the HSerGetString command will do the trick. I'm now seeing the correct responses from the board.
    Many thanks!
    Terry

     

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.