質問
機械の中にあるデータをVBAとシリアル通信 (RS232C) を用いてExcelに流したいのですが、1行目のデータしか受信することができません(改行された2行目以降のデータが取得できません)。
どのようにすれば改行以降のデータも取得することができるのでしょうか。
正直、シリアル通信を詳しく理解できておらず、現在勉強している最中です。
分かりやすく教えて頂けると助かります。
よろしくお願い致します。
該当のソースコード
VBE
1'WindowsAPI初期設定 2Declare PtrSafe Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal filename As String, ByVal rw As Long, ByVal d1 As Long, ByVal d2 As Long, ByVal d3 As Long, ByVal d4 As Long, ByVal d5 As Long) As Long 3Declare PtrSafe Function ReadFile Lib "kernel32" (ByVal handle As Long, ByVal buf As String, ByVal bytes As Long, ReadBytes As Long, ByVal d1 As Long) As Long 4Declare PtrSafe Function SetCommState Lib "kernel32" (ByVal handle As Long, ByRef lpDCB As DCB) As Long 5Declare PtrSafe Function SetCommTimeouts Lib "kernel32" (ByVal handle As Long, ct As COMMTIMEOUTS) As Long 6Declare PtrSafe Function CloseHandle Lib "kernel32" (ByVal handle As Long) As Long 7 8'CreateFile関数用定数 9Const GENERIC_READ = (&H80000000) 10Const GENERIC_WRITE = (&H40000000) 11 12'SetCommState関数用構造体定義 13Type DCB 14 DCBlength As Long 15 BaudRate As Long 16 bfModeCTL As Long 17 wReserved As Integer 18 XonLim As Integer 19XoffLim As Integer 20 ByteSize As Byte 21 Parity As Byte 22 StopBits As Byte 23 XonChar As Byte 24 XoffChar As Byte 25 ErrorChar As Byte 26 EofChar As Byte 27 EvtChar As Byte 28 wReserved1 As Integer 29End Type 30 31'SetCommTimeouts関数用構造体定義 32Type COMMTIMEOUTS 33 ReadIntervalTimeout As Long 34 ReadTotalTimeoutMultiplier As Long 35 ReadTotalTimeoutConstant As Long 36 WriteTotalTimeoutMultiplier As Long 37 WriteTotalTimeoutConstant As Long 38End Type 39 40Sub RS232C通信() 41 Dim cs As DCB 42 Dim ct As COMMTIMEOUTS 43 dim com as boolian 44 Dim i As Long 45 Dim l As Long 46 Dim buf As String * 16384 47 i = 1 48 49 PORT = ”COM1” 50 OP = Right(PORT, 1) 51 h = CreateFile(PORT, GENERIC_READ, 0, 0, OP, 0, 0) 52 53 '通信条件設定 54 cs.BaudRate = 9600 55 cs.ByteSize = 8 56 cs.Parity = 0 57 cs.StopBits = 0 58 a = SetCommState(h, cs) 59 60 'タイムアウト設定 61 ct.ReadIntervalTimeout = 0 62 ct.ReadTotalTimeoutMultiplier = 1 63 ct.ReadTotalTimeoutConstant = 1000 64 ct.WriteTotalTimeoutMultiplier = 1 65 ct.WriteTotalTimeoutConstant = 1000 66 b = SetCommTimeouts(h, ct) 67 68 'データ送信 69 c = ReadFile(h, ByVal buf, 10000, l, 0) 70 Cells(1, 5) = buf 71 72 'ポートクローズ:CloseHandle (ポートのハンドル) 73 com = CloseHandle(h) 74End Sub

バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/10/17 10:02
2022/10/17 10:06
2022/10/17 12:15