sending ascii commands over serial


jkupke

Recommended Posts

Ok, I know this is incredibly simple but I'm not sure what I'm doing wrong. I have a stepper motor connected over serial. It operates using very simple commands, send w to wake up the motor, send G to step motor. I have no problems controlling the motor through the monitor window. But when I try to use a sequence i get a c1000 channel or function not found error.

I have created the device and it works since it works in the monitor window. I simply need to step the motor

device.stepmotor.write(x)

where x is whatever command i need to send. Is this error because I'm not sending the commmand in the right format? I've tried just typing the G, I've tried Asc(G), I even tried chr(G) even though I'm pretty sure that's converting in the wrong direction.

Any help?

Link to comment
Share on other sites

Ok, that did it thanks

One other question, if I wanted to create an edit box that lets me change these inputs rather than coding them in the sequence, it looks like the edit box will only send values to a channel. I've got it sending to virtual channels I've created. But how do I put that in a sequence? How do I get the value inside the write function?

Link to comment
Share on other sites

I do not recommend using edit boxes except for forms inside configuration pages (see the blog). Instead, and especially when you need to do an action at the same time, I recommend using a popup edit box. (button with Set To action) The easiest way to do this is to create a Test String channel, then in the event of that channel, put the command. Something like:

device.stepmotor.write(mychannel[0])

Then whenever you set mychannel to a value it will send it out the serial port.

Link to comment
Share on other sites

Ok, using the test channels worked perfectly. Now I have another question for serial. It looks like the pins that let me read the two outputs from the motors encoder are disabled so I can't just set up a set of quadrature timers. The only way to get position data from the encoder is through the serial connection. There is a setting that causes the motor to send an ASCII "M" every time the motor steps. What is the best way to capture this along with time so I can back speed out of it?

Link to comment
Share on other sites

Speed? That's going to be hard because the input gets buffered so you won't get the exact time that the device sent that "M". Also, there's no directional encoding. Personally, I would consider purchasing an encoder and attaching that to the system to get exact position. It will be much more reliable and easier to setup.

Link to comment
Share on other sites

If its only one direction I suppose you could count the M's coming in. Just create a sequence that reads the port and counts the M's. Something like this (I'm doing this off the cuff):

global pos = 0
private string in
while(1)
   try
	  in = device.mydevice.read(1)
	  pos += (in == "M")
   catch()
   endcatch
endwhile

Link to comment
Share on other sites

  • 1 month later...

So I have the sequence correctly counting pulses, but if i wanted to put these pulse counts to a file how would i code that. I have the following in the sequence at the moment.

private Filehandle = file.Open("c:\stepdata.csv",0,1,1,1)
global pos = 0
private string in
private string stringpos
while(1)
   try
	  in = device.stepmotor.read(1)
	  pos += (in == "m")
	  stringpos =doubletostr(pos)
	   file.Write(Filehandle,stringpos)
   catch()
   endcatch
endwhile
file.Close(Filehandle)

but this is just creating the file. It's not writing anything to the file. My end goal here is to put each count in a file along side a time stamp so that I can later match this data up with another channel.

Link to comment
Share on other sites

That worked well for counting the pulses, but when I add another channel that is reading the signal from a load cell, the export stops recording the "pos" variable, I just get the ""time, ,load cell" for each row.

Any thoughts on why the export isn't recording both at the same time?

Link to comment
Share on other sites

Data alignment. The load cell channel and the pos channel have different time stamps so they don't end up on the same row. I'd do this:

1) create a new global variable with the load cell reading, let's say "load_cell_log". Have the export set log that instead of the channel.

2) in your event, change this line:

pos += (in == "m")

to something like this:

pos += (in == "m")

pos.time = systime()

load_cell_log = load_cell[0]

load_cell_log.time = pos.time

beginExport(myexport)

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.