SerialCameraFunctions.bas module Code
Try copying and pasting the following VB6 code into a standard module named SerialCameraFunctions.bas and then use the functions as needed on a form that you create.
'******************************************************************************************
'******************************************************************************************
'*** ***
'*** Module: SerialCameraFunctions for the LS-Y201 RS232 Camera ***
'*** SaveCameraJPG(Comm As MSComm, FileName As String) As String. Example:"F:\Camera\Pic1.jpg") ***
'*** SetCameraBaud(Comm As MSComm, Baud As String) As String. Example: "38400" ) ***
'*** SetCameraImageSize(Comm As MSComm, Size As String) As String. Example: "320x240" ) ***
'*** SetCameraCompression(Comm As MSComm, CompRate As String) As String.("1" to "255") ***
'*** ResetCamera (Comm As MSComm) ***
'*** CameraHexOutputString(Comm As MSComm) ***
'*** All functions must include the mscomm name as the first argument. ***
'*** All camera functions output a string about the camera's health. ***
'*** I do not need the power saving command so no sub for that here. ***
'*****************************************************************************************
'*****************************************************************************************
'These declarations needed for the very cool Pause function.
Private Declare Function GetTickCount Lib "kernel32" () As Long
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
'Borrowed Pause function, so I can get delays in this standard module,
'where normal timer controls are not available.
Public Sub Pause(Optional ms As Long = 200) ' delay in ms. 200 ms default.
On Error Resume Next
Dim tc As Long
tc = GetTickCount
While GetTickCount < tc + ms: Sleep 1: DoEvents: Wend
End Sub
'I do these steps to save a picture:
'(1) Freeze the image in the camera with the "Take Picture" command.
'(2) Find the approximate file length with the "Read JPG File Size" command.
'(3) Tell the camera to send its JPG content to Comm1 input buffer.
'(4) Transfer the input buffer into a byte array.
'(5) Repair the byte array by trimming extra bytes off beginning and end.
'(6) Send the "Stop Taking Pictures" command so new pictures can happen.
'(7) Write the byte array to a JPG file.
Public Function SaveCameraJPG(Comm As MSComm, ByVal FileName As String) As String
Dim S As String
Dim TempArray() As Byte
Dim InputArray() As Byte
Dim OutputArray() As Byte
Dim CurrentInputSize As Integer
Dim lngFile As Long
On Error GoTo ErrorHandler
Comm.Output = CameraCommand("56 00 36 01 00") ' (1) Send take-picture command to camera.
Pause 500 ' Wait 500 ms.
Comm.InBufferCount = 0 'discard input buffer.
Comm.Output = CameraCommand("56 00 34 01 00") ' (2) Send read-jpg-size command to camera.
S = CameraHexOutputString(Comm) 'reads the size string of the jpg still in the camera
L = Len(S) ' the number of charactors in the size string.
S = Mid(S, 29, 3) + Mid(S, 33, 2) 'pick out the two hex string digits concerning file size.
' (3) Send read jpg command to camera.
Comm.Output = CameraCommand("56 00 32 0C 00 0A 00 00 00 00 00 00 " + S + " 00 0A")
Do 'this loop until comm buffer count remains constant for 0.2 seconds.
CurrentInputSize = Comm.InBufferCount
Pause 200 'Wait (0.2)
Loop Until CurrentInputSize = Comm.InBufferCount
TempArray = Comm.Input ' (4) All data fits into a Byte Array!
OutputArray = Repaired(TempArray) ' (5) Byte array for output to file was repaired.
Comm.Output = CameraCommand("56 00 36 01 03") ' (6) Send stop-taking-picture command to camera.
Kill (FileName) 'deletes old file if any, goes to error handler if no file.
DoEvents
lngFile = FreeFile() 'get next free file number from the system.
Open FileName For Binary Access Write As lngFile ' open JPG file for writing.
Put lngFile, , OutputArray ' (7) File is written all at once.
Close lngFile ' close file
SaveCameraJPG = "JPG saved to " + FileName ' String returned by this function.
Exit Function
ErrorHandler:
If Err.Number = 53 Then
SaveCameraJPG = " Making new file " + FileName
Else
SaveCameraJPG = Error(Err.Number) 'returned message
End If
Resume Next
End Function
'OK, for the image size to take effect, a reset has to happen.
'A reset changes the baud rate back to the default 38400. What a pain in the ass!
'I am going to take care of the mess here, so that it is transparent to the user.
Public Function SetCameraImageSize(Comm As MSComm, ByVal Size As String) As String
Dim S As String, BaudRate As String
On Error GoTo ErrorHandler
S = Comm.Settings ' going to extract baud rate string
BaudRate = Mid(S, 1, Len(S) - 6)
If Size = "160x120" Then S = "22" ' Find the rate specific last byte for
If Size = "320x240" Then S = "11" ' the hex image size command string.
If Size = "640x480" Then S = "00"
' Send image size command to camera.
Comm.Output = CameraCommand("56 00 31 05 04 01 00 19 " + S)
Pause 500 ' wait 500 ms before sending next command
S = ResetCamera(Comm) 'Send reset command to camera.
Pause 500 ' wait 500 ms before sending next command
S = SetCameraBaud(Comm, BaudRate) 'Send prior baud rate command to camera.
SetCameraImageSize = "Image size = " + Size 'returned message is image size.
Exit Function
ErrorHandler:
SetCameraImageSize = Error(Err.Number) 'returned message
End Function
Public Function SetCameraBaud(Comm As MSComm, ByVal Baud As String) As String
Dim S As String
On Error GoTo ErrorHandler
If Baud = "9600" Then S = "AE C8" ' hex codes associated with these baud rates.
If Baud = "19200" Then S = "56 E4"
If Baud = "38400" Then S = "2A F2"
If Baud = "57600" Then S = "1C 4C"
If Baud = "115200" Then S = "0D A6"
Comm.Output = CameraCommand("56 00 24 03 01 " + S) ' Send baud rate command to camera.
'Pause 500 ' Give the camera a chance to output responce using old baud rate.
Comm.Settings = Baud + ",N,8,1" ' Change Comm1 baud rate to match the camera's new rate.
SetCameraBaud = "Camera Baud Rate = " + Baud 'returned message
Exit Function
ErrorHandler:
SetCameraBaud = Error(Err.Number) 'returned message
End Function
Public Function SetCameraCompression(Comm As MSComm, ByVal CompRate As String) As String
Dim S As String, L As Integer
On Error GoTo ErrorHandler
S = Hex(Val(CompRate)) ' converts CompRate string to decimal to hex string
If Len(S) = 1 Then S = "0" + S ' adds formating leading zero if needed.
Comm.Output = CameraCommand("56 00 31 05 01 01 12 04 " + S) ' Send compression command to camera.
SetCameraCompression = "Camera compression = " + CompRate 'returned message
Exit Function
ErrorHandler:
SetCameraCompression = Error(Err.Number) 'returned message
'Resume Next
End Function
Public Function ResetCamera(Comm As MSComm) As String
On Error GoTo ErrorHandler
Comm.Output = CameraCommand("56 00 26 00") ' Send reset command to camera.
Comm.Settings = "38400,N,8,1" 'Resetting camera changes its baud rate to the default.
ResetCamera = "Camera Reset. Baud rate now 38400" 'returned message
Exit Function
ErrorHandler:
ResetCamera = Error(Err.Number) 'returned message
End Function
Public Function CameraCommand(ByVal strInput As String) As Byte()
'This function takes a hex looking string input and changes it into a binary array for output.
'Input must be in the form "HH HH HH ...HH" (3 * N - 1 charactors long including spaces)
Dim Temp(20) As Byte
Dim I As Integer, N As Integer
N = Int((Len(strInput) + 1) / 3) ' number of hex input pairs.
For I = 0 To N - 1 ' build temp array. First pair will be #0 in array
Temp(I) = CByte("&H" + Mid(strInput, I * 3 + 1, 2)) ' string to hex to decimal byte
Next I
CameraCommand = Temp ' Output is a byte array representation of a camera command
End Function
Public Function CameraHexOutputString(Comm As MSComm) As String
'Makes a string of the camera output waiting in the comm input buffer.
'Exits function when input buffer is quiessent for 0.2 seconds.
Dim CurrentInputSize As Integer
Dim I As Integer
Dim S As String
Dim TempArray() As Byte
Do 'this loop until comm buffer count remains constant for 0.2 seconds.
CurrentInputSize = Comm.InBufferCount
Pause 200 'Wait 0.2 seconds
Loop Until CurrentInputSize = Comm.InBufferCount
TempArray = Comm.Input ' All data fits into Bytearray!
For I = 0 To UBound(TempArray) ' Build up the output string
S = Hex(TempArray(I)) ' Hex output.
If Len(S) = 1 Then S = "0" + S 'add a leading "0" if only one hex digit.
CameraHexOutputString = CameraHexOutputString + S + " " 'Build output string.
Next I
End Function
Public Function Repaired(InArray() As Byte) As Byte()
Dim OutArray() As Byte
Dim I As Integer, J As Integer, N As Integer
Dim Start As Integer, Finish As Integer
Dim AddOn As Boolean
AddOn = False
N = UBound(InArray)
Finish = N
For I = N - 1 To 1 Step -1 'start at end of input array and work backwards.
If Hex(InArray(I)) = "FF" Then
If Hex(InArray(I + 1)) = "D8" Then Start = I 'this is the start byte
If Hex(InArray(I + 1)) = "D9" Then Finish = I + 1 ' this is the stop byte
End If
Next I
J = Finish - Start 'the length of the repaired array
If Finish = N Then ' never found the stop byte, so better add the stop bytes
J = J + 2 'increase the length of the repaired array for the two stop bytes
AddOn = True ' remind me to add stop bytes
End If
ReDim OutArray(J) As Byte
For I = Start To Finish
OutArray(I - Start) = InArray(I)
Next I
If AddOn Then 'need to add stop bytes
B = 255
OutArray(J - 1) = B
B = 217
OutArray(J) = B
End If
Repaired = OutArray
End Function