Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
Sending serial data

Posted by clubnet (clubnet), 3 March 2008
Presently I can send a the hex code 05 to a serial port and I receive 06  back which is correct, however I need to send 02 4C 03 4F  to the same port but it does not respond.


set ch [binary format H2 05 ]
is what I use to format the hex code
and below is the code that I use to read back.
set tmp [read $serialport ]
   binary scan $tmp "c*" tmp2


Original c++ code

#define HARTONC_ENQ             0x05
#define HARTONC_ACK             0x06
#define HARTONC_STX              0x02
#define HARTONC_LL              0x4c
#define HARTONC_ETX              0x03

#define HARTON_DISCONNECT       0
#define HARTON_SENDENQ          1
#define HARTON_INQUIRE          2
#define HARTON_DATAIN           3
#define HARTON_SENDACK          4
#define HARTON_IDLE             5

#define HTBUFMAX    128
//--------------------------------------------------------------------------------
int     Protocol = 0;
int     Protocol_shift = 0;
int     Ptc_status = 0;


char Harton_Inquire[HARTON_LL_SIZE+1] = {HARTONC_STX, HARTONC_LL, HARTONC_ETX, HARTONC_LL^HARTONC_ETX/*ECC*/};


The original code above  basically
does the following

STX     = 1 byte         (02)H

L         = 1 byte         (4C)H

ETX     = 1 byte         (03)H

BCC    = 1 byte         (4C)XOR(03)H

02 4C 03 4F

Your help would be much appreciated.
Dave.

Posted by clubnet (clubnet), 3 March 2008
Solved it, for anybody that might require it the future the code is ,


proc golfps_main::config_open {} {
 
 
  global serialport
 
   set serialport [open com1 r+]
 
   fconfigure $serialport -translation binary -blocking 0

   fconfigure $serialport  -buffering none
   fconfigure $serialport  -mode 9600,o,7,1 -handshake none
 

 switch -glob -- $::tcl_platform(os) {
       Windows* {
           fconfigure $serialport  -sysbuffer {65536 4096} -pollinterval 10
       }
   }

 
golfps_main::hex_serial_data 1
 
};# end of proc


proc golfps_main::send_serial_data { str } {

  global serialport


 puts -nonewline $serialport $str
 flush  $serialport

after 500 golfps_main::reader

};# end of proc


proc  golfps_main::hex_serial_data { decimal_value } {

     
     
     
 if { $decimal_value == "1" } {
 

          set ch [ eval binary format H2 05 ]
 
 }

 if { $decimal_value == "2" } {
 
     set ch ""
     
     
     set td1 [ eval  binary format H2 02 ]
     set td2 [ eval  binary format H2 4C ]
     set td3 [ eval  binary format H2 03 ]
     set td4 [ eval  binary format H2 4F ]

     append  ch $td1
     append  ch $td2
     append  ch $td3
     append  ch $td4
 
 }

 if { $decimal_value == "3" } {
 

          set ch [ eval binary format H2 06 ]
 
 }
 
 
 
golfps_main::send_serial_data  $ch


return -code


};# end of proc




proc golfps_main::reader {    } {


global serialport    
   
  #open up file for test  purposes only
  set tmp_file [open  testserver.txt  a+ ]
 
     
     
  set tmp [ read $serialport ]
  binary scan $tmp "c*" tmp2


       
  set newstring $tmp2

  ##set chk1 [ lindex $newstring 0 ]
  set chk2 [ lindex $newstring 25 ]

##tk_messageBox -icon warning -type ok -title Message -message "Constantly $chk1 $chk2\n\n $tmp2"

if { $chk2 == "3" } {
puts $tmp_file "Data Received from Master $tmp2 "
puts $tmp_file "Sending Ack "
golfps_main::hex_serial_data 3





} else {


puts $tmp_file "Trying for more data"
golfps_main::hex_serial_data 2

}

###golfps_main::hex_serial_data 2

## tk_messageBox -icon warning -type ok -title Message -message "Constantly Reading Data $tmp2\n\n "


   update idletasks    ;# display changes

   close $tmp_file


};# end of proc

golfps_main::config_open


Posted by admin (Graham Ellis), 3 March 2008
Magic - thanks for following up with that solution



This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2024: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho