Jump to content

Serial Port /COM Port UDF


martin
 Share

Recommended Posts

Hi Martin,

Just wanted to let you know that I have your Serial COMM code working in a couple of instances. One is quite interesting where on one serial interface I am querying for information, doing some work and then passing this information in a different form to requests on a different serial port. Both ports handling requests at @30Hz and all at remarkably low CPU usage (~ 1.4%) - I am very pleased.

The other thing I am pleased about is that the code appears robust - my code here has been working, collecting data from production machinery 24x7 for a couple of weeks now - no issues - great :graduated:

In a previous post I mentioned that collecting data via the "GetString" call was so much slower (or consume 3x CPU cycles) than using "getByteArray" and converting to string. Did you ever get the chance to have a look ?

Another question (not related - and NOT a request - just a question !!)

Is is possible to add functionality to add "event" handling.

Just now, we poll the port to see if any information is there - the Holy Grail here is to be able to use an event to say "hey I'm here with some data - come and get me". I don't really know enough about callbacks and events to know if this could be made to work.

Thanks again.

Steve

Hi Steve,

Good to here you have made good use of this udf.

I did take a quick look at the _ComGetString but I couldn't see any reason why it should be slower that any other way to read the data. I didn't investigate though to see if I agree that it is slower.

Event mode is a possibility and not really difficult but I'm unlikely to do that soon, but I can see that it is desirable..

martin

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.
Link to comment
Share on other sites

  • 2 months later...

Hello. I have a little problem with this UDF.

I used a Serial Port Monitor to look what the original program sends to the device.

And then I send the same bytes to the device but it is not the same.

I made some screenshots to show you what I mean.

http://www.abload.de/gallery.php?key=uquKDJKJ

First picture shows the original program sending, second my try with _commsendbyte and third my try with _commsendbytearray or _commsendstring.

What can I do to send the bytes the same way. If it is not the same way the device does not answer.

Thanks for any help.

Edit:

I got it working!!

I set the flow control to NONE and played with RTS and DTR

_CommSetPort($iPort, $sErr, 9600, 8, 2, 1, 2)
If @error Then
 ConsoleWrite("Fehler beim Öffnen des COM-Ports!" & @CR)
 Exit
EndIf

_CommSetRTS(0)
_CommSetDTR(0)
Sleep(50)
_CommSetRTS(1)
_CommSetDTR(1)
Sleep(50)
Edited by funkey

Programming today is a race between software engineers striving to
build bigger and better idiot-proof programs, and the Universe
trying to produce bigger and better idiots.
So far, the Universe is winning.

Link to comment
Share on other sites

  • 4 weeks later...

Hello all! I am having difficulties sending a serial command to a given device. I am fully capable of sending ascii strings just fine, but when it comes to sending hex, I am having a issue sending hex. The string that I am trying to send is 54 79 70 65 20 79 6F 75 72 20 74 65 78 74 20 68 65 72 65. Would someone mind giving me a pointer or two?

Thank you much!

Link to comment
Share on other sites

Hello all! I am having difficulties sending a serial command to a given device. I am fully capable of sending ascii strings just fine, but when it comes to sending hex, I am having a issue sending hex. The string that I am trying to send is 54 79 70 65 20 79 6F 75 72 20 74 65 78 74 20 68 65 72 65. Would someone mind giving me a pointer or two?

Thank you much!

Can you give an example of how you are trying to send those characters? If you want to send hex data then normally you would use _ComSendByte or _ComSendByteArray.

If the hex characters you showed are meant to be sent as "Type your text here" then you only need to convert them to a string and then send the string,

But sending each value as a byte is exactly the same thing as sending them converted to an ASCII charcter string .

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.
Link to comment
Share on other sites

  • 2 weeks later...

Basically, I am just trying to send a couple of bytes. The format I am using is

_CommSendByteArray ("1008090B011008090B02", 1, 0)

I know I am totally missing something and may need to go back to serial school.... but it just aint working.

What am I doing wrong?

Thanks!

Link to comment
Share on other sites

Basically, I am just trying to send a couple of bytes. The format I am using is

_CommSendByteArray ("1008090B011008090B02", 1, 0)

I know I am totally missing something and may need to go back to serial school.... but it just aint working.

What am I doing wrong?

Thanks!

Maybe you have not read the information in the udf or you do not understand it, so I think you shouldn't be trying to use _CommSendByteArray at the moment. The first parameter must be the address of a struct but you have used a string.

If you want to send a couple of bytes then that function isn't the best to use anyway. _CommSendByteArray and _CommGetByteArray are for people who need to deal with lots of bytes very fast. Instead, use _CommSendByte. (and maybe _ComReadByte)

$S = "1008090B011008090B02"
For $n = 1 To StringLen($S) Step 2
    _CommSendByte(Number("0x" & StringMid($S, $n, 2)), 0)
Next

[/autoit]

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.
Link to comment
Share on other sites

Maybe you have not read the information in the udf or you do not understand it, so I think you shouldn't be trying to use _CommSendByteArray at the moment. The first parameter must be the address of a struct but you have used a string.

If you want to send a couple of bytes then that function isn't the best to use anyway. _CommSendByteArray and _CommGetByteArray are for people who need to deal with lots of bytes very fast. Instead, use _CommSendByte. (and maybe _ComReadByte)

$S = "1008090B011008090B02"
For $n = 1 To StringLen($S) Step 2
    _CommSendByte(Number("0x" & StringMid($S, $n, 2)), 0)
Next

[/autoit]

This did the trick. Thank you very much. It was not that I did not read it, it is just that I am somewhat new to programming and find the autoitscript forums as an incredibly valuable tool to learn the tricks of the trade. Much appreciated kind sir!

Link to comment
Share on other sites

  • 2 weeks later...

I am trying to get a string from my comport.

Everything goes well of a few seconds, after that my program get stuck.

My code:(just to test)

#include <GUIConstantsEx.au3>
#include <ProgressConstants.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <Array.au3>
#include 'CommMG.au3';or if you save the commMg.dll in the @scripdir use #include @SciptDir & '\commmg.dll'
Opt("WINTITLEMATCHMODE",3)
HotKeySet("{ESC}","alldone")

#Region ### START Koda GUI section ### Form=C:\Users\Erik\Desktop\koda\Forms\Form1.kxf
$Form1 = GUICreate("Quad copter informatie", 1679, 668, 127, 187)
$motor1 = GUICtrlCreateProgress(864, 50, 50, 500, BitOR($PBS_SMOOTH,$PBS_VERTICAL,$WS_BORDER))
$motor2 = GUICtrlCreateProgress(792, 50, 50, 500, BitOR($PBS_SMOOTH,$PBS_VERTICAL,$WS_BORDER))
$motor3 = GUICtrlCreateProgress(720, 50, 50, 500, BitOR($PBS_SMOOTH,$PBS_VERTICAL,$WS_BORDER))
$motor4 = GUICtrlCreateProgress(24, 50, 50, 500, BitOR($PBS_SMOOTH,$PBS_VERTICAL,$WS_BORDER))

$Label2 = GUICtrlCreateLabel("Rechts", 720, 24, 45, 20, $SS_CENTER)
$Label3 = GUICtrlCreateLabel("Achter", 792, 24, 45, 20, $SS_CENTER)
$Label4 = GUICtrlCreateLabel("Links", 864, 24, 45, 20, $SS_CENTER)
$label5= GUICtrlCreateLabel("Voor", 648, 24, 45, 20, $SS_CENTER)
$motor1_label = GUICtrlCreateLabel("Label1", 648, 560, 45, 20, $SS_CENTER)
$motor2_label = GUICtrlCreateLabel("Label2", 720, 560, 45, 20, $SS_CENTER)
$motor3_label = GUICtrlCreateLabel("label3", 792, 560, 45, 20, $SS_CENTER)
$motor4_label = GUICtrlCreateLabel("Label4", 864, 560, 45, 20, $SS_CENTER)


$Label9 = GUICtrlCreateLabel("GyroX", 336, 48, 118, 50)
GUICtrlSetFont(-1, 30, 400, 0, "MS Sans Serif")
$Label10 = GUICtrlCreateLabel("GyroY", 336, 112, 117, 50)
GUICtrlSetFont(-1, 30, 400, 0, "MS Sans Serif")
$Label11 = GUICtrlCreateLabel("GyroZ", 336, 176, 115, 50)
GUICtrlSetFont(-1, 30, 400, 0, "MS Sans Serif")
$Label12 = GUICtrlCreateLabel("Pitch", 336, 320, 94, 50)
GUICtrlSetFont(-1, 30, 400, 0, "MS Sans Serif")
$gyrox_label = GUICtrlCreateLabel("1", 472, 48, 123, 50)
GUICtrlSetFont(-1, 30, 400, 0, "MS Sans Serif")
$gyroy_label = GUICtrlCreateLabel("2", 472, 112, 123, 50)
GUICtrlSetFont(-1, 30, 400, 0, "MS Sans Serif")
$gyroz_label = GUICtrlCreateLabel("3", 472, 176, 123, 50)
GUICtrlSetFont(-1, 30, 400, 0, "MS Sans Serif")
$Label16 = GUICtrlCreateLabel("Roll", 336, 384, 74, 50)
GUICtrlSetFont(-1, 30, 400, 0, "MS Sans Serif")
$Label17 = GUICtrlCreateLabel("Yaw", 336, 448, 81, 50)
GUICtrlSetFont(-1, 30, 400, 0, "MS Sans Serif")
$pitch_label = GUICtrlCreateLabel("4", 472, 320, 123, 50)
GUICtrlSetFont(-1, 30, 400, 0, "MS Sans Serif")
$roll_label = GUICtrlCreateLabel("5", 472, 384, 123, 50)
GUICtrlSetFont(-1, 30, 400, 0, "MS Sans Serif")
$yaw_label = GUICtrlCreateLabel("6", 472, 448, 123, 50)
GUICtrlSetFont(-1, 30, 400, 0, "MS Sans Serif")

$gas = GUICtrlCreateProgress(648, 50, 50, 500, BitOR($PBS_SMOOTH,$PBS_VERTICAL,$WS_BORDER))
$Label21 = GUICtrlCreateLabel("Gas", 32, 24, 29, 20, $SS_CENTER)
$gas_label = GUICtrlCreateLabel("Label1", 24, 560, 45, 20, $SS_CENTER)
$Graphic1 = GUICtrlCreateGraphic(968, 48, 696, 528, BitOR($GUI_SS_DEFAULT_GRAPHIC,$WS_BORDER))


GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
Local $result
Local $array[12]

$array[1] = "0"
$array[2] = "0"
$array[3] = "0"
$array[4] = "0"
$array[5] = "0"
$array[6] = "0"
$array[7] = "0"
$array[8] = "0"
$array[9] = "0"
$array[10] = "0"
$array[11] = "0"


_CommSetport(3,$result,57600,8,'none',1,1)
MsgBox(0,'Setport error = ',$result)
_CommSetXonXoffProperties(11,13,100,100)
_CommSetRTS(1)
_CommSetDTR(1)

_CommSetRTS(0)
_CommSetDTR(0)

;motor1 motor2 motor3 motor4 gas gyrox gyroy gyroz pitch roll yaw

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            MsgBox(0,"Error","Gebruik de ESC toets om af te sluiten")

    EndSwitch

    $ComSTring = _CommGetLine(@CR,0,0)
    ;$comString = _CommReadByte(0)
    ;msgbox(0,"",$comString)
    ;$instr =  _CommGetString()
    $array = StringSplit($ComSTring," ")
    ;msgbox(0,"",$instr)
    if $array[0] < 11 Then
        ;MsgBox(0,"",$array )

    Else
        ;MsgBox(0,"",$array[1])
        $motor1 = $array[1]
        $motor2 = $array[2]
        $motor3 = $array[3]
        $motor4 = $array[4]

        $gas = $array[5]

        $gyrox = $array[6]
        $gyroy = $array[7]
        $gyroz = $array[8]

        $pitch = $array[9]
        $roll = $array[10]
        $yaw = $array[11]

        GUICtrlSetData($motor1_label, $motor1)
        GUICtrlSetData($motor2_label, $motor2)
        GUICtrlSetData($motor3_label, $motor3)
        GUICtrlSetData($motor4_label, $motor4)

        GUICtrlSetData($gyrox_label, $gyrox)
        GUICtrlSetData($gyroy_label, $gyroy)
        GUICtrlSetData($gyroz_label, $gyroz)

        GUICtrlSetData($pitch_label, $pitch)
        GUICtrlSetData($roll_label, $roll)
        GUICtrlSetData($yaw_label, $yaw)
        GUICtrlSetData($gas_label, $gas)
    EndIf

    sleep(10)



WEnd

Func AllDone()
    ;MsgBox(0,'will close ports','')
    _Commcloseport()
    ;MsgBox(0,'port closed','')
    Exit
EndFunc

The program get stuck at $ComSTring = _CommGetLine(@CR,0,0).

I would like to have a running program at about 115200, already tried this but i thought it could be the baud rate but that isn't.

At the ens of my string i send a \r that should be equal to @CR.

Hope you can help me with my little code, why it get stuck.

Edited by Erik.

I little problem, hard to find and fix

Link to comment
Share on other sites

Erik,

The line

$ComSTring = _CommGetLine(@CR,0,0)

has the wait time set to zero which means that the function will not return until it recieves a @CR character, so I assume that it is stuck there because it doesn't receive that character.

BTW in the_ComSetPort line you have used 'none' as a parameter but it should be 0 which means none. I don't think that it is the cause of your problem though.

Your first code tag has a backslash which needs to be removed to neaten up your post.

Edited by martin
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.
Link to comment
Share on other sites

Hi martin,

Thanks for you answer.

I know there is a waiting time until i get that char, i get all the information from my quad copter. I am sure it sends all his parameters every routine.

I tried to delete 'none' that did nothing.

I also tried to set a little delay of 10, the program runs but i am getting strange value's. I am sending 11 times 0 and i am getting other numbers.

As you can see i made a array, the first 0 does not always come on the first place array, after some loops it comes into another place.

My startup information looks like this: 0 0 0 0 0 0 0 0 0 0 0\r.

I am getting other information back, when using a simple terminal the information is correct.

Edited by Erik.

I little problem, hard to find and fix

Link to comment
Share on other sites

Hi martin,

Thanks for you answer.

I know there is a waiting time until i get that char, i get all the information from my quad copter. I am sure it sends all his parameters every routine.

I tried to delete 'none' that did nothing.

I also tried to set a little delay of 10, the program runs but i am getting strange value's. I am sending 11 times 0 and i am getting other numbers.

As you can see i made a array, the first 0 does not always come on the first place array, after some loops it comes into another place.

My startup information looks like this: 0 0 0 0 0 0 0 0 0 0 0\r.

I am getting other information back, when using a simple terminal the information is correct.

Then I would say the most likely problem is the settings you make in _commSetPort. Maybe something is not as you think. Are you sure that CR is sent on lines after the first one? Maybe you need to clear the input buffer before you start, maybe the transmitter is using hardware handshaking or different stop bits. I would try different flow settings, stop bits etc. Are you using the latest version of the ud and dll? (CommMG.au3 V2.8 and commg.dll V 2.71 I think)

If a simple terminal works does the terminal example in my first post work?

Try writing a simple script which does just what you think the sending program does and run that on another PC or the same PC on a different port and see if yoiu get the same. Every time I have written programs to talk to each other using the udf they have worked but that doesn't mean I have tried all possible settings.

Don't forget

A. Your string split is used on a string which has @CR at the end so I think you should remove that first.

B. you have used a closing code tag where you meant to use an opening code tag in post #328 and your post is inconveniently long in terms of screen usage.

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.
Link to comment
Share on other sites

Hi,

I first tried your program and that works just fine.

I am sure the \r has been send, when opening terminal each new print begins at a new line.

Yes i am using the l;ast version of your program, downloaded ist week or something.

The transmitter is not using handshaking or different stop bits, eveything should just work fine.

Edited by Erik.

I little problem, hard to find and fix

Link to comment
Share on other sites

Hi,

I first tried your program and that works just fine.

I am sure the \r has been send, when opening terminal each new print begins at a new line.

Yes i am using the l;ast version of your program, downloaded ist week or something.

The transmitter is not using handshaking or different stop bits, eveything should just work fine.

If my simple terminal works then you should be able to get your script to work. Make the following addition to the example script

After the line

$instr = _CommGetLine(@CR,100,200);String()

add the line

if $instr <> '' then filewrite("dump.txt",$instr)

Run the script to th estage where you think it has gone wrong then close it and then send me the file "dump.txt"

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.
Link to comment
Share on other sites

I did this and you can find the file in thos post.

The program stops at his self, hope you can do something with it.

This is what i am sending:

printf("%d %d %d %d %d %d %d %d %d %d %d\r",m1,m2,m3,m4,rx_throttle,gyro_x,gyro_y,gyro_z,pitch,roll,yaw);

After setting a minimum wait time the script does not stop anymore. The problem i now have.

The value's do not stay on the same place.

For example my m1 is 1800. the first time i will see it in array[1] but the second time i will see it somewhere else.

That is not going to work.

dump.txt

Edited by Erik.

I little problem, hard to find and fix

Link to comment
Share on other sites

I did this and you can find the file in thos post.

The program stops at his self, hope you can do something with it.

This is what i am sending:

printf("%d %d %d %d %d %d %d %d %d %d %d\r",m1,m2,m3,m4,rx_throttle,gyro_x,gyro_y,gyro_z,pitch,roll,yaw);

After setting a minimum wait time the script does not stop anymore. The problem i now have.

The value's do not stay on the same place.

For example my m1 is 1800. the first time i will see it in array[1] but the second time i will see it somewhere else.

That is not going to work.

The dump.txt files shows that you are receiving 7 zeros separated by spaces then repeated lines of 11 values of zero separated by a space and ending with a CR. There is no occurance of any other value that I can see. If this is what you got by modify the example terminal program then I assume that you didn't see any values received. Yet I think you said that the simple terminal example worked so I am confused.

To clarify:-

1. Does the example terminal program in my first post show the expected characters?

2. When you made the file dump.txt was it made by modifying the terminal program and when the modified version was running did you see any characters received (apart from 0)?

Maybe your program which sends the data sends only zero values to start with and that is what is shown in the dump file.

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.
Link to comment
Share on other sites

I thought i already told that the sending program only sends 11 * a 0.

After the startup it will send other data but i do not stop the startup.

I am now getting 11 * 0 with space. After a few seconds the program stops or it will give me strange vlaue's.

When running my program(microcontroller) i am sending at m1 the motor value. I need to have it in array[1]. The problem now is it can also come in array[5] or 7 it doesn't matter.

The steam is not constant.

I little problem, hard to find and fix

Link to comment
Share on other sites

I thought i already told that the sending program only sends 11 * a 0.

After the startup it will send other data but i do not stop the startup.

I am now getting 11 * 0 with space. After a few seconds the program stops or it will give me strange vlaue's.

When running my program(microcontroller) i am sending at m1 the motor value. I need to have it in array[1]. The problem now is it can also come in array[5] or 7 it doesn't matter.

The steam is not constant.

I think the problem is that I really don't understand you or your description of the problem. I understood that you were failing to receive the correct characters and they were sent as

This is what i am sending:

printf("%d %d %d %d %d %d %d %d %d %d %d\r",m1,m2,m3,m4,rx_throttle,gyro_x,gyro_y,gyro_z,pitch,roll,yaw);

which the dump file is supposed to show.

But now you tell me

I thought i already told that the sending program only sends 11 * a 0.

in which case why are we wasting our time getting a dump file which says you are getting exactly that?

What is the point of sending me a file of zeros, and you knew you only sent zeros, when I'm trying to find out what's going wrong when you don't send zeros.?

What's the point of trying to help you when you don't answer my questions? If you think I am psychic then why are we bothering posting at all?

How can the motor value come in array[5] by error if all you send is zeros? I can't follow what your problem is because, as far as I can see, your explanation of the problem is totally inadequate.

The idea of the dump file is that I can see what is being received and then I can possibly see what the problem is in your script assuming you can provide a working reproducer.

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.
Link to comment
Share on other sites

I think what you mean,

What i did was this: printf("%d %d %d %d %d %d %d %d %d %d %d \r",1,2,3,4,5,6,7,8,9,10,11);

now you should see number 1 at array position 1 and number 8 at position 8.

You can in the file that the input is correct.

The only problem i know have, i want to display the value's but they do not stay at the same position.

number 8 could be in array[8] but also in array[1].

Hope you understand what i mean?

I think the problem is not at the sending or receiving side but at the split array or something like that.

dump_3.txt

Edited by Erik.

I little problem, hard to find and fix

Link to comment
Share on other sites

I tried to add _CommClearInputBuffer after each loop but that didn't do the job.

When added that line the numbers are at different positions every time.

You can see in the dump file everything was received well. I added this line: msgbox(0,"",$ComSTring).

When starting the program i see 1 2 3 etc. in the message box. After a few seconds thins are going wrong. The message box shows me for example 5 6 7 8 9 10 11 1 2

Another time i get 9 10 11 1 2 or 2 3 etc.

The strange ting is, i can't find that error in the dump file but i can see it when using a message box. that is strange isn't it ?

To let you see what i mean, 2 screenshots. The first one is the good and after a few seconds i made a screenshot whats going wrong.

post-11819-0-76010700-1303573849_thumb.j

post-11819-0-95121800-1303573858_thumb.j

Edited by Erik.

I little problem, hard to find and fix

Link to comment
Share on other sites

I tried to add _CommClearInputBuffer after each loop but that didn't do the job.

When added that line the numbers are at different positions every time.

You can see in the dump file everything was received well. I added this line: msgbox(0,"",$ComSTring).

When starting the program i see 1 2 3 etc. in the message box. After a few seconds thins are going wrong. The message box shows me for example 5 6 7 8 9 10 11 1 2

Another time i get 9 10 11 1 2 or 2 3 etc.

The strange ting is, i can't find that error in the dump file but i can see it when using a message box. that is strange isn't it ?

To let you see what i mean, 2 screenshots. The first one is the good and after a few seconds i made a screenshot whats going wrong.

It isn't strange that the dump file shows all ok and the msgbox doesn't. It means that you are processing a line before the full line has been received. So for example when the numbers 1,2,3 have been returned by _CommGetLine because of the time out, 1,2,3 is written to the file and your message box shows "1,2,3". Later you recieve 4,5,6,7,8,9,10,11@CR and that is added to the file to make a complete line and your message box shows "4,5,6,7,8,9,10,11@CR".

So you need to read the string from _CommGetLine, or just use _CommGetString, and then you need to do something like this.

Global $Buffer;<------add this

;now replace _CommGetLine($par1,$par2,$par3) with GetNextLine($par1)
;eg
$ComSTring =GetNextLine(@CR)

Func GetNextLine( $EndChar);assumes $EndChar is a single char
    Local $last, $Result, $j

    $last = _CommGetString()
    if $last = '' then return ''


    $Buffer &= $last

    $j = StringInStr($Buffer, $EndChar)

    If $j = 0 Then return ''

    $Result = StringLeft($Buffer, $j - 1)
    $Buffer = StringTrimLeft($Buffer, $j)
   
    
  return $Result
  
EndFunc   ;==>GetNextLine

I haven't tried it but I hope you get the idea.

Edited by martin
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.
Link to comment
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
 Share

×
×
  • Create New...