Menu

Sending Multiple Bytes with Hsersend

2017-03-12
2017-03-12
  • William Roth

    William Roth - 2017-03-12

    I have been whining for quite sometime (mostly in private) about not being able to send multiple bytes of data with "Hsersend". While not a perfect solution this method is relatively simple and works quite well. Here's what I did.

    1. Create an array (buffer) to hold the bytes to be sent
    2. Create a subroutine to send the data loaded into the buffer
    3. Test

    This routine makes hsersend a little less cumbersome when sending data packets of various lengths

    The main applications would be to send raw data from a microcontroller to another device. The other device could be another micrcontroller, an RF module, a serial LCD Display, etc.

    I used an Arduino Uno R3, but any other GCB supported microcontroller with sufficient memory and an USART will work with the same code. If the chip does not have a USART, this method could be adapted for use with software serial "Sersend"

    Here is the working code:

     #chip mega328p, 16
    
     #define USART_BAUD_RATE 9600
     #define USART_BLOCKING
     #define USART_TX_BLOCKING
    
     #define CR 13     '// Carriage return
     #define LF 10     '// Line Feed
    
     DIM HserData(64)  '// Max 64 bytes (Reduce or increase size as needed)
    
     DIM Var1,Var2,Var3,Var4 as Byte   '// For testing
     DIM Chksum as Byte
    
     Wait 200 ms ' stabilize
    
    'Starting Values
    Var1 = 48
    Var2 = 56
    Var3 = 59
    var4 = 87
    Chksum = Var1+Var2+var3+var4
    
    '--------- Main Routine Begins Here ------------- 
    Do
    
     '*** Send Test Packet 1 (5 Bytes) ***
     HserData = Var1,Var2,Var3,Var4,Chksum : HserSendData
    
     '*** Send Test Packet 2 ( 14 Bytes) ***
     HserData = 14,8,19,88,56,22,12,99,74,13,59,34,101,172 : HserSendData
    
      Var1++      '// Increment all by 1
      Var2++
      Var3++
      Var4++    
      Chksum = Var1+Var2+Var3+Var4   '// Re-Calculate Chksum
       Wait 200 ms '// Between Packets
    Loop
    
    '========== Subs Below ========================
    
    Sub HserSendData
      For ii = 1 to HserData(0) '// number of bytes to send
         HserSend  HserData(ii)
      Next
    End Sub
    
     

    Last edit: William Roth 2017-03-12
  • Marc in the Dark

    Very useful!
    Thank you!!

     

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.