Jump to content
Sign in to follow this  
saget

Serial-port communication SOS

Recommended Posts

saget

Hi all,

I am working on a project which requires serial communication. i got vb code works:

#####################

Public Function tx_read_frame(data_type As Byte, data1 As Byte, data2 As Byte, data3 As Byte, data4 As Byte)

Static Byteout(0 To 7) As Byte, i As Byte

Dim sum As Integer

Byteout(0) = &H55

Byteout(1) = 1

Byteout(2) = data_type

Byteout(3) = data1

Byteout(4) = data2

Byteout(5) = data3

Byteout(6) = data4

sum = 0

For i = 0 To 6 Step 1

sum = sum + Byteout(i)

Next

Byteout(7) = sum Mod 256

main_form.MSComm1.InBufferCount = 0

main_form.MSComm1.Output = Byteout

End Function

##################

it didn't work when i try transfer to autoit...

##################

Func _RX_READ_FRAME($type, $data1, $data2, $data3, $data4)

Local $i, $sum, $Byteout_Array[8], $JXX_ARRAY[1]

$Byteout_Array[0] = 0x55

$Byteout_Array[1] = Hex(1, 2)

$Byteout_Array[2] = Hex($type, 2)

$Byteout_Array[3] = Hex($data1, 2)

$Byteout_Array[4] = Hex($data2, 2)

$Byteout_Array[5] = Hex($data3, 2)

$Byteout_Array[6] = Hex($data4, 2)

$sum = 0

For $i = 0 To 6 Step 1

$sum = $sum + $Byteout_Array[$i]

Next

$Byteout_Array[7] = Mod($sum , 256)

$Com_Object.Output = "0x55"&$Byteout_Array[1]&$Byteout_Array[2]&$Byteout_Array[3]&$Byteout_Array[4]&$Byteout_Array[5]&$Byteout_Array[6]&$Byteout_Array[7]

EndFunc

##################

any help...? thx a lot!

Edited by saget

Share this post


Link to post
Share on other sites
smartee

This by martin may be of interest to you. :)

Share this post


Link to post
Share on other sites
martin

This by martin may be of interest to you. :)

If it is then the function you showed might look like this ( it could also be done in other ways)

Func _RX_READ_FRAME($type, $data1, $data2, $data3, $data4)
    Local $i, $sum, $Byteout_Array[8], $JXX_ARRAY[1]

    $Byteout_Array[0] = 0x55
    $Byteout_Array[1] = 1
    $Byteout_Array[2] = $type
    $Byteout_Array[3] = $data1
    $Byteout_Array[4] = $data2
    $Byteout_Array[5] = $data3
    $Byteout_Array[6] = $data4
    $sum = 0

    For $i = 0 To 6 Step 1
        $sum = $sum + $Byteout_Array[$i]
        _ComSendByte($Byteout_Array[$i], 0)
    Next

    _ComSendByte(Mod($sum,256), 0)

EndFunc   ;==>_RX_READ_FRAME

You would need to set the serial port up first, but that must be done somewhere in the VB script as well.


Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites
saget

This by martin may be of interest to you. :)

cool !! powerfull man! powerfull autoit! thx

Share this post


Link to post
Share on other sites
saget

If it is then the function you showed might look like this ( it could also be done in other ways)

Func _RX_READ_FRAME($type, $data1, $data2, $data3, $data4)
    Local $i, $sum, $Byteout_Array[8], $JXX_ARRAY[1]

    $Byteout_Array[0] = 0x55
    $Byteout_Array[1] = 1
    $Byteout_Array[2] = $type
    $Byteout_Array[3] = $data1
    $Byteout_Array[4] = $data2
    $Byteout_Array[5] = $data3
    $Byteout_Array[6] = $data4
    $sum = 0

    For $i = 0 To 6 Step 1
        $sum = $sum + $Byteout_Array[$i]
        _ComSendByte($Byteout_Array[$i], 0)
    Next

    _ComSendByte(Mod($sum,256), 0)

EndFunc   ;==>_RX_READ_FRAME

You would need to set the serial port up first, but that must be done somewhere in the VB script as well.

It works!!!!!!!!! thanks so much, dear martin! It should be sent 1by1..?! - -!!!

PS: This code is for USB switch turn ON/OFF external device power~~ XD

Share this post


Link to post
Share on other sites
martin

It works!!!!!!!!! thanks so much, dear martin! It should be sent 1by1..?! - -!!!

PS: This code is for USB switch turn ON/OFF external device power~~ XD

What do you mean "it should be sent 1by1"? If you mean should the bytes be sent 1 at a time as in the function then I don't think it matters unless you are sending a lot of data and need to send it fast. In that case I would use _ComSendByteArray, but if what you've done works ok then leave it because it is nice and simple.

Can you tell me what the usb controller is you're using?


Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites
saget

What do you mean "it should be sent 1by1"? If you mean should the bytes be sent 1 at a time as in the function then I don't think it matters unless you are sending a lot of data and need to send it fast. In that case I would use _ComSendByteArray, but if what you've done works ok then leave it because it is nice and simple.

Can you tell me what the usb controller is you're using?

y, _ComSendByte already works perfect~~ thanks again!

post-58891-0-13654000-1310098722_thumb.j

Share this post


Link to post
Share on other sites
martin

y, _ComSendByte already works perfect~~ thanks again!

post-58891-0-13654000-1310098722_thumb.j

Thanks for the photo but what is it? I mean do you have a link where I can see the spec etc?

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×