AutoHotkey Community

It is currently May 26th, 2012, 10:06 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: COM Port script error
PostPosted: October 17th, 2008, 1:10 pm 
Offline

Joined: December 29th, 2004, 8:22 pm
Posts: 75
[Moderator's note: Moved.]

First, thanks aobrien for this: http://www.autohotkey.com/forum/viewtopic.php?t=28703&postdays=0&postorder=asc&highlight=com+port&start=0

I'm trying to send data to a machine control using this script. (sorry, no notations) Data does send, but I get an error on the machine: "overrun-error". Any suggestions?

Code:

#NoEnv
SendMode Input
#SingleInstance Force
SetTitleMatchMode, 2

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;----%Input% is sample string to send----;
;----F12::-Triggers-send-----------------;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

Input = O6()`nN1M6T1(#4-CCD)`nG55G90G0X10.Y0.S3000M3`nG43Z.1H1`nM8`nM98P555`nG80M9`nG91G28Z0.`nG91G28Y0.`nM30`nO7()`nO8()

COM_Port     = COM1
COM_Baud     = 9600
COM_Parity   = E
COM_Data     = 7
COM_Stop     = 2

COM_Settings = %COM_Port%:baud=%COM_Baud% parity=%COM_Parity% data=%COM_Data% stop=%COM_Stop% dtr=Off

Return


;;;;;;;;;;;;;;;;;;;;;;;;;;;;


F12::

Initialize_COM(COM_Settings)
StringReplace, Input, Input, `r, , All
Loop
{
   StringReplace, Input, Input, `n`n, `n, UseErrorLevel
   If ErrorLevel = 0
      Break
}

StringReplace, Input, Input, `n, ¢, All
Input = `%¢%Input%¢`%

Loop, Parse, Input
{
   In = %A_loopField%
   If In =
      Break
   If In = ¢
      In = 0x0D,0x0A
   Else
      In := Hex(In)
   Out = %Out%%In%,
}

StringTrimRight, Out, Out, 1
;MsgBox, %Out%
Write_to_COM(Out)
MsgBox, Send complete...
Close_COM(COM_FileHandle)
ExitApp


;;;;;;;;;;;;;;;;;;;;;;;;;;;;


Initialize_COM(COM_Settings)
{
  Global COM_FileHandle

  VarSetCapacity(DCB, 28)
  BCD_Result := DllCall("BuildCommDCB"
       ,"str" , COM_Settings
       ,"UInt", &DCB)
  If (BCD_Result <> 1)
  {
    MsgBox, There is a problem with Serial Port communication. `nFailed Dll BuildCommDCB, BCD_Result=%BCD_Result% `nThe Script Will Now Exit.
    Exit
  }
  StringSplit, COM_Port_Temp, COM_Settings, `:
  COM_Port_Temp1_Len := StrLen(COM_Port_Temp1)
  If (COM_Port_Temp1_Len > 4)
    COM_Port = \\.\%COM_Port_Temp1%
  Else
    COM_Port = %COM_Port_Temp1%

  COM_FileHandle := DllCall("CreateFile"
       ,"Str" , COM_Port         
       ,"UInt", 0xC0000000
       ,"UInt", 3
       ,"UInt", 0
       ,"UInt", 3
       ,"UInt", 0
       ,"UInt", 0
       ,"Cdecl Int")
  If (COM_FileHandle < 1)
  {
    MsgBox, There is a problem with Serial Port communication. `nFailed Dll CreateFile, COM_FileHandle=%COM_FileHandle% `nThe Script Will Now Exit.
    Exit
  }
  SCS_Result := DllCall("SetCommState"
       ,"UInt", COM_FileHandle
       ,"UInt", &DCB)
  If (SCS_Result <> 1)
  {
    MsgBox, There is a problem with Serial Port communication. `nFailed Dll SetCommState, SCS_Result=%SCS_Result% `nThe Script Will Now Exit.
    Close_COM(COM_FileHandle)
    Exit
  }

  ReadIntervalTimeout        = 0xffffffff
  ReadTotalTimeoutMultiplier = 0x00000000
  ReadTotalTimeoutConstant   = 0x00000000
  WriteTotalTimeoutMultiplier= 0x00000000
  WriteTotalTimeoutConstant  = 0x00000000

  VarSetCapacity(Data, 20, 0)
  NumPut(ReadIntervalTimeout,         Data,  0, "UInt")
  NumPut(ReadTotalTimeoutMultiplier,  Data,  4, "UInt")
  NumPut(ReadTotalTimeoutConstant,    Data,  8, "UInt")
  NumPut(WriteTotalTimeoutMultiplier, Data, 12, "UInt")
  NumPut(WriteTotalTimeoutConstant,   Data, 16, "UInt")

  SCT_result := DllCall("SetCommTimeouts"
     ,"UInt", COM_FileHandle
     ,"UInt", &Data)
  If (SCT_result <> 1)
  {
    MsgBox, There is a problem with Serial Port communication. `nFailed Dll SetCommState, SCT_result=%SCT_result% `nThe Script Will Now Exit.
    Close_COM(COM_FileHandle)
    Exit
  }

  Return %COM_FileHandle%
}


Hex(Inp,UC = 0)
{
   OldFmt = %A_FormatInteger%
   SetFormat, Integer, hex

   Loop, Parse, Inp
   {
      TransForm, Asc, Asc, %A_LoopField%
      Asc += 0
      StringTrimLeft, Hex, Asc, 2
      IfEqual, UC, 0
         Result = %Result%%Hex%
      Else
         Result = %Result%%Hex%00
   }
   SetFormat, Integer, %OldFmt%
   StringUpper, Result, Result
   Result = 0x%Result%
   Return Result
}


Write_to_COM(Message)
{
  Global COM_FileHandle
  Global COM_Port

  SetFormat, Integer, DEC

  StringSplit, Byte, Message, `,
  Data_Length := Byte0

  VarSetCapacity(Data, Byte0, 0xFF)

  i=1
  Loop %Byte0%
  {
    NumPut(Byte%i%, Data, (i-1) , "UChar")
    i++
  }


  WF_Result := DllCall("WriteFile"
       ,"UInt" , COM_FileHandle
       ,"UInt" , &Data
       ,"UInt" , Data_Length
       ,"UInt*", Bytes_Sent
       ,"Int"  , "NULL")
  If (WF_Result <> 1 or Bytes_Sent <> Data_Length)
    MsgBox, Failed Dll WriteFile to %COM_Port%, result=%WF_Result% `nData Length=%Data_Length% `nBytes_Sent=%Bytes_Sent%
}


Close_COM(COM_FileHandle)
{
  CH_result := DllCall("CloseHandle", "UInt", COM_FileHandle)
  If (CH_result <> 1)
    MsgBox, Failed Dll CloseHandle CH_result=%CH_result%

  Return
}



Thanks

_________________
-buttons, buttons,...
I like to push all the buttons!!!


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 17th, 2008, 7:23 pm 
Offline

Joined: December 29th, 2004, 8:22 pm
Posts: 75
I realized something that may be the problem.
The flow control is suppose to be set Xon/Xoff.
How could I set that within the script?

_________________
-buttons, buttons,...
I like to push all the buttons!!!


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 21st, 2008, 4:06 pm 
Offline

Joined: December 29th, 2004, 8:22 pm
Posts: 75
Ok, got everything working now. (crudely, but up & running)
Also threw a simple Gui to send & recieve with.
Just type & hit send, or, hit recieve.

This script is still a little buggy though.
I threw a short sleep in the sending loop to get it working,
but, is still not ther correct fix.
I have a feeling my true problem has something to do with bit count,
but out of time for now.

...anyway, here's the code:

Code:

#NoEnv
SendMode Input
#SingleInstance Force
SetTitleMatchMode, 2


COM_Port     = COM1
COM_Baud     = 9600
COM_Parity   = E
COM_Data     = 7
COM_Stop     = 2
Dwell        = 60

COM_Settings = %COM_Port%:baud=%COM_Baud% parity=%COM_Parity% data=%COM_Data% stop=%COM_Stop% dtr=Off


Gui, Add, Button, x10 y10 w250 h30, Send
Gui, Add, Button, x260 y10 w250 h30, Recieve
Gui, Add, Edit, x10 y50 w500 h600 vInput
Gui,Show

Initialize_COM(COM_Settings)

Return


;;;;;;;;;;;;;;;;;;;;;;;;;;;;


ButtonRecieve:

Input =
GuiControl,, Input
;Initialize_COM(COM_Settings)
Read_Data := Read_from_COM("0xFF")
;SplashTextOn, 400, 100, Recieving data...,

Loop
{
   Read_Data := Read_from_COM("0xFF")
   If (Bytes_Received > 0)
   {
      IF (1)
      {
         ASCII =
         Read_Data_Num_Bytes := StrLen(Read_Data) / 2

         Loop %Read_Data_Num_Bytes%
         {
            StringLeft, Byte, Read_Data, 2
            StringTrimLeft, Read_Data, Read_Data, 2
            Byte = 0x%Byte%
            Byte := Byte + 0   
            ASCII_Chr := Chr(Byte)
            If ASCII_Chr =
            {
               Continue
            }
            ASCII = %ASCII%%ASCII_Chr%
         }
         Out = %Out%%ASCII%
         GuiControl,, Input, %Out%
      }
   }
}

Stop:

Close_COM(COM_FileHandle)
GuiControlGet, Input
Input = %Input%
Loop
{
   StringReplace, Input, Input, `n`n, `n, UseErrorLevel
   If ErrorLevel = 0
      Break
}
GuiControl,, Input, %Input%
MsgBox, Recieve complete...
Return


;;;;;;;;;;;;;;;;;;;;;;;;;;;;


ButtonSend:

;Initialize_COM(COM_Settings)
GuiControlGet, Input
SplashTextOn, 400, 100, Sending data...,

StringReplace, Input, Input, `r, , All
Loop
{
   StringReplace, Input, Input, `n`n, `n, UseErrorLevel
   If ErrorLevel = 0
      Break
}
Input = `%`n%Input%`n`%

Loop, Parse, Input, `n
{
   Line = %A_LoopField%
   StringLeft, Check, Line, 1
   If Check = O
   {
      Num =
      Test = %Line%
      StringTrimLeft, Test, Test, 1
      Loop, Parse, Test
      {
         Char = %A_LoopField%
         If Char is not Number
            Break
         Num = %Num%%Char%
      }

   }
   Loop, Parse, Line
   {
      In = %A_LoopField%
      In := Hex(In)
      Out = %Out%%In%,
   }
   Out = %Out%0x0D,0x0A,
   Write_to_COM(Out)
   ControlSetText, Static1, `nO%Num%`n`n%Line%, Sending data...
   Out =
   Sleep, %Dwell%
}

SplashTextOff
Close_COM(COM_FileHandle)
MsgBox, Send complete...
Return


;;;;;;;;;;;;;;;;;;;;;;;;;;;;


Initialize_COM(COM_Settings)
{
  Global COM_FileHandle

  VarSetCapacity(DCB, 28)
  BCD_Result := DllCall("BuildCommDCB"
       ,"str" , COM_Settings
       ,"UInt", &DCB)
  If (BCD_Result <> 1)
  {
    MsgBox, There is a problem with Serial Port communication. `nFailed Dll BuildCommDCB, BCD_Result=%BCD_Result% `nThe Script Will Now Exit.
    Exit
  }
  StringSplit, COM_Port_Temp, COM_Settings, `:
  COM_Port_Temp1_Len := StrLen(COM_Port_Temp1)
  If (COM_Port_Temp1_Len > 4)
    COM_Port = \\.\%COM_Port_Temp1%
  Else
    COM_Port = %COM_Port_Temp1%

  COM_FileHandle := DllCall("CreateFile"
       ,"Str" , COM_Port         
       ,"UInt", 0xC0000000
       ,"UInt", 3
       ,"UInt", 0
       ,"UInt", 3
       ,"UInt", 0
       ,"UInt", 0
       ,"Cdecl Int")
  If (COM_FileHandle < 1)
  {
    MsgBox, There is a problem with Serial Port communication. `nFailed Dll CreateFile, COM_FileHandle=%COM_FileHandle% `nThe Script Will Now Exit.
    Exit
  }
  SCS_Result := DllCall("SetCommState"
       ,"UInt", COM_FileHandle
       ,"UInt", &DCB)
  If (SCS_Result <> 1)
  {
    MsgBox, There is a problem with Serial Port communication. `nFailed Dll SetCommState, SCS_Result=%SCS_Result% `nThe Script Will Now Exit.
    Close_COM(COM_FileHandle)
    Exit
  }

  ReadIntervalTimeout        = 0xffffffff
  ReadTotalTimeoutMultiplier = 0x00000000
  ReadTotalTimeoutConstant   = 0x00000000
  WriteTotalTimeoutMultiplier= 0x00000000
  WriteTotalTimeoutConstant  = 0x00000000

  VarSetCapacity(Data, 20, 0)
  NumPut(ReadIntervalTimeout,         Data,  0, "UInt")
  NumPut(ReadTotalTimeoutMultiplier,  Data,  4, "UInt")
  NumPut(ReadTotalTimeoutConstant,    Data,  8, "UInt")
  NumPut(WriteTotalTimeoutMultiplier, Data, 12, "UInt")
  NumPut(WriteTotalTimeoutConstant,   Data, 16, "UInt")

  SCT_result := DllCall("SetCommTimeouts"
     ,"UInt", COM_FileHandle
     ,"UInt", &Data)
  If (SCT_result <> 1)
  {
    MsgBox, There is a problem with Serial Port communication. `nFailed Dll SetCommState, SCT_result=%SCT_result% `nThe Script Will Now Exit.
    Close_COM(COM_FileHandle)
    Exit
  }

  Return %COM_FileHandle%
}


Read_from_COM(Num_Bytes)
{
  Global COM_FileHandle
  Global COM_Port
  Global Bytes_Received
  SetFormat, Integer, HEX

  Data_Length  := VarSetCapacity(Data, Num_Bytes, 0x55)

  Read_Result := DllCall("ReadFile"
       ,"UInt" , COM_FileHandle   ; hFile
       ,"Str"  , Data             ; lpBuffer
       ,"Int"  , Num_Bytes        ; nNumberOfBytesToRead
       ,"UInt*", Bytes_Received   ; lpNumberOfBytesReceived
       ,"Int"  , 0)               ; lpOverlapped

  If (Read_Result <> 1)
  {
    MsgBox, There is a problem with Serial Port communication. `nFailed Dll ReadFile on %COM_Port%, result=%Read_Result% - The Script Will Now Exit.
    Close_COM(COM_FileHandle)
    Exit
  }

  Data_HEX =
  Loop %Bytes_Received%
  {
    Data_HEX_Temp := NumGet(Data, i, "UChar")
    StringTrimLeft, Data_HEX_Temp, Data_HEX_Temp, 2
    Length := StrLen(Data_HEX_Temp)
    If (Length =1)
      Data_HEX_Temp = 0%Data_HEX_Temp%
    i++
    Data_HEX := Data_HEX . Data_HEX_Temp
  }

  SetFormat, Integer, DEC
  Data := Data_HEX

  Return Data

}


Hex(Inp,UC = 0)
{
   OldFmt = %A_FormatInteger%
   SetFormat, Integer, hex

   Loop, Parse, Inp
   {
      TransForm, Asc, Asc, %A_LoopField%
      Asc += 0
      StringTrimLeft, Hex, Asc, 2
      IfEqual, UC, 0
         Result = %Result%%Hex%
      Else
         Result = %Result%%Hex%00
   }
   SetFormat, Integer, %OldFmt%
   StringUpper, Result, Result
   Result = 0x%Result%
   Return Result
}


Write_to_COM(Message)
{
  Global COM_FileHandle
  Global COM_Port

  SetFormat, Integer, DEC

  StringSplit, Byte, Message, `,
  Data_Length := Byte0

  VarSetCapacity(Data, Byte0, 0xFF)

  i=1
  Loop %Byte0%
  {
    NumPut(Byte%i%, Data, (i-1) , "UChar")
    i++
  }


  WF_Result := DllCall("WriteFile"
       ,"UInt" , COM_FileHandle
       ,"UInt" , &Data
       ,"UInt" , Data_Length
       ,"UInt*", Bytes_Sent
       ,"Int"  , "NULL")
  If (WF_Result <> 1 or Bytes_Sent <> Data_Length)
    MsgBox, Failed Dll WriteFile to %COM_Port%, result=%WF_Result% `nData Length=%Data_Length% `nBytes_Sent=%Bytes_Sent%
}


Close_COM(COM_FileHandle)
{
  CH_result := DllCall("CloseHandle", "UInt", COM_FileHandle)
  If (CH_result <> 1)
    MsgBox, Failed Dll CloseHandle CH_result=%CH_result%

  Return
}



I'll try next week to fix it further, but if anyone has suggestions,
please post them...

_________________
-buttons, buttons,...
I like to push all the buttons!!!


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: [VxE], Bing [Bot], Google Feedfetcher, iBob35555VR, Tilter_of_Windmills and 77 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group