AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

COM Port script error

 
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
mAdDoG



Joined: 29 Dec 2004
Posts: 75

PostPosted: Fri Oct 17, 2008 12:10 pm    Post subject: COM Port script error Reply with quote

[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!!!
Back to top
View user's profile Send private message
mAdDoG



Joined: 29 Dec 2004
Posts: 75

PostPosted: Fri Oct 17, 2008 6:23 pm    Post subject: Reply with quote

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!!!
Back to top
View user's profile Send private message
mAdDoG



Joined: 29 Dec 2004
Posts: 75

PostPosted: Tue Oct 21, 2008 3:06 pm    Post subject: Reply with quote

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!!!
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group