C#での文字のシリアル通信
今バブルメッセンジャーという名前で、入力した文字を一文字ずつ水槽内に泡として出力するものをVB.NETで書かれたプログラムをC#に翻訳して作り直しています。
プログラムはシリアル通信を通してリレー回路が組まれた基盤に送り、それでバルブの開け閉めで文字を作るようにしています。
発生している問題
文字の入力後、送信をする際に基盤の回路が動かずそのままになってしまいます
該当のソースコード
C#
1using System; 2using System.Collections.Generic; 3using System.ComponentModel; 4using System.Data; 5using System.Drawing; 6using System.Linq; 7using System.Text; 8using System.Threading.Tasks; 9using System.Windows.Forms; 10 11namespace bubble_2019 12{ 13 public partial class Form1 : Form 14 { 15 private byte[] outdata = new byte[10]; 16 17 public Form1() 18 { 19 InitializeComponent(); 20 } 21 22 private void Form1_Load(object sender, EventArgs e) 23 { 24 serialPort1.PortName = "COM0"; 25 serialPort1.BaudRate = 9600; 26 serialPort1.Parity = System.IO.Ports.Parity.None; 27 serialPort1.DataBits = 8; 28 serialPort1.Handshake = System.IO.Ports.Handshake.None; 29 serialPort1.RtsEnable = false; 30 cmdSEND.Enabled = false; 31 sendroop.Enabled = false; 32 textBox1.Text = "文字を入れてください"; 33 timer1.Enabled = false; 34 } 35 36 private void cmdOPEN_Click(object sender, EventArgs e) 37 { 38 label2.Text = "エラーはありません"; 39 if (serialPort1.IsOpen == true) 40 { 41 serialPort1.Close(); 42 cmdOPEN.Text = "通信開始"; 43 comboBox1.Enabled = true; 44 cmdSEND.Enabled = false; 45 sendroop.Enabled = false; 46 } 47 else 48 { 49 try 50 { 51 serialPort1.PortName = comboBox1.SelectedItem.ToString(); 52 serialPort1.Open(); 53 } 54 catch 55 { 56 label2.Text = "COMポートが間違っています"; 57 return; 58 } 59 cmdOPEN.Text = "通信停止"; 60 comboBox1.Enabled = false; 61 cmdSEND.Enabled = true; 62 sendroop.Enabled = true; 63 } 64 } 65 private void SENDdeta() 66 { 67 string moji; 68 int a, b, c, n, mojisu, mojisu2; 69 int data1, data2, data3, data4, data5, data6; 70 long color; 71 Bitmap bitm = new Bitmap(pictureBox1.Image); 72 Color co; 73 mojisu = textBox1.Text.Length; 74 mojisu2 = mojisu; 75 serialPort1.DiscardInBuffer(); 76 moji = textBox1.Text; 77 mojisu = moji.Length; 78 79 for (n = 1; n <= mojisu; n++) //バッファ? 80 { 81 outdata[0] = 0x53; 82 outdata[1] = 0x30; 83 outdata[2] = 0x30; 84 outdata[3] = 0x30; 85 outdata[4] = 0x30; 86 outdata[5] = 0x30; 87 outdata[6] = 0x30; 88 outdata[7] = 0x30; 89 outdata[8] = 0x30; 90 outdata[9] = 0xD; 91 92 do 93 { 94 if (serialPort1.IsOpen == true) 95 { 96 serialPort1.Write(outdata, 0, outdata.Length); 97 string readData = serialPort1.ReadExisting(); 98 } 99 } 100 while (serialPort1.IsOpen == true); 101 for (var Y = 0; Y <= 23; Y++) 102 { 103 a = 0; 104 for (var X = 0; X <= 7; X++) 105 { 106 co = bitm.GetPixel((n - 1) * 24 + X, Y); 107 color = co.A; 108 a = a * 2; 109 if (color != 0) 110 a = a + 1; 111 } 112 b = 0; 113 for (var X = 8; X <= 15; X++) 114 { 115 co = bitm.GetPixel((n - 1) * 24 + X, Y); 116 color = co.A; 117 b = b * 2; 118 if (color != 0) 119 b = b + 1; 120 } 121 c = 0; 122 for (var X = 16; X <= 23; X++) 123 { 124 co = bitm.GetPixel((n - 1) * 24 + X, Y); 125 color = co.A; 126 c = c * 2; 127 if (color != 0) 128 c = c + 1; 129 } 130 131 data1 = (a / 16) % 16; 132 data2 = a % 16 + 0x30; 133 data3 = (b / 16) % 16 + 0x30; 134 data4 = b % 16 + 0x30; 135 data5 = (c / 16) % 16 + 0x30; 136 data6 = c % 16 + 0x30; 137 138 outdata[0] = 0x53; 139 outdata[1] = (byte)data5; 140 outdata[2] = (byte)data6; 141 outdata[3] = (byte)data5; 142 outdata[4] = (byte)data6; 143 outdata[5] = (byte)data3; 144 outdata[6] = (byte)data4; 145 outdata[7] = (byte)data1; 146 outdata[8] = (byte)data2; 147 outdata[9] = 0xD; 148 149 do 150 { 151 if (serialPort1.IsOpen == true) 152 { 153 serialPort1.Write(outdata, 0, outdata.Length); 154 string readData = serialPort1.ReadExisting(); 155 } 156 } 157 while (serialPort1.IsOpen == true); 158 159 System.Threading.Thread.Sleep(50); 160 } 161 outdata[0] = 0x53; 162 outdata[1] = 0x30; 163 outdata[2] = 0x30; 164 outdata[3] = 0x30; 165 outdata[4] = 0x30; 166 outdata[5] = 0x30; 167 outdata[6] = 0x30; 168 outdata[7] = 0x30; 169 outdata[8] = 0x30; 170 outdata[9] = 0xD; 171 172 do 173 { 174 if (serialPort1.IsOpen == true) 175 { 176 serialPort1.Write(outdata, 0, outdata.Length); 177 string readData = serialPort1.ReadExisting(); 178 } 179 } 180 while (serialPort1.IsOpen ); 181 182 System.Threading.Thread.Sleep(500); 183 } 184 } 185 186 private void textBox1_TextChanged(object sender, EventArgs e) 187 { 188 Bitmap mojisend = new Bitmap(pictureBox1.Width, pictureBox1.Height); 189 int mojisu, n, X, Y; 190 long color; 191 Color co; 192 193 pictureBox1.Image = null; 194 mojisu = textBox1.Text.Length; 195 196 for (n = 1; n <= mojisu; n++) 197 { 198 Bitmap mojikioku = new Bitmap(pictureBox2.Width, pictureBox2.Height); 199 Graphics g = Graphics.FromImage(mojikioku); 200 StringFormat format = new StringFormat(); 201 Font fnt = new Font("MS 明朝", 18, FontStyle.Regular, GraphicsUnit.Point); 202 format.Alignment = StringAlignment.Center; 203 format.LineAlignment = StringAlignment.Near; 204 g.Clear(Color.White); 205 g.DrawString(textBox1.Text. Substring(n-1,1), fnt, Brushes.Black, 13, 0,format); 206 fnt.Dispose(); 207 g.Dispose(); 208 pictureBox2.Image = mojikioku; 209 Bitmap bitm = new Bitmap(pictureBox2.Image); 210 211 for (Y = 0; Y <= 23; Y++) 212 { 213 for (X = 0; X <= 23; X++) 214 { 215 Graphics tenset = Graphics.FromImage(mojisend); 216 co = bitm.GetPixel(X, Y); 217 color = co.B; 218 if (color == 0) 219 { 220 tenset.DrawEllipse(Pens.Black, (n-1) * 24 + X, Y, 1, 1); 221 tenset.Dispose(); 222 pictureBox1.Image = mojisend; 223 } 224 } 225 } 226 pictureBox2.Image.Dispose(); 227 pictureBox2.Image = null; 228 } 229 } 230 231 private void Button4_Click(object sender, EventArgs e) 232 { 233 if (serialPort1.IsOpen == true) 234 235 serialPort1.Close(); 236 Environment.Exit(0); 237 } 238 239 private void cmdSEND_Click(object sender, EventArgs e) 240 { 241 SENDdeta(); 242 } 243 244 private void timer1_Tick(object sender, EventArgs e) 245 { 246 timer1.Enabled = false; 247 SENDdeta(); 248 timer1.Enabled = true; 249 } 250 251 private void sendroop_Click(object sender, EventArgs e) 252 { 253 if (sendroop.Text == "繰り返し送信") 254 { 255 sendroop.Text = "繰り返し停止"; 256 timer1.Enabled = true; 257 } 258 else 259 { 260 sendroop.Text = "繰り返し送信"; 261 timer1.Enabled = false; 262 } 263 } 264 265 private void TextBox1_GotFocus() 266 { 267 textBox1.SelectionStart = 0; 268 textBox1.SelectionLength = textBox1.Text.Length; 269 } 270 271 private void Form1_FormClosing(object sender, FormClosingEventArgs e) 272 { 273 if (serialPort1.IsOpen == true) 274 serialPort1.Close(); 275 Environment.Exit(0); 276 } 277 } 278}
試したこと
様々なサイトを回り、handshakeやあることを確認したりしました
補足情報
VB.NETのプログラムは6年前に卒業した先輩の残した物なので作成者に聞くことができません
回答2件
あなたの回答
tips
プレビュー