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...